/* SVN $Id: main.js 96 2009-05-08 14:09:24Z jeroen $ */
/* SVN $URL: file:///T:/SOURCE_SVN/Start_Website/httpdocs/js/main.js $ */

/*############################################################################*/
/*# Initialize */

$('document').ready(function(){
	initOfferte();
	initSlidingBoxes();
	loadImages();
  GoogleLoadMap();
})

/*# /Initialize */
/*############################################################################*/
/* Google */

function GoogleLoadMap() {
  if (GBrowserIsCompatible() && null !== document.getElementById("map")) {
    var map = new GMap2(document.getElementById("map"));
    var point = new GLatLng(51.3157781400069, 5.33890444965238);

    map.setCenter(point, 14);
    map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());
    map.setMapType(G_NORMAL_MAP);

    var marker = new GMarker(point);
    var html = "<b>All Print Nederland</b><br>Elskensakker 10<br>5571 SK Bergeijk<br><br>0497-336908";
    GEvent.addListener(marker, 'click', function() { marker.openInfoWindowHtml(html); });

    map.addOverlay(marker);

    
  }
}



/*# Slider */

function loadImages(url, dirid)
{
  var _data = [];
  var _json = [];
  var _url = url;
  var _id = dirid;
        
  $.ajax({
    url: _url,
    dataType: "json",
    success: function(data) {
      onJSONComplete(data);
    }
  });

  function onJSONComplete(data){
      var success = parseTree(data.images, _id, _data);
      if(success) {
        if(_data.length) {
          setTimeout(getImage, 100);
        }
      }
   }

  function parseTree(object, id, target){
    var success = false;
    var i;

    if(object.id.toLowerCase() == id){
      for (i in object.aI){
        target.push(object.aI[i]);
      }
      success = true;
    }
    else {
      for(i in object.aF){
        success = parseTree(object.aF[i], id, target);
        if(success){
          break;
        }
      }

      if (!success) {
        for (i in object.aI){
          if(object.aI[i].id.toLowerCase() == id){
            target.push(object.aI[i]);
            success = true;
            break;
          }
        }
      }
    }
    return success;
  }
  
  function getImage(){
    $('#slidermenu').append('<span class="slidermenu_inner" />')

    for(var i=0; i<_data.length; i++){
      $(".slidermenu_inner").append('<span id="item'+ i +'">'+ String(i + 1) + '</span>');
      if(_data[i].title){
        $('<a href="'+ _data[i].title +'"><img src="'+ _data[i].url +'" /></a>').appendTo('#slidercontent');
      } else {
        $('<img src="'+ _data[i].url +'" />').appendTo('#slidercontent');
      }
    };

    initSlider();
  }
}

function initSlider()
{
  var itemCurrentID;
  var itemCurrent;
  var itemPrevious;
  var itemLength = $('#slidercontent').children().length;
  var itemTimerLength = 6000;
  var itemAutoScroll = true;
  var itemTimer;

  showItem(0)

  $('.slidermenu_inner').children().click(function() {
    unsetTimer()
    var id = Number(($(this).attr('id')).substr(4));
    showItem(id);
    setTimer();
	});
  
  $('#slidercontent').mouseenter(function() {
    unsetTimer()
  });
  
  $('#slidercontent').mouseleave(function() {
    setTimer();
  });
  
  function showItem(id) {
  
    if(itemCurrentID != id) {
    
    	$('.slidermenu_inner').children().removeClass('selected');
      $('#item'+String(id)).addClass('selected');
      
           
      if(itemCurrent) {
        itemPrevious = itemCurrent;
        itemPrevious.css("zIndex", 1);
        itemPrevious.animate({left:-$('#slidercontent').width()}, 500);  
      }
      
      if(itemPrevious) {
        itemPrevious.animate({left:$('#slidercontent').width()}, 0);
      }
      
      itemCurrent = $('#slidercontent').children().eq(id);
      itemCurrentID = id;
    
      itemCurrent.css("zIndex", 2);
      itemCurrent.css("left", $('#slidercontent').width());
      itemCurrent.animate({left:0}, 500);
    }
  }
  
  function nextItem() {
    
    if(itemCurrentID + 1 >= itemLength) {
      showItem(0);
    }
    else {
      showItem(itemCurrentID + 1);
    }
   
  }

  function setTimer(){
    if(itemAutoScroll){
      itemTimer = setInterval(nextItem, itemTimerLength);
    }
  }
  
  function unsetTimer(){
    if(itemTimer){
      clearInterval(itemTimer);
    }
  }
  
  setTimer();

};

/*# /Slider */
/*############################################################################*/
/*# Offerte */

 function initOfferte(){
	 
	 //Hide div w/id extra
	 $("#offerte_foldout").css("display","none");

		// Add onclick handler to checkbox w/id checkme
	 $("#offertecheck").click(function(){
	 
		// If checked
		if ($("#offertecheck").is(":checked"))
		{
				//show the hidden div
				$("#offerte_foldout").show("fast");
		}
		else
		{     
				//otherwise, hide it
				$("#offerte_foldout").hide("fast");
		}
	});
	 
 }
	 
/*# /Offerte */
/*############################################################################*/
/*# Homeboxes */

 function initSlidingBoxes(){


			//To switch directions up/down and left/right just place a "-" in front of the top/left attribute
			//Vertical Sliding
			$('.boxgrid.slidedown').hover(function(){
				$(".cover", this).stop().animate({top:'275px'},{queue:false,duration:300});
			}, function() {
				$(".cover", this).stop().animate({top:'0px'},{queue:false,duration:300});
			});
			//Horizontal Sliding
			$('.boxgrid.slideright').hover(function(){
				$(".cover", this).stop().animate({left:'325px'},{queue:false,duration:300});
			}, function() {
				$(".cover", this).stop().animate({left:'0px'},{queue:false,duration:300});
			});
			//Diagnal Sliding
			$('.boxgrid.thecombo').hover(function(){
				$(".cover", this).stop().animate({top:'260px', left:'325px'},{queue:false,duration:300});
			}, function() {
				$(".cover", this).stop().animate({top:'0px', left:'0px'},{queue:false,duration:300});
			});
			//Partial Sliding (Only show some of background)
			$('.boxgrid.peek').hover(function(){
				$(".cover", this).stop().animate({top:'90px'},{queue:false,duration:160});
			}, function() {
				$(".cover", this).stop().animate({top:'0px'},{queue:false,duration:160});
			});
			//Full Caption Sliding (Hidden to Visible)
			$('.boxgrid.captionfull').hover(function(){
				$(".cover", this).stop().animate({top:'160px'},{queue:false,duration:160});
			}, function() {
				$(".cover", this).stop().animate({top:'260px'},{queue:false,duration:160});
			});
			//Caption Sliding (Partially Hidden to Visible)
			$('.boxgrid.caption').hover(function(){
				$(".cover", this).stop().animate({top:'160px'},{queue:false,duration:160});
			}, function() {
				$(".cover", this).stop().animate({top:'220px'},{queue:false,duration:160});
			});

 }
		
/*# /Homeboxes */		
/*############################################################################*/
		
