$(document).ready(function() {
	var currentYear = (new Date).getFullYear();
	var rangeYear = (currentYear-100) + ':' + (currentYear);
	
	$("#dateOfBirth").datepicker({dateFormat:'dd/mm/yy', changeYear:true, changeMonth:true, yearRange:rangeYear});
	/* execute the page visibility	*/
	$('.content').css({visibility:"visible"});
	
	
	/************************
	MENU HOVER
	************************/
	
	$("ul#nav li").hover(
		function(){
			$(this).find('div.sub').show();
			Cufon.refresh();
		},
		function(){
			$(this).find('div.sub').hide();
			Cufon.refresh();
		}
	);
	
	$('.searchtext, .input-text, .listname, .locationname').each(function() {
		var default_value = this.value;
		$(this).focus(function(){
			if(this.value == default_value) {
				this.value = '';
			}
		});
		$(this).blur(function(){
			if(this.value == '') {
				this.value = default_value;
			}
		});
	});
	
	
	/************************
	SIDEBAR CHECKED STATE (This is for demonstration purposes only)
	************************/

/*	
	$(".sidebar ul li a, .subscribepopup ul li a").click(function () { 
    	$(this).toggleClass("checked");
		return false;
    });
*/	
	
	$('.cart-btn').hover(
	function(){
		$(this).stop().animate({"width": 130}, 300, "linear", function(){$(this).find('span').fadeIn("fast");});
	},
	function(){
		$(this).find('span').hide();
		$(this).stop().animate({"width": 23}, 300, "linear");
	});
	
	
	$('.add-to-list').hover(
	function(){
		$(this).parent().find('div.add-item-box').fadeIn(200);
		$(this).parent().find('div.add-item-box').hover(
			function(){
				//do nothing
			}, 
			function(){
				$(this).parent().find('div.add-item-box').fadeOut(200);
			}
		);
	},
	function(){
		//do nothing
	});
	
	$(".add-btn").click(function () { 
		var productid = $(this).parent().find('.productid').val();
		var quantity = $(this).parent().find('.quantity').val();
		
		// Send values to server side script useing ajax
		
    });
	
	$(".select-colour").click(function () {
		$(this).parent().find('.colour-inner').fadeIn(200);
		$(this).parent().find('.colour-inner a').click(function () {
			$(".select-colour img").attr('src', $(this).find('img').attr('src')).fadeIn(250);
			$('.colour-inner').fadeOut(200);
			return false;
		});
		return false;
    });
	
	$(".control-plus").click(function () {
		var amount = parseInt($(this).parent().find('.quantity').val());
		$(this).parent().find('.quantity').val(amount + 1);
		return false;
    });
	
	$(".add-to-basket-btn").click(function () {
		if($("select.size").val() == ""){
			$('.vtooltip').fadeIn(250);
		}
    });
	
	$(".control-minus").click(function () {
		var amount = parseInt($(this).parent().find('.quantity').val());
		if(amount != 1){
			$(this).parent().find('.quantity').val(amount - 1);
		}
		return false;
    });
	
	
	$(".createlist-link").click(function () {
		$(this).parent().parent().parent().find('div.add-item-box').fadeOut(200);
		$(this).parent().parent().parent().find('div.create-list-box').fadeIn(200);
		return false;
    });
	
	
	$('.footercol .submitbtn').click(function(){
		$(this).parent().find('.subscribepopup').fadeIn(200);
	});
	
	$('.thumb-carousel ul li a').click(
		function() {
			$('.thumb-carousel ul li a img').removeClass('border');
			$(this).find('img').addClass('border');
			var currentBigImage = $('.bigpic img').attr('src');
			var newBigImage = $(this).attr('href');
			var currentThumbSrc = $(this).find('img').attr('src');
			switchImage(newBigImage, currentBigImage, currentThumbSrc);
			return false;
		}
	);
	
	$('.ratereview-btn').click(
		function () {
			$(this).toggleClass("minus");
			return false;
		}
	);
	
	$( ".tabs" ).tabs();
});
			  
resetPassword = function(pag){
	$('#reset_button_'+pag).hide();
	document.getElementById("button_"+pag).disabled=true;
	$('#password_hint_msg_'+pag).text('');
	$('#loginGif_'+pag).show();
	var text = (document.getElementById('loginInput_'+pag));
	if(text && text.value==''){
		$('#loginGif_'+pag).hide();
		$('#password_hint_msg_'+pag).text('Please enter your username to proceed.');
		$('#reset_button_'+pag).hide();
		document.getElementById("button_"+pag).disabled=true;
	}else{
		$.ajax({ 
			url: '${originatingRequest.contextPath}/ajax/passwordHint.jsp',
			type: "POST",
 					data: "username="+text.value,
 					success: function(msg){
 						$('#loginGif_'+pag).hide();
 						if(msg.indexOf('invalid_username')!=-1){
 							$('#password_hint_msg_'+pag).text('The username is not valid.');
			  		$('#reset_button_'+pag).hide();
			  		document.getElementById("button_"+pag).disabled=true;
 						}else{
			      $('#password_hint_msg_'+pag).text(msg);
				  $('#reset_button_'+pag).show();
				  document.getElementById("button_"+pag).disabled=false;
			  	}
		    }
		 })
	}
	$('#hint_'+pag).show();
}

