// Requires JQuery
// By RBMcG
// Call Init function for each widget after document load to instantiate on page

/* Color Chooser */
function initProductToggler(){
	var $kids = $(".colorChooser").children();
	$kids.each(function (i) {
		setToggler(this);
	});
}
function setToggler(elem){
	var imageSrc = "images/products/" + $(elem).attr("class") + "-large-detail.jpg";
	$(elem).bind("click", function(){
    	$("#toggledImage").attr("src",imageSrc);
    });

}
/* Modal Window */
/* Button ID = "MySelectorName" Window ID = "MySelectorName-Window" */
function initModalWindow(){
	$(".modalWindow").bind("click", function(){
		$(".modal-screen").show();									 
		$("#" + this.id + "-window").fadeIn("slow");
		initModalClose();
    });
}
function initModalClose(){
	$(".modal-close").bind("click", function(){
    	$(".modal-screen").hide();
		$(".modal-dialogue").fadeOut("slow");
    });
}
/* End Modal Window */

/* Expand/Collapse 
 * Shows 3 li elements in partition 
 */
function initExpander(){
	collapseAll();
	$(".expander").bind("click", function(){
    	if($(this).attr("class") == "expander"){
			expandIt(this);
		}else{
			collapseIt(this);
		}
    });
}
function expandIt(elem){
	var $list = $(elem).siblings("ul");
	$list.children().fadeIn();
	setLessButton(elem);
	toggleClass(elem);
}
function collapseIt(elem){
	var $list = $(elem).siblings("ul");
	var $kids = $list.children();
	$kids.each(function (i) {
		if(i > 2){
			$(this).fadeOut();
		}
	});
	setMoreButton(elem);
	toggleClass(elem);
}
function toggleClass(elem){
	if($(elem).attr("class") == "expander"){
		$(elem).attr("class","collapser");
	}else{
		$(elem).attr("class","expander");
	}
}
function setMoreButton(elem){
	$(elem).attr("src","../images/interface/button-more-info.gif");
	$(elem).attr("alt","More Information");
}
function setLessButton(elem){
	$(elem).attr("src","../images/interface/button-less-info.gif"); 
	$(elem).attr("alt","Less Information"); 
}
function collapseAll(){
	var $lists = $(".partition ul");
	$lists.each(function () {
		var $kids = $(this).children();
		var $button = $(this).siblings(".expander");
		if($kids.length < 4){
			$button.hide();
		}
		$kids.each(function (i) {
			if(i > 2){
				$(this).hide();
			}
		});
	});
}
/* END Expand/Collapse */
/* PRODUCT PAGE ACCORDIAN */
function initAccordian(){
	$(".btn-expand").bind("click", function(){
		var $parent = $(this).parent();
		var $grandparent = $parent.parent();
		if($grandparent.hasClass("season")){
			down();
		}else{
			up();
		}
});

}
function classToggle(){
	$(".season").toggleClass("inactive");
	$(".media").toggleClass("inactive");
}
function up() {
	$(".media").toggleClass("inactive");
	$(".season").animate({ 
		height: "52px"
		}, 1000, function(){$(".season").toggleClass("inactive");} );
}
function down() {
	$(".season").toggleClass("inactive");
	$(".season").animate({ 
		height: "272px"
		}, 1000, function(){$(".media").toggleClass("inactive");} );
}
/* END PRODUCT PAGE ACCORDIAN*/

function copyToElementInnerHtmlAndCloseAjaxLoader(response, arg) {
	copyToElementInnerHtml(response, arg);
	$("#ajax-loader").hide();
}

/*FIND IN STORE POPUP*/
function populateFindInStore(context, product_id, colorway, title) {
	$("#ajax-loader").show();		
	var url = context + '/product/modal_find_in_store.jsp';
	var url_param = 'product_id=' + product_id + '&colorway=' + colorway + '&title=' + escape(title) + '&formdisp=1';
	asyncGet(url, 'copyToElementInnerHtmlAndCloseAjaxLoader', {'elementId': 'modalFindInStore-filler'}, url_param);
}

/* Returns the list of retailers the moment the "Buy Online" button is pressed */
function populateBuyItOnline(context, product_id){
	asyncGet(context + '/product/modal_buy_online.jsp', 'copyToElementInnerHtmlAndCloseAjaxLoader', {'elementId': 'modalBuyOnline-filler'}, 'product_id=' + product_id);
}


