
$(document).ready(function() {
	
	//submit form by pressing enter
	$("form#subscription-form").live("submit",function(){
		submitEmailSignUp();
		return false;
	});
	
	$(".defaultText").each( function (index, element) {
		defaultTextJs.doFocusOut($(element));
	});
	$(".defaultText").livequery( function () {
		defaultTextJs.doFocusIn(this);
		defaultTextJs.doFocusOut(this);
	});
	$(".defaultText").live("focusin click", function() {
		defaultTextJs.doFocusIn(this);
	});
	$(".defaultText").live("focusout change", function() {
		defaultTextJs.doFocusOut(this);
	});
	 $("#header .lv2").expire();
		
});

// Windows load (initializes all CC features eg.  tabs)
$(window).load(function(){  

	init_all();
	do_anchor();
	
	var urlMap = getUrlVars();
	var trail = urlMap['trail'];
	var url = "/cc/include/navBar.jsp?trail="+trail;
	$("div#includeNavBar").load(url, function(){

		if($("#nav .category").length){
			navMenuStyle($("#nav .category li"), function(){
                                    setContentPadding();
                        });
		}
		
		
		// if we are on a category page, we load the specific images corresponding to the selected category or subcategory
		if (location.pathname.indexOf("category.jsp")!=-1){
			var selectedCategory ;
			var subCatName = $('#subCatName').attr('catId');
			var selectedSubCategory = isBlank(subCatName) ? "" : subCatName.replace(" ", "+");

			if (selectedSubCategory != 'Sub+Category'){
				selectedCategory = selectedSubCategory;
			} else {
				var catName = $('#catName').attr('catId');
				selectedCategory = isBlank(catName) ? "" : (catName).replace(" ", "+");
			}

			var catUrl = "/cc/landing/include/categorySpecificImages.jsp?selectedCategory="+selectedCategory;
			$("div#categorySpecificImages").load(catUrl,function(){
                               

                        });
			
			
		// if we are on a color page, we load the specific images corresponding to the selected color
		} else if (location.pathname.indexOf("newColour.jsp")!=-1) {
			var selectedColor = $(".colorSelector .selected").attr('class').replace(" selected", "");
//			alert(selectedColor)
			var catUrl = "/cc/landing/include/colorSpecificImages.jsp?selectedColor="+selectedColor;
			$("div#colorSpecificImages").load(catUrl,function(){});
		}else {
			
		}
		
		
	});
	
	
});


//footer email signup
function footerEmailSignup() {

	globalDialogObject.dialogTitle = 'Email Sign-Up';
	globalDialogObject.dialogUrl = "/cc/dialog/emailSignUp.jsp";
	globalDialogObject.dialogClassName = "forgotPasswordDialog";
	globalDialogObject.dialogBtns = [
		{text:"Cancel", click:function(){globalDialogObject.closeGlobalDialog()}},
		{text:"Submit", click:function(){submitEmailSignUp();}}
	];
	globalDialogObject.popupGlobalDialog(function(){
		$("#dialog").cc_dialog( "option", "width", 400 );
	});
            $('.ui-button:first').addClass('tertiary');
}


function submitEmailSignUp() {
	var $form = $("#subscription-form"); // form id 
	
	$form.jquerySubmitForm($form,$form, function(){
		globalDialogObject.closeGlobalDialog();

		globalDialogObject.dialogTitle = 'Email Sign-Up';
		globalDialogObject.dialogUrl = "/cc/dialog/successfulSignUpDialog.jsp";
		globalDialogObject.dialogClassName = "";
		globalDialogObject.dialogBtns = "";
		/*
		globalDialogObject.dialogBtns = [
			{text:"OK", click:function(){globalDialogObject.closeGlobalDialog()}}
		];
		*/
		globalDialogObject.popupGlobalDialog(function(){
			$("#dialog").cc_dialog( "option", "width", 350 );
		});
	},  function(){
		
	}, 
	function(){
		globalDialogObject.closeGlobalDialog();

		globalDialogObject.dialogTitle = "Sorry, there was an error...";
		globalDialogObject.dialogUrl = "/cc/dialog/emptyDialog.jsp";
		globalDialogObject.dialogClassName = "";
		globalDialogObject.dialogBtns = [
			{text:"OK", click:function(){globalDialogObject.closeGlobalDialog()}}
		];
		globalDialogObject.popupGlobalDialog(function(){
			$("#dialog").cc_dialog( "option", "width", 350 );
		});
	});
}

// Social Network Function
function socialNetworkShare(type, url, msg,productId){
	var url = encodeURIComponent(url||location.href);
	var msg = encodeURIComponent(msg||document.title);
	var pathPrefix, pathShare;
	var pageName="";
	if(productId!=null){
		s.products=productId;
		pageName="pdp: product page";
	}
	var events = "event9";
	var prop12 = "";
	switch(type){
		case "facebook":
			pathPrefix = "http://www.facebook.com/sharer.php?";
			pathShare = pathPrefix + "u=" + url + "&t=" + msg;
			prop12="share this: "+type;
			break;
		case "twitter":
			pathPrefix = "http://twitter.com/share?";
			pathShare = pathPrefix + "url=" + url + "&text=" + msg;
			prop12="share this: "+type;
			break;
		case "weibo":
			pathPrefix = "http://service.t.sina.com.cn/share/share.php?";
			pathShare = pathPrefix + "url=" + url + "&title=" + msg;
			break;
		case "renren":
			pathPrefix = "http://share.renren.com/share/buttonshare.do?";
			pathShare = pathPrefix + "link=" + url + "&title=" + msg;
			break;
	}
	 $(document).ready(function(){
		    var s=s_gi(s_account)

		    s.linkTrackEvents=events;
		    s.events='event9';
		    s.pageName=pageName;
		    s.prop12=prop12;
		    // send the information
		    s.t();

		    // clear out variables
		    s.linkTrackEvents=""
		    s.events=""
		  });
	window.open(pathShare,"_blank","toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width=640, height=480");
}



// Adjust Content Padding with Header
var useOnlyOnceFlag = false;
function setContentPadding() {
    
	$("#header .lv2").livequery(function(){
	        if($(this).length == 0){
	            return false
	        }else{
	    		if(!useOnlyOnceFlag){
		            $("#content").css("padding-top",(parseInt($("#content").css("padding-top").replace("px", "")) + 23) + 'px');
		            $("#nav .lv1").css("background-position","0px -33px");
		            useOnlyOnceFlag = true;
	    		}
	            if($("#nav .option").length){
	                    navMenuStyle($("#nav .option li"),function(){
	                            this.eq(0).addClass("shadow");
	                            this.eq(1).addClass("shadow");
	                    });
	            }

	            setContentPadding = function(){};
	        }
    });

	
}
