var currentLocation = new String(window.location);
var mlid = 1;
$.easing.drop = function (x, t, b, c, d) {
	return -c * (Math.sqrt(1 - (t/=d)*t) - 1) + b;
};
jQuery.easing.easeOutQuart = function (x, t, b, c, d) {
	return -c * ((t=t/d-1)*t*t*t - 1) + b;
};


$(document).ready(function(){   
	//getContent(mlid);
	//test
	setAAction();
	$('img.fancyzoom').fancyzoom();
	var $pancontainer=$('div.pancontainer')
	$pancontainer.each(function(){
		var $this=$(this).css({position:'relative', overflow:'hidden', cursor:'move'})
		var $img=$this.find('img:eq(0)') //image to pan
		var options={$pancontainer:$this, pos:$this.attr('data-orient'), curzoom:1, canzoom:$this.attr('data-canzoom'), wrappersize:[$this.width(), $this.height()]}
		$img.imgmover(options)
	});	
	//end test
	$("img.jqzoom").jqueryzoom({
				xzoom: 240, //zooming div default width(default width value is 200)
				yzoom: 240, //zooming div default width(default height value is 200)
				offset: 10 //zooming div default offset(default offset value is 10)
				//position: "right" //zooming div position(default position value is "right")
			});
			var options4 =
			{

				zoomWidth: 240,
				zoomHeight: 240,
				position : 'left',
				yOffset :-100,
				xOffset :100,
				title :false


			}
			$(".jqzoom").jqueryzoom(options4);			
	$.fn.fancyzoom.defaultsOptions.imgDir='./aimages/ressources/'; //very important must finish with a /

	$('img.fancyzoom').fancyzoom();
	//$('#items a').unbind('click');
	//$('#items a').fancyzoom();
	// initialize scrollable
	//$(".scrollable").scrollable();
	//$('#scrollableimagesdiv  a').unbind('click');
	//$(".scrollable").scrollable();	
//initImageZoom();
$.tools.overlay.addEffect("drop", function(css, done) { 
   
   // use Overlay API to gain access to crucial elements
   var conf = this.getConf(),
       overlay = this.getOverlay();           
   
   // determine initial position for the overlay
   if (conf.fixed)  {
      css.position = 'fixed';
   } else {
      css.top += $(window).scrollTop();
      css.left += $(window).scrollLeft();
      css.position = 'absolute';
   } 
   
   // position the overlay and show it
   overlay.css(css).show();
   
   // begin animating with our custom easing
   overlay.animate({ top: '+=55',  opacity: 1,  width: '+=20'}, 400, 'drop', done);
   
   /* closing animation */
   }, function(done) {
      this.getOverlay().animate({top:'-=55', opacity:0, width:'-=20'}, 300, 'drop', function() {
         $(this).hide();
         done.call();      
      });
   }
);
	$("a[rel]").overlay({
		mask: 'black',
		effect: 'apple',

		/*effect: 'drop',
			mask: {
		color: '#ebecff',
		loadSpeed: 200,
		opacity: 0.9
	},*/

		onBeforeLoad: function() {

			// grab wrapper element inside content
			var wrap = this.getOverlay().find(".contentWrap");

			// load the page specified in the trigger
			wrap.load(this.getTrigger().attr("href"));
		}

	});
	$('#divResults').serialScroll({
		items:'li',
		prev:'#scrollableimagesdiv a.prev',
		next:'#scrollableimagesdiv a.next',
		offset:0, //when scrolling to photo, stop 230 before reaching it (from the left)
		start:0, //as we are centering it, start at the 2nd
		duration:1800,
		force:true,
		stop:true,
		lock:false,
		cycle:false, //don't pull back once you reach the end
		easing:'easeOutQuart', //use this easing equation for a funny effect
		jump: true //click on the images to scroll to them
	});

});

$(function() {

    function scroll(direction) {

        var scroll, i,
                positions = [],
                here = $(window).scrollTop(),
                collection = $('.post');

        collection.each(function() {
            positions.push(parseInt($(this).offset()['top'],10));
        });

        for(i = 0; i < positions.length; i++) {
            if (direction == 'next' && positions[i] > here) { scroll = collection.get(i); break; }
            if (direction == 'prev' && i > 0 && positions[i] >= here) { scroll = collection.get(i-1); break; }
        }

        if (scroll) {
            $.scrollTo(scroll, {
                duration: 750       
            });
        }

        return false;
    }

    $("#next,#prev").click(function() {        
        return scroll($(this).attr('id'));        
    });

    $(".scrolltoanchor").click(function() {
        $.scrollTo($($(this).attr("href")), {
            duration: 750
        });
        return false;
    });

});