function showElementInnerHtmlAndCloseAjaxLoader(response, arg) {
	if(response.indexOf("true") > 0){
		/* Show the Buy Online button since the jsp returned the keyword "true" */
		document.getElementById(arg.elementId).style.display = '';
	}else{
		/* Let the Buy Online button remain hidden */
	}
}
/* Checks to see if a product has any retailers, if so, show the button */
function showBuyItOnlineButton(context, product_id){
	asyncGet(context + '/product/modal_buy_online_button.jsp', 'showElementInnerHtmlAndCloseAjaxLoader', {'elementId': 'modalBuyOnlineLi'}, 'product_id=' + product_id);
}


		/*
		 * Show My Account Add Shipping Info popUp
		 */
		function showMyAccountAddShippingInfoPopUp(context) {
		
			$("#ajax-loader").show();		
			var url = context + '/templates/ajax/shipping_address_add.jsp';
			asyncGet(url, 'showAjaxModalDialog', {'elementId': 'addAddress', 'context' : context});
			
		}

		function processMyAccountAddShippingInfoPopUp(response, arg) {
			if(response == 'success') {
				window.location = arg.context+'/agent/view_credit_card';
			} else {
				checkAndCopy(response, {'elementId': 'addAddress', 'context' : arg.context});
			}
		}
		
		/*
		 * Show My Account Edit Shipping Info popUp
		 */
		function showMyAccountEditShippingInfoPopUp(context, address_id_param) {
		
			$("#ajax-loader").show();		
			var url = context + '/templates/ajax/shipping_address_edit.jsp';
			asyncGet(url, 'showAjaxModalDialog', {'elementId': 'editAddress', 'context' : context}, address_id_param);
			
		}
		
		function processMyAccountEditShippingInfoPopUp(response, arg) {
			if(response == 'success') {
				window.location = arg.context+'/agent/view_credit_card';
			} else {
				checkAndCopy(response, {'elementId': 'editAddress', 'context' : arg.context});
			}
		}
		
		/*
		 * Show My Account Add CreditCard Info popUp
		 */
		function showMyAccountAddCreditCardPopUp(context) {
		
			$("#ajax-loader").show();		
			var url = context + '/templates/ajax/credit_card_add.jsp';
			asyncGet(url, 'showAjaxModalDialog', {'elementId': 'addCard', 'context' : context}, 'context='+context);
			
		}

		function processMyAccountAddCreditCardPopUp(response, arg) {
			if(response == 'success') {
				window.location = arg.context+'/agent/view_credit_card';
			} else {
				checkAndCopy(response, {'elementId': 'addCard', 'context' : arg.context});
			}
		}
	
		/*
		 * Show My Account Edit Credit Card popUp
		 */
		function showMyAccountEditCreditCardPopUp(context, rel_id_param) {
		
			$("#ajax-loader").show();		
			var url = context + '/templates/ajax/credit_card_edit.jsp';
			asyncGet(url, 'showAjaxModalDialog', {'elementId': 'editCard', 'context' : context}, rel_id_param);
			
		}
		
		function processMyAccountEditCreditCardPopUp(response, arg) {
			if(response == 'success') {
				window.location = arg.context+'/agent/view_credit_card';
			} else {
				checkAndCopy(response, {'elementId': 'editCard', 'context' : arg.context});
			}
		}
		
		function checkAndCopy(response, arg){
			if (response.indexOf('/servlet/agentlogin') > 0){
				// means u lose your session; got the login screan; then redirect to login;
				window.location = arg.context + '/agent/login';
            }
            else{
            	copyToElementInnerHtmlAndCloseAjaxLoader(response, arg);
            	focus_first('#' + arg.elementId);
            }
            	
		}
		
function showModalDialog(response, arg){
    $('#'+arg.elementId).modal();
    /* need to dynamicly adjust the height of gray background or else firefox would not fully cover the screen with it */
    var pixelHeight = document.body.offsetHeight;
    $('#modal-screen').height(pixelHeight + "px");

}
	
//this method is for jsp's using ajax 2 was created
//because of the css issues 
function showAjaxModalDialog(response, arg){
	
    $('#'+arg.elementId).html(response);
    $('#'+arg.elementId).modal();
    
/* need to dynamicly adjust the height of gray background or else firefox would not fully cover the screen with it */
var pixelHeight = document.body.offsetHeight;
$('#modal-screen').height(pixelHeight + "px");

}


		
