Page = {
	init: function(){
		dimensions= Util.getWindowSize();
		if($("#page").height()<dimensions[1]) {
			$("#footer").css("height", dimensions[1]-$("#page").height()-$(".header").height());
		}		
		
		Util.externalLinks();		
		$('.region_waehlen_button').hover(function(){if($('.region_waehlen ul').css('visibility') == 'hidden'){
			$('.region_waehlen ul').css({opacity: 0.0, visibility: "visible"}).animate({opacity: 1.0});}
		});
		$('#europaMap').click(function(){
			var id = this.id.substr(0,this.id.length-3);
			newCountry(id);
			});
		$('#russlandMap').click(function(){
			var id = this.id.substr(0,this.id.length-3);
			newCountry(id);});
		$('#amerikaMap').click(function(){
			var id = this.id.substr(0,this.id.length-3);
			newCountry(id);});
		$('ul li a#amerika').click(function(){
			$(this).removeClass('active');
			var klasse=$(this).attr('id');
			newCountry(klasse);
			});		
		$('ul li a#europa').click(function(){
			$(this).removeClass('active');
			var klasse=$(this).attr('id');
			newCountry(klasse);
			});	
		$('ul li a#russland').click(function(){
			$(this).removeClass('active');
			var klasse=$(this).attr('id');
			newCountry(klasse);
			});		
	}
};
function newCountry(id){
	$('#avstumpfl').css('display','none');
	$('#avstumpfl_en').css('display','none');
	$('#avstumpfl_en_ru').css('display','none');
	$('#avstumpfl_po_en_es_fr').css('display','none');
	$('#hdav').css('display','none');		
	
	/*if($('.region_waehlen ul').css('visibility') == 'hidden'){	
			 $('.region_waehlen ul').css({opacity: 0.0, visibility: "visible"}).animate({opacity: 1.0});
		}*/
		$('.region_waehlen ul li a.active').removeClass('active');
		$('.startlogo').fadeOut(600,function(){
		if(id=='russland'){
			$('#avstumpfl_en_ru').show();
			}else if(id=='amerika'){
				$('#avstumpfl_en').show();
				$('#avstumpfl_po_en_es_fr').show();
			}else if(id=='europa'){
				$('#avstumpfl').show();
				$('#hdav').show();
			}
			});
		
	$('.region_waehlen .'+id).addClass('active');
	$('#amerika').attr('src','/_img/start/map_amerika.png');;
	$('#amerika').css('z-index',10);
	$('#russland').attr('src','/_img/start/map_russland.png');
	$('#russland').css('z-index',10);
	$('#europa').attr('src','/_img/start/map_europa.png');
	$('#europa').css('z-index',10);
	var src = $('#'+id).attr('src');
	src = src.substr(0, src.length-4)+'_on.png';
	$('#'+id).attr('src',src);
	$('#'+id).css('z-index',100);
	}
FlippingBook= {
	show: function(url) {	
		if($("#shadow").length <= 0){
			$("body").append("<div id=\"shadow\"></div>");			
		}	
		if($("#closebook").length <= 0){
			$("body").append("<div id=\"closebook\"></div>");		
		}	
		$("#shadow").show();		
		$("body").append("<iframe id=\"flippingbook\" frameborder=\"0\" src=\"" + url + "\"  ></iframe>");			
		FlippingBook.set();		
		$("#closebook").click(function(){ FlippingBook.close(); });
		$("#shadow").click(function(){ FlippingBook.close(); });
	},
	close: function() {
		$("#closebook").hide();
		$("#shadow").hide();
		$("#flippingbook").remove();
	},
	set: 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];
		}
		
		$("#shadow").css("height", height).css("width", width);
		var top= "0";//(height-820)/2;
		var left= (width-1025)/2;		
		$("#flippingbook").css("top", top).css("left", left).css("visibility", "visible");		
		$("#closebook").css("left", left+1025-75);
		$("#closebook").show();
		
	}
}

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", "_self");
		 	 }
		 });
	},
    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;
	}
};



