Page = {
	init: function(){
		//adjust content height
		if( $('#sidebar').height() > $('#content').height() ){
			$('#content').css('height', $('#sidebar').height()+'px');
		}		
		Util.externalLinks();		
		StartSlider.init();	
		if($('#brandlist').length > 0){ Scroll.init(); }	
		if($('#promotion_container').length > 0){ Promotion.show(); }		
		Headerimages.init();
		
	}
};

Promotion ={
	show: function(){
		var dimension= Util.getWindowSize();		
		var width= dimension[0];
		var height= dimension[1];		
		if(dimension[0]<dimension[2]) {
			width= dimension[2];
		}
		if(dimension[1]<dimension[3]) {
			height= dimension[3];
		}		
	
		var left= parseInt( (width-$("#promotion_container").width())/2 );	
		
		$("#promotion_container").css("left", left+'px').show();
		
		$("#shadow").css("height", height).css("width", width).show();
		
		//$("#shadow").click(function(){ Promotion.close(); });
		$("#promotion_container .btn_close").click(function(){ Promotion.close(); });
	},
	close: function(){
		$('#shadow').hide();
		$('#promotion_container').hide();
	}
}

StartSlider = {
	max: 0,
	init: function(){		
		
	    StartSlider.max = $('#header .current_sale').length;
		if(StartSlider.max >= 2){
			$('#header .current_sale').hide();
			$('#header #prdct1').show();
			window.setTimeout(function() { 							
				$('#header #prdct1').fadeOut(100);
				StartSlider.fade(2);
			}, 6000);
		}
	},
	fade: function( image ){
		$('#header #prdct'+image).fadeIn(100);		
		window.setTimeout(function() { 							
			$('#header #prdct'+image).fadeOut(100);
			if( image == StartSlider.max ){ image = 0; }			
			StartSlider.fade(image+1);
		}, 6000);		
	}
}
Headerimages = {
	max: 0,
	init: function(){		
	    Headerimages.max = $('#header_image_container .header_image').length;
		if(Headerimages.max >= 2){
			window.setTimeout(function() { 							
				$('#header_image_container .image1').fadeOut(2000);
				Headerimages.fade(2);
			}, 2000);
		}
	},
	fade: function( image ){
		$('#header_image_container .image'+image).fadeIn(2000);		
		window.setTimeout(function() { 							
			$('#header_image_container .image'+image).fadeOut(2000);
			if( image == Headerimages.max ){ image = 0; }			
			Headerimages.fade(image+1);
		}, 4000);		
	}
}

Util= {
	getWindowSize: function(){
		var xScroll, yScroll;

		if (window.innerHeight && window.scrollMaxY) {
			xScroll = window.innerWidth + window.scrollMaxX;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight) {
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else if(document.body) { 
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}

		var windowWidth, windowHeight;
		if (self.innerHeight) {
			if(document.documentElement.clientWidth){
				windowWidth = document.documentElement.clientWidth;
			} else {
				windowWidth = self.innerWidth;
			}
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) {
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) {
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}

		if(yScroll < windowHeight) {
			pageHeight = windowHeight;
		} else {
			pageHeight = yScroll;
		}

		if(xScroll < windowWidth) {
			pageWidth = xScroll;
		} else {
			pageWidth = windowWidth;
		}
		var arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
		return arrayPageSize;
	},
	externalLinks: function(){
		 $('a').each(function(){
		 	if( $(this).attr('rel') == "external"  ){
		 		$(this).attr('target', '_blank');
		 	}
		 
		 });
	},
    validateForm: function(id) {
		var result= true;				
		$("#"+id + " input, #"+id + " textarea, #"+id + " input.checkbox, #"+id + " .select").each(function() {
			if($(this).attr("class") && $(this).attr("class").match("required")) {
			    $(this).val($(this).val().replace(/^\s+|\s+$/g, ''));
				var itemresult= Util.validate($(this).attr("id"));				
				result= result && itemresult;
			}
		});		
		if(!result){
			$('.form_info').addClass('error');
		}
		return result;		
	},
	validate: function(id) {
		var result= true;
		var obj= $("#"+id);		
		var commands= $("#"+id).attr("class").split(" ");
		for(var i=0; i<commands.length; i++) {
			var command= commands[i].replace(/[0-9]/g, "");			
			switch(command) {
				case("minlength"):
					var length= parseInt(commands[i].replace(/minlength/g, ""));
					result= result && (obj.val().length>=length);
					break;
				case("maxlength"):
					var length= parseInt(commands[i]);
					result= result && (obj.val().length<=length);
					break;
				case("email"):
					result = result && ( obj.get(0).value.match(/\S@\S.\S{2,}/)!=null );
					break;
				case("checkbox"):
					if( $("#"+id+":checked").length <= 0 ){
						result = result && false;
						$("#"+id).parent().children(".label").addClass("error");
					}					
					break;
				case("select"):
					result = result && !( $("#"+id).val() == '---' );					
					break;
				default:
					result= result && (obj.val().length!=0);
					break;
			}
		}						
		if(!result) {
			obj.addClass("error");
		} else {
			obj.removeClass("error");			
		}
		return result;
	}
}

Scroll = {
	left: 0,
	width: 0,
	itemWidth: 0,
	interrupt: false,
	init: function() {
		$("#brandlist li").each(function() {
			Scroll.width+= parseInt($(this).width())+parseInt($(this).css("margin-left"))+parseInt($(this).css("margin-right"));
		});
		if( Scroll.width > $('#brandlist').width() ){
			$("#brandlist .sledge").css("width", (Scroll.width*2));
			$("#brandlist .sledge li").each(function() {
				$(this).clone().insertAfter("#brandlist .sledge li:last");
			});
			Scroll.run();
			$("#brandlist").hover(function() {
				Scroll.interrupt= true;
			}, function() {
				Scroll.interrupt= false;
			});
		}			
	},
	run: function() {
		if(!Scroll.interrupt) {
			if((Scroll.left*-1)>=Scroll.width) {
				Scroll.left = 0;
				$("#brandlist .sledge").animate({
					marginLeft: Scroll.left
				}, 0);
			}
			Scroll.left-=10;
		}
		$("#brandlist .sledge").animate({
			marginLeft: Scroll.left
		}, 150, "linear", function() {
			Scroll.run();
		});  
	}
}

