// JavaScript Document
var currentNavigationElement ;
var passwordFieldFocused = false;
var emailFieldFocused = false;
//$(document).ready(function(){
jQuery.noConflict();
jQuery(document).ready(function($){
	currentNavigationElement = $('#firstNavigationElement');
	// events for register input boxes when user clicks the text or focus the text box first time
	// email field focused
	$("#email").focus(function(){
		if( false == emailFieldFocused ){
			$("#email").val('');
			emailFieldFocused = true;
		}
	});
	// password field focused
	$("#password").focus(function(){
		if( false == passwordFieldFocused ){
			$("#password").val('');
			passwordFieldFocused = true;
		}
	});
	//jQuery("#zoomImage").attr("src", jQuery("#zoomImage").attr("src"));
	// show the default prduct details thru ajax
	// check the url is for some product page
	var urlParts = window.location.href.split("#");
	if( urlParts[1] != undefined ){
		showHomeAnimation();		
		getDescription('collection', '', 0 );
	}
	// magnifier effect
	$('#zoomImage').addpowerzoom();
	jQuery("#zoomImage").load(function(){
		jQuery(this).show();
		$('#zoomImage').addpowerzoom();		
	 });
	$('#zoomImage').addpowerzoom();
});
function getDescription( navigationMode, navigationElement, navigationId){
	setTimeout("getDescriptionAjax('" + navigationMode + "','" +  navigationElement + "','" + navigationId + "')", 500);
}
function getDescriptionAjax( navigationMode, navigationElement, navigationId ){
	// alert( navigationElement );
	var urlParts = window.location.href.split("#");
	//alert( urlParts );
	//	window.location.href= urlParts[0] + "#" + navigationMode + "," + navigationId;
	if( urlParts[1] != undefined ){
		navigationParts = urlParts[1].split(",");
		navigationMode = navigationParts[0];
		navigationId	= navigationParts[1];
	} else { //set collection is default 
		navigationMode = 'collection';
		navigationId = 0;
	}
	// select the menu from url
	jQuery("#collectionLink").removeClass("title");
	jQuery("#shirtsLink").removeClass("title");
	jQuery("#tiesLink").removeClass("title");
	jQuery("#"+ navigationMode + "Link").addClass("title");	
		
	var ajaxUrl = "index.php?format=raw&tpl=getdescription&navigationMode=" + navigationMode.toLowerCase() + "&id=" + navigationId;
	jQuery.ajax({
		  url		: ajaxUrl
		, type		: 'get'
		, success	: function(response){
			var result = eval( "(" + response + ")" );
			showDescription(navigationMode, navigationElement, result.product_image, result);
		}
	});
	return false;
}
// function to show the registration form 
function showRegisterForm(){
	jQuery('#registerContainerMask').animate({
		width:"0px"
	}, 2000);
}
// function to show the where to buy link or any cms like pages
function showStaticPage(navigationMode){
	var ajaxUrl = "index.php?format=raw&tpl=getpage&navigationMode=" + navigationMode.toLowerCase();
	jQuery.ajax({
		  url		: ajaxUrl
		, type		: 'get'
		, success	: function(response){
			var result = eval( "(" + response + ")" );
				// update the page content area from the result
				jQuery("#mask").animate({
					width:"700px"
					/*left:"-32%"*/
				}, {"duration":400, complete:function(){
					// update product description with navigation mode contents
					jQuery("#product_description").html( result.product_description);	
					// fix the font issue when using cuffon in ajax mode 
		Cufon.refresh();
		// cufon fixes 
					jQuery("#mask").animate({
						width:"1px"
					}, 400);	
				}});
				
		}
	});
}
// function to show the description when user clicks the product