function makeActive(id){
	$('ul').find('#current').removeAttr('id');
	$('ul').find('.item'+id).attr('id', 'current');
}
function changeID(id){
	mlid = id;
}
function getContent(lid){

	$.ajax({
	  type: 'GET',
	  url: 'assasa.helper.php',
	  data: { func: 'menuitems', id: lid },
	  success:function(data){
		$("#menuitemsdiv").html(data);
		//setAAction();
			$.ajax({
			  type: 'GET',
			  url: 'assasa.helper.php',
			  data: { func: 'menu1items', id: lid },
			  success:function(data){
				$("#menu1itemsdiv").html(data);
				//setAAction();
				$.ajax({
				  type: 'GET',
				  url: 'assasa.helper.php',
				  data: { func: 'menuimagename', id: lid },
				  success:function(data){
					$("#menuimagenametd").attr("background", data);
					$.ajax({
					  type: 'GET',
					  url: 'assasa.helper.php',
					  data: { func: 'menu1imagename', id: lid },
					  success:function(data){
						$("#menu1imagenametd").attr("background", data);
						$.ajax({
						  type: 'GET',
						  url: 'assasa.helper.php',
						  data: { func: 'customimage', id: lid },
						  success:function(data){
							$("#customimagenameimg").attr("src", data);
							$.ajax({
							  type: 'GET',
							  url: 'assasa.helper.php',
							  data: { func: 'menutext', id: lid },
							  success:function(data){
								$("#menutextdiv").html(data);
								//setAAction();
								$.ajax({
								  type: 'GET',
								  url: 'assasa.helper.php',
								  data: { func: 'menu1text', id: lid },
								  success:function(data){
									$("#menu1textdiv").html(data);
									setAAction();
									$('img.fancyzoom').fancyzoom();
	var $pancontainer=$('div.pancontainer')
	$pancontainer.each(function(){
		var $this=$(this).css({position:'relative', overflow:'hidden', cursor:'move'})
		var $img=$this.find('img:eq(0)') //image to pan
		var options={$pancontainer:$this, pos:$this.attr('data-orient'), curzoom:1, canzoom:$this.attr('data-canzoom'), wrappersize:[$this.width(), $this.height()]}
		$img.imgmover(options)
	});
	

								  }
								});								
							  }
							});							
						  }
						});						
					  }
					});  					
				  }
				});				
			  }
			}); 		
	  }
	});   
   //alert("Content loaded");
   return true;
}
function setAAction(){
//alert("modifieg clicks");
/*$('a').unbind('click');

	$('a').click(function(event){
		
		var location = this + "";
		
		currentLocation = new String(location);
	
		loadJContent(location, '#jcontent', {'pure': '1'});
		mlid = gup("id",location);
		$(this).parent().parent().parent().find('#current').removeAttr('id'); 
		$(this).parent().attr('id', 'current');
		//alert("son"+mlid);
		getContent(mlid);
		setAAction();
		return false;
	});
*/
	$('#scrollableimagesdiv  a').unbind('click');
	//$(".scrollable").scrollable();
	$('img.fancyzoom').fancyzoom();
}
function gup( name , link)
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( link );
  if( results == null )
    return "";
  else
    return results[1];
}
function loadJContent(location, container, variables){
	
	
	//$(container).fadeOut('fast');
	$(container).hide();
	
	$('#preloader').fadeIn('fast');
	
	$.get(location, variables, function(data, status) {
				
			iFound = data.indexOf("$$^^");
			while(iFound > -1){
				iFound = data.indexOf("$$^^");
				data = data.replace("$$^^", '@');
			}
			
			$('#preloader').hide();
			
			$(container).html(data);
						
			$(container).fadeIn();
							
		});
}
  function drawPercentBar(width, percent, color, background) 
  { 
    var pixels = width * (percent / 100); 
    if (!background) { background = "none"; }
 
    document.write("<div style=\"position: relative; line-height: 1em; background-color: " 

                   + background + "; border: 1px solid black; width: " 
                   + width + "px\">"); 
    document.write("<div style=\"height: 1.5em; width: " + pixels + "px; background-color: "
                   + color + ";\"></div>"); 
    document.write("<div style=\"position: absolute; text-align: center; padding-top: .25em; width: " 
                   + width + "px; top: 0; left: 0\">" + percent + "%</div>"); 

    document.write("</div>"); 
  }
  function drawPercentBarTall(height, percent, color, background) 
  { 
    var pixels = height * (percent / 100); 
    if (!background) { background = "none"; }
     document.write("<div style=\"position: relative; text-align: center; padding-top: .25em; top: 0; left: 0\">" + percent + "%</div>");
    document.write("<div style=\"position: relative; line-width: 1em; background-color: " 
                   + background + "; border: 1px solid black;width: 2em; height: " 
                   + height + "px\">");
    document.write("<div style=\"width: 2em; height: " + (height-pixels) + "px;\"></div>");				   
    document.write("<div style=\"width: 2em; height: " + pixels + "px; background-color: "
                   + color + ";margin-top: " + (height-pixels) + ";\"></div>"); 
 

    document.write("</div>"); 
  }  
