// HappyCanyonVineyard.com JavaScript - Created by Creori, Inc.
showPane = function(pane, which) {
	document.getElementById('title_BRAND').className = 'wineTitle';
	document.getElementById('title_TENGOAL').className = 'wineTitle';
	document.getElementById('title_CHUKKER').className = 'wineTitle';
	document.getElementById('title_PIOCHO').className = 'wineTitle';
	
	document.getElementById('notes_BRAND').style.display = 'none';
	document.getElementById('notes_TENGOAL').style.display = 'none';
	document.getElementById('notes_CHUKKER').style.display = 'none';
	document.getElementById('notes_PIOCHO').style.display = 'none';
	
	document.getElementById('buy_BRAND').style.display = 'none';
	document.getElementById('buy_TENGOAL').style.display = 'none';
	document.getElementById('buy_CHUKKER').style.display = 'none';
	document.getElementById('buy_PIOCHO').style.display = 'none';
	
	document.getElementById('buy_BRAND_link').className = 'buyNowLink';
	document.getElementById('buy_TENGOAL_link').className = 'buyNowLink';
	document.getElementById('buy_CHUKKER_link').className = 'buyNowLink';
	document.getElementById('buy_PIOCHO_link').className = 'buyNowLink';
	
	if (pane == 'notes')
		document.getElementById('title_' + which).className = 'wineTitle notes';
	else if (pane == 'buy') {
		document.getElementById('title_' + which).className = 'wineTitle buy';
		document.getElementById('buy_' + which + '_link').className = 'buyNowLink buy';
	}

	document.getElementById(pane + '_' + which).style.display = 'block';
		
	/*if (which == 'BRAND') {
		document.getElementById('title_' + which).class = 'wineTitle on';
	}
	else if (which == 'TENGOAL') {
		
	}
	else if (which == 'CHUKKER') {
		
	}
	else (which == 'PIOCHO') {
		
	}*/
	return true;
}


buyHover = function(state, which) {
	//console.log(document.getElementById('title_' + which).style);
	if (state == 'over') {
		//document.getElementById('title_' + which).style.background = 'url(../images/content/title_BRAND.png) no-repeat left top';
		document.getElementById('title_' + which).style.backgroundPosition = 'left top';
	}
	else {
		if (document.getElementById('title_' + which).className == 'wineTitle') {
			//console.log(document.getElementById('title_' + which).className);
			document.getElementById('title_' + which).style.backgroundPosition = 'left -26px';
		}
		else {
			//console.log(document.getElementById('title_' + which).className);
			document.getElementById('title_' + which).style.backgroundPosition = 'left top';
		}
	}
	//console.log(document.getElementById('title_' + which).style);
	return true;
}



// ITEM DETAIL PHOTO
popUpWin = "nowin";
popWin = function(name, file, width, height, status, resizable, scrollable) {
	var h=height;
	var w=width;
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	var winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',status='+status+',resizable='+resizable+',scrollbars='+scrollable;
	popUpWin = window.open( file , name, winprops );
	if (popUpWin)
		popUpWin.focus();
	return popUpWin;
}

closePopUp = function() {
	if( popUpWin != "nowin" ) {
		if( !popUpWin.closed ) {
			popUpWin.close();
		}
	}
}

showPasswordPopup = function(url) {
	popWin('passwordPopup', url, 380, 400, 0, 0, 0);
}
showBigImage = function(elem) {
	var imgName = $(elem).down('img').src.replace(/size2/,"size1");
	popWin('bigimage', '/images/showImg.cfm?imgName=' + imgName, 500, 650, 0, 0, 0);
}
showBigImages = function(images, selected, itemName, imagePath) {
	//this.up().down('img').src.split('/').pop();
	if (typeof imagePath != 'undefined')
		bigImages = popWin('bigImages', '/images/showImg.cfm?images=' + images.toString() + '&selected=' + selected + '&itemName=' + itemName + '&imagePath=' + imagePath, 500, 650, 0, 0, 0);
	else
		bigImages = popWin('bigImages', '/images/showImg.cfm?images=' + images.toString() + '&selected=' + selected + '&itemName=' + itemName, 500, 650, 0, 0, 0);
	if(bigImages)
		bigImages.focus();
}