function showDescription(navigationMode, navigationElement, productImage, result) {
	jQuery(navigationElement).addClass("title");
	if( '' != currentNavigationElement )
		jQuery(currentNavigationElement).removeClass("title");
	currentNavigationElement = navigationElement;
	// animate product description part
	jQuery("#mask").animate({
		width:"655px"
		/*left:"-32%"*/
	}, {"duration":400, complete:function(){
		// update product description with navigation mode contents
		jQuery("#navigation_designs").html(result.navigation_designs);
		jQuery("#product_description").html( result.product_description);
		jQuery("#product_description").css("visibility", "visible");
		// fix the font issue when using cuffon in ajax mode 
		Cufon.refresh();
		// cufon fixes 
			
		
		jQuery("#mask").animate({
			width:"1px"
		}, 400);	
	}});
	// animate navigation design part
	jQuery("#navigation_designs").animate({
		width:"0px"
	}, {"duration":400, complete:function(){
		// update product description with navigation mode contents
		jQuery("#navigation_designs").animate({
			/*width:"600px"*/
			width:"60%"
		}, 400);	
	}});
	// animate the image part
	jQuery("#zoomImageMask").animate({
		width:"400px"
		/*width:"30%"*/
	}, {"duration":400, complete:function(){
		//alert( productImage );
		// update the product image
		if( undefined != productImage ){ 
			if( 'ties' == navigationMode ){
				jQuery("#zoomImage").css({'margin-top':'125px', 'height': '300px'});
			} else if('collection' == navigationMode ){
				jQuery("#zoomImage").css({'margin-top':'0px','height': '550px'});
			} else {
				jQuery("#zoomImage").css({'margin-top':'75px','height': '400px'});
			}
			jQuery("#zoomImage").attr("src", "images/" + productImage);
			jQuery("#zoomImage").hide();
			jQuery("#imageloader").show();
			jQuery("#zoomImage").load(function(){
				jQuery("#zoomImage").show();
				jQuery("#imageloader").hide();				
			});
		}
		//again animate the image area to get the default width
		jQuery( "#zoomImageMask").animate({
			width:"0px"
		}, {duration:400, complete:function(){
			// set default head image for collection type product
			// update the header part for collection
			if( 'collection' == navigationMode ){
				jQuery("#headerImageArea").html("<img src=\"images/head.png\" />");
				/*jQuery("#headerImageMask").animate({
					"marginRight" : "-100px"
				}, 400);*/				
			}
			
		}});
	}});
	// update the header part for collection
	if( 'collection' == navigationMode ){
		jQuery("#headerImageArea").css("visibility", "visible");
		jQuery("#headerImageArea").html("<img src=\"images/head-turned.png\" />");
	} else {
		jQuery("#headerImageArea").css("visibility", "hidden");

			/*jQuery("#headerImageMask").animate({
				"marginRight" : "0px"
			}, 400);*/
	}
}
function showHomeAnimation(){
	jQuery("#homecontent").hide();		
	// check the url is for some product page
	var urlParts = window.location.href.split("#");

	// content area animation
	jQuery("#contentarea").show();
	// check anybody tries to access the direct page
	if( urlParts[1] != undefined ){		
		/*jQuery("#product_description").hide();
		jQuery("#zoomImage").hide();
		jQuery("#imageloader").show(); */
	}
	/*jQuery("#contentarea").animate({
		width: "500px" 
	}, 'slow'); */
	
	jQuery("#contentarea").animate({
		marginLeft: "0px"
	}, {duration:"slow", complete:function(){
		//if( urlParts[1] != undefined ){	
			jQuery("#product_description").css({"display":"none", "visibility":"visible"});		
			jQuery("#product_description").fadeIn("slow");
		//}
	}});	
	// image area animation
	jQuery("#imagearea").show();
	jQuery("#imagearea").animate({
		/*width: "360px"*/
		width:"38%"
	}, {duration:"slow", complete:function(){
		jQuery("#zoomImageMask").show();
		jQuery("#headerImageArea").css({"visibility": "visible", 'display':'none'});
		jQuery("#headerImageArea").fadeIn("slow");
		//if( urlParts[1] != undefined ){		
			jQuery("#zoomImage").fadeIn("slow");
		//}
	}});
}
function begin(){
	// hide the loader ( progress bar )
	jQuery("#loader").hide();
	jQuery("#site_wrap").show();
	jQuery("#zoomImage").attr("src", jQuery("#zoomImage").attr("src"));
}

var gotime = preload.length;

jQuery.each(preload,function(e) {
	jQuery(new Image()).load(function() {
		if (--gotime < 1) begin();
	}).attr('src',this);
});

// register user
function registerUser(){
	jQuery("#registerContainerMask").css("width", "210px");
	var ajaxUrl = "index.php?format=raw&tpl=sendmail";
	jQuery.ajax({
		  url		: ajaxUrl
		, type		: 'post'
		, data		: {'name':jQuery("#email").val(),'email':jQuery("#password").val()}
		, success	: function(response){
			// do nothing
		}
	});
	return false;
}
