$(document).ready(function() {
	
	
	/*
	 * Things to controll the height of body
	 * its  needed when there is a background image
	 * */
	if(document.location.href!="http://localhost/onbrunos/coopdugrandorme/coopdugrandorme/")
	{
		if(document.location.href!="http://localhost/onbrunos/coopdugrandorme/coopdugrandorme/index.php?route=common/home")
		{
			//alert("im made");
			//alert(document.location.href)	
			$('.insideContent').has('img').css('min-height','770px');
		}
		$('li.category a').click(
				function() {
					//alert("sss");
					 if(clicks<1)
						 $('.insideContent').height(640);
					});
		
	}
	else
	{
		$('li.category a').click(
				function() {
					 if(clicks<1)
						 $('.insideContent').has('#slider2').css('min-height','640px');
			      });
	}
	/////////////////////////////////////////////////////////////////////////////////
	/* Search */
	$('#search .left').bind('click', function() {
		url = 'index.php?route=product/search';
		 
		var filter_name = $('input[name=\'filter_name\']').attr('value')
		
		if (filter_name) {
			url += '&filter_name=' + encodeURIComponent(filter_name);
		}
		
		location = url;
	});
	
	$('input[name=\'filter_name\']').keydown(function(e) {
		if (e.keyCode == 13) {
			url = 'index.php?route=product/search';
			 
			var filter_name = $('input[name=\'filter_name\']').attr('value')
			
			if (filter_name) {
				url += '&filter_name=' + encodeURIComponent(filter_name);
			}
			
			location = url;
		}
	});
	
	/* Ajax Cart */

	$('#cart > .heading a').bind('click', function() {
		$('#cart').addClass('clicked');

		$('#cart .content').css('display','block');

		$('#cart').css('background-color','white');
		$.ajax({
			url: 'index.php?route=checkout/cart/update',
			dataType: 'json',
			success: function(json) {
			

				if (json['output']) {
					$('#cart .content').html(json['output']);
				}
				
				
				if($('#cart .content').has('.empty'))
				{
					$('#cart .content').css('padding-top','40px');
					//alert("hihi");
					
//'Shopping Cart'
				}
				

			}
		});			
		
		$('#cart ').bind('mouseleave', function() {
			$('#cart').removeClass('clicked');
			$('#cart').css('background','none');
			$('#cart .content').css('display','none');
		});
	});
	
	/* Mega Menu */
	

	
	var kids;
	var spot;
	$('ul#navigator li').has('ul').hover(function() 
			{
				//$(this).add('div');
				
				 kids=$(this).children('ul');
				 
				
				//alert($(this).width());
				 var width=$(this).width();
					//alert(width);
				
				 
				 
				// alert(spot.width());
				//$(this).css('background','#E5742E;');
				kids.hide();
				kids.css('visibility','visible');
				kids.slideDown("normal");
				
				
					//$('ul#navigator ul ').slideToggle("normal");
				} ,function()
						{
							
					kids.slideToggle("fast");
					kids.css('visibility','hidden');
							//$(this).css('background','none');
						
					
						});
//mega menu ends-------------
	
	
	
	/*calendar and accordion*/
	
	
	
	//trying to find the url contents
	
	   var categoriesAreImportant=false;
		var urlcontents=document.location.href.split('?');
		
//		alert (urlcontents[1]);
//		var indi=urlcontents[1].indexOf("product")
//		alert (indi);
		if(!(typeof urlcontents[1] == 'undefined'))
			{ 
				if(urlcontents[1].indexOf("product")!=-1)
				categoriesAreImportant=true;
			}
		var clicks = 0;

	//$('#calendarForm form').hide();
	$(' #menu ul').hide();
	if(!categoriesAreImportant)
	  $(' #menu ul:first').show();
	else
		$(' #menu li.category ul').show();
	
	  //$(' #menu ul:first').show();
	 
	$('#menu li a.modil').click(
	    function() {
	      var checkElement = $(this).next();
	      if((checkElement.is('ul')) && (checkElement.is(':visible'))) {
	    	 
	        return false;
	        }
	      if((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
	    	  
	        $('#menu ul:visible').slideUp('normal');
	        checkElement.slideDown('normal');
	        return false;
	        }
	     
	      }
	    );
	 
	
	
	var timounYo;
	  $('ul#menu li.papaTimoun').has('ul').hover(
			  function()
			  {//alert("--")
				  
				 // var checkElement = $(this).next();
				  // kids=$(this).children('ul');
				  var checkElement = $(this).children('ul');
			  // alert(checkElement.contents());
				  timounYo=checkElement;
				  if((checkElement.is('ul')) && (checkElement.is(':visible'))) {
					//  alert(checkElement+"--")
				       // return false;
				        }
				  if((checkElement.is('ul.timoun')) && (!checkElement.is(':visible'))) {
				        //$('#menu ul:visible').slideUp('normal');
					  //
					  //alert(checkElement+"..")
					 // checkElement.show();
					  checkElement.css('visibility','visible');
				        checkElement.show('normal');
				      //  return false;
				        }
			  },
			  function()
			  {
				  var checkElement =timounYo; 
				  if((checkElement.is('ul')) && (checkElement.is(':visible'))) {
					 // alert("dd")
					   checkElement.css('visibility','visible');
					  checkElement.hide();
				       // return false;
				        }
				  
			  });
	$('li.event a').click(
		function() {
			 if(clicks<1)
					$('fieldset.jcalendar').jcalendar();
		
			clicks++;
	       // return false;
	      });
	
	


	/*
	 * calendar and accordion
	 * ends=====================================
	 * */
	
	
	///anything slider things---------------------------------------------------------------
	// Set up Sliders
	// **************
	
	
	var titles= new Array();;
	$.ajax({
		url: 'index.php?route=module/anythingslider/showTitles',
		dataType: 'json',
		async:false,
		success: function(json) 
		{
			//alert ("bout to do it");
			if(json)
			{
				var test=json[0]['title'];
				
				for(x in json)
				{
					titles[x]=json[x] ;
					//alert(titles[x].lang);
					
				}
			}
			
		}
	});

	
	var start;
	var stop;
	
	if(titles[x].lang=='fr')
		{
		start='D&eacute;marrer';
		stop='Arr&ecirc;t';
		}
	else
		{
		stop='stop';
		start='start';
		}
	
	while(typeof titles == 'undefined');
	//alert("done");
	
	for(x in titles)
	{
		titles[x] = titles[x].title;
		//alert(titles[x]);
	}
	
	//alert(titles);
	$(function(){

		$('#slider2').anythingSlider({
			width               : 520,   // if resizeContent is false, this is the default width if panel size is not defined
			height              : 400,   // if resizeContent is false, this is the default height if panel size is not defined
			resizeContents      :false, // If true, solitary images/objects in the panel will expand to fit the viewport
			startStopped        : true,  // If autoPlay is on, this can force it to start stopped
			autoPlay            : true,
			startText			:start,
			stopText			:stop,
			navigationFormatter : function(index, panel){ // Format navigation labels with text
				return titles[index-1];//return ['la coop', 'membres', 'our name and logo', ' Prix M&eacute;rite Cooporatif 2011 '][index - 1];
			}
		});

	});
	
	
		
	
//	things about the colorbox
	//for the events
	$(".forevent").colorbox({width:"500px", height:"500px", iframe:true});
	$(".example7").colorbox({width:"760px", height:"600px", iframe:true});
	$(".productImage").colorbox();

});

$('.success img, .warning img, .attention img, .information img').live('click', function() {
	$(this).parent().fadeOut('slow', function() {
		$(this).remove();
	});
});


function addToCart(product_id) {
	
	//alert("atleast i am in the method("+product_id+")");
	$.ajax({
		url: 'index.php?route=checkout/cart/update',
		type: 'post',
		data: 'product_id=' + product_id,
		dataType: 'json',
		success: function(json) {
			$('.success, .warning, .attention, .information, .error').remove();
			
			if (json['redirect']) {
				location = json['redirect'];
			}
			
			if (json['error']) {
				if (json['error']['warning']) {
					$('#menu').after('<div class="warning" style="display: none;">' + json['error']['warning'] + '<img src="catalog/view/theme/default/image/close.png" alt="" class="close" /></div>');
				}
			}	 
						
			if (json['success']) {
				//alert (product_id)
				//$('#menu').after('<div class="attention" style="display: none;">' + json['success'] + '<img src="catalog/view/theme/default/image/close.png" alt="" class="close" /></div>');
				$('.breadcrumb').after('<div class="attention" style="display: none;">' + json['success'] + '<img src="catalog/view/theme/default/image/close.png" alt="" class="close" /></div>');
				$('.attention').fadeIn('slow');
				
				$('#cart_total').html(json['total']);
			}	
		}
	});
	
	
   
	
	
}

function removeCart(key) {
	$.ajax({
		url: 'index.php?route=checkout/cart/update',
		type: 'post',
		data: 'remove=' + key,
		dataType: 'json',
		success: function(json) {
			$('.success, .warning, .attention, .information').remove();
			
			if (json['output']) {
				$('#cart_total').html(json['total']);
				
				$('#cart .content').html(json['output']);
			}			
		}
	});
}

function removeVoucher(key) {
	$.ajax({
		url: 'index.php?route=checkout/cart/update',
		type: 'post',
		data: 'voucher=' + key,
		dataType: 'json',
		success: function(json) {
			$('.success, .warning, .attention, .information').remove();
			
			if (json['output']) {
				$('#cart_total').html(json['total']);
				
				$('#cart .content').html(json['output']);
			}			
		}
	});
}

function addToWishList(product_id) {
	$.ajax({
		url: 'index.php?route=account/wishlist/update',
		type: 'post',
		data: 'product_id=' + product_id,
		dataType: 'json',
		success: function(json) {
			$('.success, .warning, .attention, .information').remove();
						
			if (json['success']) {
				$('#menu').after('<div class="attention" style="display: none;">' + json['success'] + '<img src="catalog/view/theme/default/image/close.png" alt="" class="close" /></div>');
				
				$('.attention').fadeIn('slow');
				
				$('#wishlist_total').html(json['total']);
			}	
		}
	});
}

function addToCompare(product_id) {
	$.ajax({
		url: 'index.php?route=product/compare/update',
		type: 'post',
		data: 'product_id=' + product_id,
		dataType: 'json',
		success: function(json) {
			$('.success, .warning, .attention, .information').remove();
						
			if (json['success']) {
				$('#menu').after('<div class="attention" style="display: none;">' + json['success'] + '<img src="catalog/view/theme/default/image/close.png" alt="" class="close" /></div>');
				
				$('.attention').fadeIn('slow');
				
				$('#compare_total').html(json['total']);
			}	
		}
	});
}

