Page= {
	init: function() {
		Search.init();
		Video.init();
		$('div.news.grey').css({ opacity: 0.6 });
		if( $('.cufon_replace').length>0){
			Cufon.replace('.cufon_replace');
		}
		
		if($('#news_image_thumbs').length>0){
			News.init();
		}	
		if($('.print_retailers').length>0){
			Retailers.init();
		}
		
		if(!$("#userdata").attr("class").match("set")) {
			if(google.loader.ClientLocation) {
		        latitude = google.loader.ClientLocation.latitude;
		        longitude = google.loader.ClientLocation.longitude;
				country = google.loader.ClientLocation.address.country_code;
				city = google.loader.ClientLocation.address.city;

				$.ajax({
					url: '/helper/geo',
					data: 'latitude='+latitude+"&longitude="+longitude+"&country="+country+"&city="+city,
					type: 'POST',
					dataType: 'html',
					timeout: 5000,
					success: function(html) {
						window.location.reload();
					}
				});
	   	    }
		}

		if($("#form_country").length>0) {
			$("#form_country").change(function() {
				$("#form_country option:selected").each(function () {
					switch($(this).attr("value")) {
						case 'GR':
						case 'IR':
					    case 'IL':
						case 'LU':
						case 'SK':
						case 'SI':
						case 'CZ':
							url= location.href;
							url = url.substr(0, url.lastIndexOf("/")+1)+$(this).attr("value");
							location.href= url;
							break;
					}
				});
			});
		}
		
	    $(".colorbox").colorbox();
		
		Util.externalLinks();
	}
}
Gallery= {
	init: function() {
		/*$(".galleries .gallery").each(function() {
			var gallery= $(this).attr("id");
			$(".image", $(this)).click(function() { 
				$(".info").attr("style", "background:transparent url(" + $(this).attr("id") + ") no-repeat center center;");
			});
		});*/
	}
}

Retailers={
	init: function(){
		$(".print_retailers .btn").click(function(){
			var data = "";
			var count=1;
			var first = true;
			$(".make_notice input:checked").each(function(){
				if(!first){
					data += "&";
				}				
				data += "retailer"+count+"="+$(this).attr("name");
				count++;
				first=false;
			});		
			//var current_lang = $("#languageselect li.active a").attr("href");
			//window.open(current_lang+"/print/retailers?"+data,"","menubar=0,scrollbars=1,width=680");
			window.open("/de/print/retailers?"+data,"","menubar=0,scrollbars=1,width=640");
		});
	}
}

News={
	init: function(){
		$('.news_image_thumb').hover(function(){
			$('#news_image .news_image_big').hide();
			$('#news_image .'+$(this).attr('id')).show();			
		},function(){
			$('#news_image .news_image_big').hide();
			$('#news_image .news_image_big.news_image1').show();
			$('#news_image #news_image1').addClass('active');
			
		});
	}
}

Video= {
	init: function() {
        $("#videoplayer .close").click(function() {
			$("#videoplayer .iframe").html("");
			$("#videoplayer").hide();
			$(".video").removeClass("active");
		});
		
		$(".video").click(function() {
			$("#videoplayer .iframe").html('<iframe src="'+$(this).attr("rel")+'" width="300" height="168" scrolling="no" frameborder="0"></iframe>');
			$("#videoplayer").show();
			$(".video").removeClass("active");
			$(this).addClass("active");
		});
	}
}

Servicevideos= {
	init: function() {
		$("#servicevideos1 .servicevideo, #servicevideos2 .servicevideo").click(function() {
			$("#servicevideos1 .servicevideo, #servicevideos2 .servicevideo").removeClass("active");
			$(this).addClass("active");
			$("#serviceplayer").html($(".video", $(this)).html());
		})
	}
}