hidePopupPassword = function(pag){
	document.getElementById('button_'+pag).disabled=true;
	$('#hint_'+pag).hide();
}

switchImage = function(imageHref, currentBigImage, currentThumbSrc) {
	var theBigImage = $('.bigpic img');
	if (imageHref != currentBigImage) {
		theBigImage.fadeOut(250, function(){
			theBigImage.attr('src', imageHref).fadeIn(250);
		});
	}
}

/************************
Text Replacement
************************/

Cufon.replace('#nav li a.mainlink, ul#nav li h2', { fontFamily: 'wfutura', hover: true, fontSize: '11.7px' });

/************************
productId: product Id
newListName: input name of new list (in the popup)
dropdownId: list dropdown id 
quantity: item quantity to add
cboxCloseId: div id of popup close button
divListDropdown: div container list dropdown
skuId: skuId
************************/

	function submitCreateList2Ajax(productId, newListName, dropdownId, quantity, cboxCloseId, divListDropdown, skuId){
			
			var nListName = document.getElementById(newListName).value;
            var selListType	= $('#'+dropdownId).val();
           
            /*$('#'+divListDropdown).css({'visibility':'hidden','overflow':'hidden','height':'17px'});*/
            $('#'+divListDropdown).hide();
			
			$('#div_spinner_'+productId).show();
			var skuId = skuId;
			$.ajax({
                     url: '/store/ajax/shoppingList.jsp',
                     data: "listName="+nListName+"&productId="+productId+"&skuId="+skuId+"&selListType="+selListType+"&quantity="+quantity,
                     dataType: 'json',
                     async:true,
                     success: function(data) {
                     	var errorMess = data.errorMessage;
                     	if(errorMess!=''){
                     		/*open popup error*/
                     		document.getElementById("messageInPopup").innerHTML= errorMess;
                     		$('#showPopupMsg').click();
                     	} else {
                     		$('#'+cboxCloseId).click();
                     		/*display successfully message and update dropdown*/
                     		$('#shopListMessage_'+productId).show();
                     		$('#shopListMessage_'+productId).css({'height':'15px'});
                     		document.getElementById("shopListMessage_"+productId).innerHTML= "Successfully added to list";
                     		
                     		if(selListType=='createList'){
                     			$(".items_drop option[value='createList']").remove(); 
                     			$('.items_drop').append('<option value="' + data.listaId + '">' + data.listaName + '</option>');
                     			$('.items_drop').val(data.listaId);
                     			$('.items_drop').next().find(':first-child').html(data.listaName);
                     			$('.items_drop').append('<option value="createList">Create Shopping List</option>');
                     		} else {
                     			$('.items_drop').val(data.listaId);
                     			$('.items_drop').next().find(':first-child').html(data.listaName);
                     		}
                     	}
                     	
						$('#'+newListName).val('');
                     },
                     error: function() { 
                      	alert('error');
                      $('#div_spinner_'+productId).hide();
                     },
                     complete: function() {
                   	 /*alert('complete'); */
                   		$('#div_spinner_'+productId).hide();
                     },
                     timeout: 20000
                   });
		}

function continueClickAndCollectOrder_click(){
	$('#cboxClose').click();
}

function switchToDeliveryOrder_click(){
	$('#cboxClose').click();
	$('#navChangeSuburb').click()

	$('input:radio[name=delivery]:nth(0)').attr('checked',true);
	setDeliveryMyOrderCS();
}

function checkFoodProduct(isFoodProduct, currentProductId){								
	var allFood = $('#allFoodInCart').val();
	var isClickAndCollectOrder = $('#clickAndCollectOrderNav').val();
	
		
	/*							
	alert("isFoodProduct:"+isFoodProduct+'-- allFoodInCart: '+ allFood+'-- isClickAndCollectOrder: '+isClickAndCollectOrder);
	alert('Order clickAndCollect '+ $('#clickAndCollectOrderNav').val());
	*/
	
	if(isClickAndCollectOrder != 'true'){
		if(allFood == 'true' && isFoodProduct != 'true'){
			$('#allFoodInCart').val('false')
		}
		return true;
	}else{								
		if(isFoodProduct == 'true' && allFood == 'true'){
			/*alert("click su add di destra: idBtnAddBasket_"+currentProductId)*/
			return true;
		}
		
		if(isFoodProduct != 'true' && allFood == 'true'){
			$('#showPopupAlertClickAndCollect').click();
			return false;
		}
		
		return true;
	}	
}
