/********************** ChopShop General Functions ******************/

var newsArchiveOpen = false;
var artistNewsArchiveOpen = false;
var homeNewsArchiveOffset = 254;
var artistNewsArchiveOffset = 254;

jQuery(document).ready(function() {	
	
	jQuery("*").click(function () {
		jQuery("#artistList").hide()
		jQuery("#shopList").hide()
	});

	//~ Artist Flyout
	jQuery("#navArtistLink").mouseover(function () {
		jQuery("#artistList").slideDown("medium")
	});
	jQuery("#artistList").mousemove(function () {
		jQuery("#artistList").show()
	});
	jQuery("#artistList,#navArtistLink").mouseout(function () {
		jQuery("#artistList").hide()
	});

	//~ Shop Flyout
	jQuery("#navShopLink").mouseover(function () {
		jQuery("#shopList").slideDown("medium")
	});
	jQuery("#shopList").mousemove(function () {
		jQuery("#shopList").show()
	});
	jQuery("#shopList,#navShopLink").mouseout(function () {
		jQuery("#shopList").hide()
	});

	//~ News Archive Flyout
		
	jQuery("*").click(function () {
		if (newsArchiveOpen)
			jQuery("#newsArchive").hide()
		if (artistNewsArchiveOpen)
			jQuery("#artistNewsArchive").hide()
	});
	
	//~ Home News Archive Flyout
	jQuery("#newsArchiveLink").click(function () {

		//~ Position Home Page NewsArchive
		newsArchivePosition = jQuery("#newsArchiveLink").position()
		archiveListTop = homeNewsArchiveOffset + newsArchivePosition.top
		jQuery("#newsArchive").css('top',archiveListTop)

		if (!newsArchiveOpen) {
			jQuery("#newsArchive").slideDown("medium")
			newsArchiveOpen = true;
		}
		else {
			newsArchiveOpen = false;
		}
	});
	
	//~ Artist News Archive Flyout
	jQuery("#artistNewsArchiveLink").click(function () {

		//~ Position Artist Page NewsArchive
		artistNewsArchivePosition = jQuery("#artistNewsArchiveLink").position()
		artistArchiveListTop = artistNewsArchiveOffset + artistNewsArchivePosition.top
		jQuery("#artistNewsArchive").css('top',artistArchiveListTop)

		if (!artistNewsArchiveOpen) {
			jQuery("#artistNewsArchive").slideDown("medium")
			artistNewsArchiveOpen = true;
		}
		else {
			artistNewsArchiveOpen = false;
		}
	});

	//~ Shop Page Alert Box
	jQuery("#alertBoxCloseButton").click(function () {
		jQuery("#alertBox").hide();
	});

});

function validate() {
	var nameField = document.getElementById("name").value;
	var lastNameField = document.getElementById("lastName").value;
	var emailField = document.getElementById("email").value;
	var postalField = document.getElementById("postalcode").value;
	var countryField = document.getElementById("country").value;
	
	var nameError = document.getElementById("nameError");
	var lastNameError = document.getElementById("lastNameError");
	var emailError = document.getElementById("emailError");
	var postalError = document.getElementById("postalError");
	var countryError = document.getElementById("countryError");
	
	var validForm = true;
	
	if (nameField == "") {
		nameError.style.visibility = "visible";
		validForm = false;
	}
	else {
		nameError.style.visibility = "hidden";			
	}
	if (lastNameField == "") {
		lastNameError.style.visibility = "visible";
		validForm = false;
	}
	else {
		lastNameError.style.visibility = "hidden";			
	}
	if (emailField == "") {
		emailError.innerHTML = "Please enter your email address";
		emailError.style.visibility = "visible";
		validForm = false;
	}
	else if (!emailField.match(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/)) {
		emailError.innerHTML = "Please enter a valid email address";
		emailError.style.visibility = "visible";
		validForm = false;
	}
	else {
		emailError.style.visibility = "hidden";			
	}
	if (postalField == "") {
		postalError.style.visibility = "visible";
		validForm = false;
	}
	else {
		postalError.style.visibility = "hidden";			
	}	
	if (countryField == "") {
		countryError.style.visibility = "visible";
		validForm = false;
	}
	else {
		countryError.style.visibility = "hidden";			
	}
	
	if(validForm) {
		document.signup.submit();
		return false;
	}
	return true;
}

function addToCart(formName, productName, isApparel, errorId) {
	var cartForm = document.getElementById(formName);
	var validForm = true;
	
	if (isApparel.toLowerCase() == 'y') {
		if (cartForm.apparelSize.value != "") {
			cartForm.item_name.value = productName + " " + cartForm.apparelSize.value;
		}
		else {
			jQuery(errorId).show();
			validForm = false;
		}
	}
	else {
		cartForm.item_name.value = productName;
	}
	if (validForm) {
		jQuery('.apparelError').hide();
		cartForm.submit();
	}
}

function loadPlayer(obj) {
	Event.observe(window,'load',function(){
		DP.TRACK.prevPlayers.push(obj); 
	});
}
