Page = {}
Util = {}
Page = {
	init: function(){	
		Page.setCufon();		
		Util.externalLinks();			
		if($("#slideheader_con").length > 0){
			Headerimages.init();
		}
		if($("#flashcontent").length > 0){
			$(".mainnav_level1").css('visibility','hidden');
		}
		Footer.init();
		Page.setLayout();
	},
	setCufon: function(){
		$('#mainnav li.level0').hover(function(){
			$(this).children('a.l0').addClass('hover');
			Cufon.replace('#mainnav a.l0.hover',{color: '#E2001A'});
		},function(){
			$(this).children('a.l0').removeClass('hover');
			Cufon.replace('#mainnav a.l0');
			Cufon.replace('#mainnav li.level0.active a.l0',{color: '#E2001A'});
		});
		Cufon.replace('#mainnav a.l0');
		Cufon.replace('#mainnav li.level0.active a.l0',{color: '#E2001A'});
		Cufon.replace('#subnavigation li.intro span');
		Cufon.replace('h1');
		Cufon.replace('.infobox h3');
		Cufon.replace('#companyfacts h3');
		Cufon.replace('.company_nav li.greetings');
		Cufon.replace('.company_nav li.itxt');
		Cufon.replace('.accitem .company_icon');
	},
	setLayout: function(){
		var dimensions = Util.getWindowSize();//pageWidth,pageHeight,windowWidth,windowHeight
		$("#page").css("height","auto");
		if( $("#page").height() < dimensions[3] ){
			 $("#page").css("height", dimensions[3]+"px"); 
		}else{
			//$("#page").css("height", $("#page").height()+"px");
		}		
	}
}

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("href") != ""
		 	   && $(this).attr("href").indexOf(window.location.hostname) < 0
		 	   && $(this).attr("href").indexOf("http://") >= 0 	
			   ||  $(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){
			$(".mandatory").addClass("error");
		}else{
			$(".mandatory").removeClass("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(".checkbox .info").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;
	}
}

Headerimages = {
	max: 0,
	init: function(){		
	    Headerimages.max = $('#slideheader_con .pic').length;
	    $('#slideheader_con .pic1').fadeIn(2000);
		if(Headerimages.max >= 2){
			window.setTimeout(function() { 							
				$('#slideheader_con .pic1').fadeOut(2000);
				Headerimages.fade(2);
			}, 10000);
		}
	},
	fade: function( image ){
		$('#slideheader_con .pic'+image).fadeIn(2000);		
		window.setTimeout(function() { 							
			$('#slideheader_con .pic'+image).fadeOut(2000);
			if( image == Headerimages.max ){ image = 0; }			
			Headerimages.fade(image+1);
		}, 17000);		
	}
}

Footer = {
	fheight:0,
	init: function(){
		$("#footer .close_btn").click(function(){
			if($("#footer").hasClass("active")){
				Footer.fheight = 0;
				Footer.animatebox();
				$("#footer").removeClass("active");
			}else{
				Footer.fheight = $("#footer .closebox").show().css('height','auto').height();
				$("#footer .closebox").css('height','0px');
				Footer.animatebox();
				$("#footer").addClass("active");			
			}
		});
	},
	animatebox: function(){
		$("#footer .closebox").animate({
			height: Footer.fheight+"px"
		}, 500, function(){});
	}
}
Accordion ={
	init: function(){
		$(".accitem .close_btn").click(function(){
			var id = "#"+$(this).parent().attr("id");
			var dimensions = Util.getWindowSize();
			var h = $(id+" .content_container").show().css('height','auto').height();
			if($(id).hasClass("active")){			
				if( $("#page").height() - h < dimensions[3] ){
					$("#page").css('height',dimensions[3]+'px');
				}				
				Accordion.animatebox(id, 0);
				$(id).removeClass("active");				
			}else{				
				$(id + " .content_container").css('height','0px');
				Accordion.animatebox(id, h);
				$(id).addClass("active");			
			}
		});
	},
	animatebox: function(item_id, box_height){		
		$(item_id +" .content_container").animate({
			height: box_height+"px"
		}, "slow", function(){ Page.setLayout(); });
	}
}


