function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function PopupCentrata(url,w,h) {
   var l = Math.floor((screen.width-w)/2); // default width = 640
   var t = Math.floor((screen.height-h)/2); // default height = 400
   //window.open(url,"","width=" + w + ",height=" + h + ",top=" + t + ",left=" + l);
   window.open(url,"","scrollbars,width=" + w + ",height=" + h + ",top=" + t + ",left=" + l);
}

function getContent(txt) {
	var parts = txt.split('|');
	return '<div class="title">'+parts[0]+'</div><div class="comment">'+parts[1]+'</div>';
}

// This method is called right before the ajax form validation request
// it is typically used to setup some visuals ("Please wait...");
// you may return a false to stop the request 
function beforeCall(form, options){
    //if(console) console.log("Right before the AJAX form validation call");

    $(form[0]).validationEngine('hide');
    $(form[0]).hide();
    $('#newsletter-response').hide();
    $('#newsletter-loading').show();
    return true;
}

$(function() {	
 $(".fancybox").fancybox();
 
 $('.picturelist .thumb').each(function() {
		$(this).qtip({
			content: getContent($(this).attr('alt')),
			//content: $(this).attr('alt'),
			position: {
				corner: {
					target: 'topMiddle',
					tooltip: 'bottomMiddle'
				},
				//target: 'mouse',
				adjust: {
					y: -1
				}
			},
			show: {
				when: 'mouseover',
				solo: false,
				delay: 0,
				effect: {
					length: 100
				}
			},
			hide: { 
				effect: {
					length: 200
				}
			},
			style: {
				tip: {
					corner: 'bottomMiddle',
					size : {
						x: 6,
						y: 6
					}
				},
				border: {
					width: 3,
					radius: 1,
					color: '#999999'
				},
				padding: 10,
				width: { min: 75, max: 300},
				textAlign: 'left',
				background: '#F4DCCC',
				color: '#666666',
				name: 'cream'
			}
		});
	});
	
	$('.picturelist .thumb').click(function () {
 		$('.qtip').hide();	
  });
  
	$("#autocomplete").autocomplete({
		source: "index.php?action=search",
		minLength: 2,
		select: function( event, ui ) {
		 $('#search-product-id').attr('value', ui.item.id);
		 $('#search form').submit();
		}
	});
});

$(document).ready(function() {
 //transitions
 //for more transition, goto http://gsgd.co.uk/sandbox/jquery/easing/
 var style = 'easeOutQuint';
 //var style = 'easeOutElastic';
 
 //Retrieve the selected item position and width
 if($('#menu').find('li').hasClass('active')) {
 	var default_left = Math.round($('#menu li.active').offset().left - $('#menu').offset().left) + 15;
  var default_width = $('#menu li.active').width();
 }
 else {
	default_left = -100;
  default_width = 0;
 }
 
 //Set the floating bar position and width
 $('#box').css({left: default_left});
 $('#box .head').css({width: default_width});
 
 //if mouseover the menu item
 $('#menu li').hover(function () {
 		
 	//Get the position and width of the menu item
 	left = Math.round($(this).offset().left - $('#menu').offset().left) + 15;
 	width = $(this).width(); 
 
 	//Set the floating bar position, width and transition
 	$('#box').stop(false, true).animate({left: left},{duration:1000, easing: style});	
 	$('#box .head').stop(false, true).animate({width:width},{duration:1000, easing: style});	
 
 	//if user click on the menu
 }).click(function () {
 		
 		//reset the selected item
 		$('#menu li').removeClass('active');	
 		
 		//select the current item
 		$(this).addClass('active');
 
 });
 	
 //If the mouse leave the menu, reset the floating bar to the selected item
 $('#menu').mouseleave(function () {
 
 	//Retrieve the selected item position and width
 	if($('#menu').find('li').hasClass('active')) {
	 default_left = Math.round($('#menu li.active').offset().left - $('#menu').offset().left) + 15;
	 default_width = $('#menu li.active').width();
  }
  else {
	 default_left = -100;
	 default_width = 0;
  }
 	
 	//Set the floating bar position, width and transition
 	$('#box').stop(false, true).animate({left: default_left},{duration:1500, easing: style});	
 	$('#box .head').stop(false, true).animate({width:default_width},{duration:1500, easing: style});		
 	
 });
});
