//////////////////////////////////////////////////////////////////////////////////////////
////GENERAL FUNCTIONS
//////////////////////////////////////////////////////////////////////////////////////////
function equalHeight(group) {
	tallest = 0;
	group.each(function() {thisHeight = $(this).height();if(thisHeight>tallest){tallest=thisHeight;}});
	group.height(tallest);
}
//glo_pagepath


$(document).ready(function() {
	/*SHOPPING FUNCTIONS*/
	/* THUMBNAILS*/
	$('a.item_thumb_link').click(function(event) {
		event.preventDefault();
		thumb_bg = $(this).children(".item_thumb").css('backgroundImage');
		thumb_bg = thumb_bg.replace('url(', '');
		thumb_bg = thumb_bg.replace(')', '');
		thumb_bg = thumb_bg.replace('small', 'medium');
		$('.item_main_image').css("background-image", "url("+thumb_bg+")"); 
		//item_main_image
	});
	// COLOUR SELECT SWATCH PREVIEW
	$('#colour_select').change(function() {
		 $("#colour_select option:selected").each(function () {
                colour = $(this).attr('rel');
         });
		if(colour!=''){
			$('.colour_preview').hide();
			$('.colour_preview').css('background-color','#'+colour);
			$('.colour_preview').slideDown();
		} else {
			$('.colour_preview').hide();	
		}
	});
	
	//SHIPPING COST ADJUSTER
	$('#shipping').change(function() {
  		type = $(this).val();
		if(type=='Standard'){
			delivery_cost = '3.95';	
		} else {
			delivery_cost = '0.00';
		}
		$.ajax({
					type: "POST",
					url: "cart_process.php",
					data: "process=update_delivery&option="+type,
					success: function(totals){
						$('#delivery_cost').html('&pound;'+delivery_cost);
					}
			});
	});

	/*QUANTITIES */
	$('a.add_item').click(function(event) {
		event.preventDefault();
		quantity = $('#item_quantity').val();
		//item_id=$('#item_id').html();
		
		the_item_id=document.getElementById('item_id').innerHTML;
		//option = $('input:radio[name=item_options]:checked').val();
		//var allVals = [];
		//$('ul#item_options :checked').each(function() {
       	//	allVals.push($(this).val());
     	//});
		//option = allVals.toString();
		option = '';
		thego=1;
		$('#error_alert').hide();
		$('.item_option').each(function(index) {
			if($(this).val()==''){
				$('#error_alert').slideDown();
				thego=0;
			}
    		option += $(this).val()+' | ';
			
  		});
		option=option.substring(0, option.length-2);
		if(thego==1){
			$.ajax({
					type: "POST",
					url: "cart_process.php",
					data: "item_id="+the_item_id+"&process=add&option="+option+"&quantity="+quantity,
					success: function(totals){
						$('#add_alert').slideDown();
					}
			});
		}
	});
		
	$('a.change_quantity').click(function(event) {
		event.preventDefault();
  		type=$(this).attr('alt');
		prod_id=$(this).attr('rel');
		array_id=$('#array_id_'+prod_id).html();
		current_quantity = parseInt($('#quantity_'+prod_id).html());
		item_price = parseFloat($('#price_'+prod_id).html());
		
		
			if(type=='plus'){
				new_quantity = current_quantity+1;
			} else if(type=='minus'){
				new_quantity = current_quantity-1;	
			}
			if(new_quantity<=0){
				new_total = 'Item will be removed on update';
				new_quantity=0;
			} else {
				new_total = item_price*new_quantity	;
				new_total = new_total.toFixed(2);
				new_total = '&pound;'+new_total;
			}
			$('#total_'+prod_id).html(new_total);
			$('#quantity_'+prod_id).html(new_quantity);
			$.ajax({
   				type: "POST",
   				url: "cart_process.php",
   				data: "array_id="+array_id+"&process=update&quantity="+new_quantity,
   				success: function(totals){
     				new_totals = totals.split("|");
					$('#sub_total').html(new_totals[0]);
					$('#grand_total').html(new_totals[1]);
   				}
 			});
 
			
		
	});
	
	
	
	/*SIGN-IN BUTTON*/
	$('#btn_signin').click(function(){$('#pods_signin div').slideToggle('fast');});
	/*SIGN-IN BUTTON*/
	function addMega(){$(this).children('div').show();}
    function removeMega(){$(this).children('div').hide();}
    var megaConfig = {interval: 0,sensitivity: 2,over: addMega,timeout: 0,out: removeMega};
    $("li.megadouble").hoverIntent(megaConfig);
	$("li.megadoubleleft").hoverIntent(megaConfig);
	$("li.megasingle").hoverIntent(megaConfig);
	$("li.megagallery").hoverIntent(megaConfig);
	
	
	
	/*SEARCH LIST*/
	$('#searchterm').keyup(function(){
		var keywords = $('#searchterm').val();
		if(keywords.length>=3){
			$('#search div').show();
			var resultsdata=null;
			$.get(glo_pagepath+"assets/includes/content/search_results.php?keywords="+keywords, function(data){
				$('#search div').html(data);		
			});							 
				$(document).click(function(){								 
					 if(currentFocus==''){//clicked elsewhere on screen so don't show
						$('#search div').html('');
						$('#search div').hide();
					 }
				});
		}//end if greater than 3
	});
	var currentFocus = null; 
	$('#search div').mouseover( function() {  	currentFocus = 'searchresults';
	}).mouseout( function() {	currentFocus = '';}); 
	
	
	
	
});