/* USER ACCOUNT FUNCTIONS */

setAddressDefault = function (input, url) {
	var userGUID = input.getAttribute("userGUID");
	var addressGUID = input.getAttribute("addressGUID");
	//console.log(addressGUID, userGUID);
	if (!userGUID.length || !addressGUID.length)
		return false;

	new Ajax.Request(url, {
		method: 'get',
		parameters: { userGUID: userGUID, addressGUID: addressGUID },
		onSuccess: function(transport) {
			if (transport.responseText == 'true') {
				if (url.indexOf('illing') > 0) {
					new Effect.Highlight('defaultBillingLabel_' + addressGUID);
					$('statusBar').hide().update('Default billing address set.');
				}
				else if (url.indexOf('hipping') > 0) {
					new Effect.Highlight('defaultShippingLabel_' + addressGUID);
					$('statusBar').hide().update('Default shipping address set.');
				}
			}
			else {
				if (url.indexOf('illing') > 0)
					$('defaultBilling_' + addressGUID).checked = false;
				else if (url.indexOf('hipping') > 0)
					$('defaultShipping_' + addressGUID).checked = false;
				$('statusBar').hide().update("There was an error saving your selection.");
			}
			new Effect.Appear($('statusBar'), {duration: 0.5});//, afterFinish:hideStatus, from: 0.0, to: 0.7, afterFinish:false 

		},
		onFailure: function (transport) {
			//$('statusBar').update(transport.responseText);
			if (url.indexOf('illing') > 0)
				$('defaultBilling_' + addressGUID).checked = false;
			else if (url.indexOf('hipping') > 0)
				$('defaultShipping_' + addressGUID).checked = false;
		}
	});
	return true;
}

hideStatus = function () {
	new Effect.Fade($('statusBar'), {duration: 2});
}

setDefaultBillingAddress = function (input) {
	var url = "/?event=user.address.setDefaultBilling";
	setAddressDefault(input, url);
}

setDefaultShippingAddress = function (input) {
	var url = "/?event=user.address.setDefaultShipping";
	setAddressDefault(input, url);	
}

// For user account page.
showOrder = function (orderGUID) {
	//$('prevOrderMin_' + effect.element.id.split('_')[1]).show();
	$('prevOrderMin_' + orderGUID).hide();
	Effect.SlideDown('prevOrderFull_' + orderGUID, {duration:1});
	return;
}
// For user account page.
hideOrder = function (orderGUID) {
	Effect.SlideUp('prevOrderFull_' + orderGUID, {afterFinish:showTiny, duration:0.5});
	return;
}
// For user account page.
showTiny = function (effect) {
	$('prevOrderMin_' + effect.element.id.split('_')[1]).show();
	return;
}

// footer newsletter signup form
updateResponseDiv = function(theForm, theAction) {
	new Ajax.Request(theAction, {method: 'post', parameters: $(theForm).serialize(true),
		onSuccess: function(transport){ 
			if(transport.responseText == '"subscriptionSuccess"'){
				//$('errResponse').setStyle({display:'none'});
				$('response').innerHTML = "<h4 class='successResponse'>Thanks for Subscribing!&nbsp;</h4>";
				new Effect.Fade('newsletter', {duration: 0.5, queue: 'front' });
				new Effect.Appear('response', {duration: 0.5, queue: 'end' });
			}
			else if(transport.responseText == '"emailError"'){
				$('response').hide().innerHTML = "Please enter a valid Email Address: ";
				new Effect.Appear('response', {duration: 0.25});
			}
			else if(transport.responseText == '"mailinglistError"' || transport.responseText == '"subscriptionError"'){
				$('response').hide().innerHTML = "Sorry, We are unable to take your subscription at this time.";
				new Effect.Appear('response', {duration: 0.25});
			}

		}
	});
	return false;
}

checkText = function(formField) {
	var formFieldValue = formField.value;
	var formFieldDefault = formField.title;
	if (formFieldValue == formFieldDefault) {
		$(formField).setStyle({color: '#000'}).value = "";
	}
	else if(formFieldValue == "") {
		$(formField).setStyle({color: '#CCC'}).value = formFieldDefault;
	}
}