Search= {
	active: false,
	
	init: function() {
		$("#search").hover(function() {
			$(this).get(0).focus();
		});
		$("#search").keyup(function() {
			Search.go();
		}).click(function() {
			Search.go();
		});
		$("#searchresults .close").click(function() {
			Search.hideResults();
		}); 
    },
	go: function() {
		Search.showLoader();
		if($("#search").val().length>=3) {
			$.ajax({
				url: '/helper/search',
				data: 'search='+$("#search").val(),
				type: 'POST',
				dataType: 'html',
				timeout: 5000,
				success: function(html) {
					$("#searchresults .list").html(html);
					Search.showResults();
				}
			});
		} else {
			Search.hideResults();
		}
	},
	showResults: function() {
		if(!Search.active) {
			$("#searchresults").css("top", '-'+$("#searchresults").height()+33);
			$("#searchresults").animate({
				top: "33px"
			}, 500, 'swing', function() {
				Search.active= true;
			});
		}
		Search.hideLoader();
	},
	hideResults: function() {
		if(Search.active) {
			postop= ($("#searchresults").height()+33)*-1;
			$("#searchresults").animate({
				top: postop
			}, 500, 'swing', function() {
				Search.active= false;
			});
		}
		Search.hideLoader();
	},
	showLoader: function() {
		$("#form_search .loader").show();
	},
	hideLoader: function() {
		$("#form_search .loader").hide();
	}   

}

Kitchen= {
	current: 0,
	
	init: function() {
		$("#focus img").load(function() {
			$(this).fadeIn("fast");
		});
		$("#zoom img").load(function() {
			$("#zoom").fadeIn("fast");
		});
		
		$("#focus").hover(function() {
			$(".hover", $(this)).show();
		}, function() {
			$(".hover", $(this)).hide();
		}).click(function() {
			image= $("#zoom"+Kitchen.current).attr("rel");
			$("#zoom img").attr("src", image);
		});
		$("#zoom").click(function() {
		    $(this).fadeOut("fast");
		});
		
		$("#gallery .image").click(function() {
			Kitchen.current= $(this).attr("id").replace(/gallery/, '');
			image= $("#focus"+Kitchen.current).attr("rel");
			
			$("#focus img").fadeOut("fast", function() {
				$(this).attr("src", image);
			});
		});
	}
}

DemoKitchen= {
	current: 0,
	
	init: function() {
		$("#demozoom img").load(function() {
			$("#demozoom").fadeIn("fast");
		});

		$("#demozoom").click(function() {
		    $(this).fadeOut("fast");
		});
		
		$(".demo .images .main, .demo .images .gallery .image").click(function() {
			DemoKitchen.current= $(this).attr("id").replace(/gallery/, '');
			image= $("#demozoom"+DemoKitchen.current).attr("rel");
			$("#demozoom img").attr("src", image);
		});
	}
}

Start= {
	first: 1,
	last: 3,
	current: 1,
	timeout: null,
	
	init: function () {
		$("#page .start .detail").hide();
		$("#page .start .detail:first").show();
		$("#page .start .button").hover(function() {
			clearTimeout(Start.timeout);
			$("#page .start .button").removeClass("hover");
			$(this).addClass("hover");
			$("#page .start .detail").hide();
			$("#detail"+$(this).attr("id").replace(/button/, '')).show();
			
		}, function() {
			Start.timeout= window.setTimeout(Start.next, 5000);
		});
		
		window.setTimeout(Start.run, 5000);
	},
	run: function() {
		$("#page .start .preview").fadeOut("slow");
		
		Start.timeout= window.setTimeout(Start.next, 5000);
	},
	next: function() {
		Start.current++;
		if(Start.current>Start.last) {
			Start.current= Start.first;
		}
		$("#page .start .button").removeClass("hover");
		$("#button"+Start.current).addClass("hover");
		$("#page .start .detail").hide();
		$("#detail"+Start.current).show();
	
		Start.timeout= window.setTimeout(Start.next, 5000);
	}
}

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() {
		//Source: http://www.sitepoint.com/print/standards-compliant-world/
		if (!document.getElementsByTagName) {
			return;
		}
		var anchors = document.getElementsByTagName("a");
		for (var i=0; i<anchors.length; i++) {
			var anchor = anchors[i];
			if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") {
				anchor.target = "_blank";
			}
		}
	},
	validateForm: function(id) {
		var result= true;
		$("#"+id + " input, #"+id + " textarea").each(function() {
			if($(this).attr("class") && $(this).attr("class").match("required")) {
			  // trim input field
			  $(this).val($(this).val().replace(/^\s+|\s+$/g, ''));
				var itemresult= Util.validate($(this).attr("id"));
				result= result && itemresult;
			}
		});
		if(result) {
			$("#form_error").removeClass("error");	
		} else {
			$("#form_error").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;
				default:
					result= result && (obj.val().length!=0);
					break;
			}
		}
		if(!result) {
			obj.addClass("error");
		} else {
			obj.removeClass("error");
		}
		return result;
	}      
}
