/*
 * fslider is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * fslider is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with fslider.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Author: Bryan Nielsen, bnielsen1965@gmail.com
 * Copyright 2010, Bryan Nielsen
 */

var $j = jQuery.noConflict()
var featuresjson = null;
var animated = false;
var slidetimer = null;
var imgi = 0;
var imgpage = 0;

function setupview() {
  if( featuresjson.features ) {
    resetslidetimeout()
    animated = true
    setupviewsequence()
    $j(".slidelist").hover(
      function() {
        if( !$j(this).hasClass("activelist") ) $j(this).addClass("hoverlist")
      },
      function() {
        if( !$j(this).hasClass("activelist") ) $j(this).removeClass("hoverlist")
      }
    )

    // slide clicked
    $j(".slidelist").click(function() {
      if( !animated ) {
        imgi = $j(this).index()
        slide()
      }
    })
  }
}

// move to the next slide
function nextslide() {
  if( !animated ) {
    animated = true
    imgi += 1
    if( imgi > 3 ) {
      imgi = 0
      imgpage += 1
      if( imgpage * 4 >= featuresjson.features.length ) imgpage = 0
      setupview()
    }
    slide()
  }
}

// slide
function slide() {
  animated = true
  resetslidetimeout()

  // change active class
  $j(".activelist").removeClass("activelist")
  if( $j(".slidelist:eq(" + imgi + ")").hasClass("hoverlist") ) $j(".slidelist:eq(" + imgi + ")").removeClass("hoverlist")
  $j(".slidelist:eq(" + imgi + ")").addClass("activelist")

  // define img tag string
  imgstr = featuresjson.features[imgi + imgpage * 4].img
  if( imgstr.length > 0 ) {
    imgstr = '<img src="' + featurespath + '/' + imgstr + '" /><div class="slidedetail"></div>'
  }
  else imgstr = "&nbsp;"

  // setup and slide slide2
  $j(".slide:eq(1)").html(imgstr)
  $j(".slide:eq(1) .slidedetail").html(featuresjson.features[imgi + imgpage * 4].html)
  $j(".slide:eq(1)").animate(
    {
      left: "0px"
    },
    1000,
    function() {
      $j(".slide:eq(0)").html($j(".slide:eq(1)").html())
      $j(".slide:eq(0) .slidedetail").html($j(".slide:eq(1) .slidedetail").html())
      $j(".slide:eq(0)").css("left", "0px")
      $j(".slide:eq(1)").css("left", "500px")
      animated = false
      setslidetimeout()
    }
  )
}



// sequentially set up the slides
function setupviewsequence() {
  if( featuresjson.features[imgi + imgpage * 4].img.length > 0 ) {
    cacheimage = document.createElement('img')
    cacheimage.src = featurespath + '/' + featuresjson.features[imgi + imgpage * 4].img

    if( cacheimage.complete || cacheimage.readyState == 'complete') {
      setslideimage()
    }
    else {
      $j(cacheimage).bind('load', function(e) {
        setslideimage()
      })
    }
  }
  else {
    setslideimage()
  }
}

// set slide image
function setslideimage() {
  // define img tag string
  imgstr = featuresjson.features[imgi + imgpage * 4].img
  if( imgstr.length > 0 ) {
    imgstr = '<img src="' + featurespath + '/' + imgstr + '" />'
  }
  else {
    imgstr = "&nbsp;"
  }

  htmlstr = featuresjson.features[imgi + imgpage * 4].html
  if( htmlstr.length == 0 ) htmlstr = "&nbsp;"

  $j("div.slideimg:eq(" + imgi + ")").html(imgstr)
  $j("div.slidehtml:eq(" + imgi + ")").html(htmlstr)
  if( imgi < 3 ) {
    imgi += 1
    setupviewsequence()
  }
  else imgi = 0
}

// reset slide timeout
function resetslidetimeout() {
  if( slidetimer != null ) clearTimeout(slidetimer)
  slidetimer = null
}

// set slide timeout
function setslidetimeout() {
  slidetimer = setTimeout("nextslide()", 5000)
}

// setup events when the document is ready
$j(document).ready(function() {

  $j(".slide:eq(0)").css("z-index", "900")
  $j(".slide:eq(1)").css("z-index", "999")

  // get the feature components
  $j.getJSON(jsonpath + '/features-json.php', function(data) {
    featuresjson = data
    setupview()
    slide()
  })


})

