Functions= {};

$(document).ready(function () {
	if(!browser.isIE6x) {
		Functions.init();
	}
	if($(".lightbox").length > 0 ) {
		$(".lightbox").lightbox();
	}
	if($(".clickLink").length > 0 ){
		$(".clickLink").click(
			function()
			{	
			    window.location = $(this).attr("title");
			}
		);		
	}
	
	if( $('#newsteaser').length > 0 ){
		TeaserSlide.init();
	}
	if( $('#paging_table').length > 0 ){
		Pagination.init();
	}
	
		
	$('a').each(function(){
	 	if( $(this).attr('rel') == "external"  ){
	 		$(this).attr('target', '_blank');
	 	}
	});
	//fix for house hover-effect
	$('#page .house a.button').hover(function(){},function(){Functions.hidePreview();});	
	$('#page .house').hover(function(){},function(){Functions.hidePreview();});
});



Functions= {
	navigationPosition: new Array(),
	activePosition: '',	
	init: function() {
		var cumulate= 0;
		var previous= 0;
		$("#header .mainnavigation li a").each(function() {
			cumulate= previous;
			Functions.navigationPosition[$(this).attr("id")]= cumulate;
			$(this).hover(function() {
				//onmouseover
				var width= $(this).width();
				if(browser.isIE && browser.versionMajor < 8) {
					width-= 30;
				}
				$("#navigation_highlight .middle").css("width", width);
				$("#navigation_highlight").css("width", width+18).css("left", Functions.navigationPosition[$(this).attr("id")]+6);
			}, function() {
				//onmouseout
				Functions.setNavigation(Functions.activePosition);
			});
			previous+= $(this).width()+30;
		});
		$("#header .mainnavigation li").each(function() {
			var a= $("a", $(this));
			$(this).css("padding-left", 0).css("padding-right", 0).css("width", a.width()+30);
			a.css("width", a.width()+30).css("display", "block");
		});
	},
	setNavigation: function(id) {
		Functions.activePosition= id;
		var width= $("#"+id).width();
		if(browser.isIE && browser.versionMajor < 8) {
			width-= 30;
		}
		if(typeof(Functions.navigationPosition[$("#"+id).attr("id")])=='undefined') {
			$("#navigation_highlight").css("left", -2000);
		} else {
			$("#navigation_highlight .middle").css("width", width);
			$("#navigation_highlight").css("width", width+18).css("left", Functions.navigationPosition[$("#"+id).attr("id")]+6);
		}
	},
	homeZoom: function(img) {
		if($("#homeimage img:first").attr("src")!=img) {
			$("#homeimage img:first").fadeOut("fast", function() {
				$(this).attr("src", img);
				$(this).fadeIn("fast");
			});
		}
	},
	validateForm: function(id) {
		var result= true;
		$("#"+id + " input").each(function() {
			if($(this).attr("class") && $(this).attr("class").match("required")) {
				//cause of brindis
				var itemresult= Functions.validate($(this).attr("id"));
				result= result && itemresult;
			}
		});
		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;
				default:
					result= result && (obj.val().length!=0);
					break;
			}
		}
		if(!result) {
			obj.addClass("error");
		} else {
			obj.removeClass("error");
		}
		return result;
	},
	showPreview: function(id, url, p_left, p_top) {
		$.ajax({
			url: url,
			data: 'id='+id,
			type: 'POST',
			dataType: 'html',
			timeout: 5000,
			success: function(html) {
				//$("#house_preview");
				$("#house_preview").html(html).css({left: (parseInt(p_left)+50)+'px', top: (parseInt(p_top)-80)+'px'}).show();
			}
		});
	},
	updatePreview: function(e) {
		x = (document.all) ? window.event.x + document.body.scrollLeft : e.pageX;
		y = (document.all) ? window.event.y + document.body.scrollTop  : e.pageY;
		var dimensions= Functions.getWindowSize();
		var correction_x= ((dimensions[0]-800)/2*-1)+120;
		var correction_y= -130;
		if((document.all)) {
			correction_x= +115;
			correction_y= 0;
		}		
		$('#house_preview').css('left', x+correction_x + "px");
		$('#house_preview').css('top', y+correction_y + "px");
	},
	hidePreview: function() {
		$("#house_preview").html("").hide();
	},
	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){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else if(document.body) { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}

		var windowWidth, windowHeight;

		if (self.innerHeight) { // all except Explorer
			if(document.documentElement.clientWidth){
				windowWidth = document.documentElement.clientWidth;
			} else {
				windowWidth = self.innerWidth;
			}
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}

		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else {
			pageHeight = yScroll;
		}


		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){
			pageWidth = xScroll;
		} else {
			pageWidth = windowWidth;
		}

		var arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
		return arrayPageSize;
	}
}

Pagination = {
	items_per_page: 0,
	nmb_of_items: 0,
	entries: 0,
	showrows: function(page_id, jq){
		var showrowclass = '.group' + (page_id+1);
		$('#paging_table tr').hide();
		$(showrowclass).show();
		return false;		
	},
	init: function(){
		Pagination.items_per_page = $('#paging_table .group1').length;
		Pagination.nmb_of_items = $('#paging_table .row').length;
		if(Pagination.nmb_of_items > Pagination.items_per_page){
	  		Pagination.entries=(Pagination.nmb_of_items/Pagination.items_per_page);
		  	var x=Pagination.nmb_of_items%Pagination.items_per_page;
		  	if(x != 0){
		  		Pagination.entries=(parseInt(Pagination.entries))+1;
		    }
		    $('#overviewarea').prepend('<div class="pagination_wrapper"><div class="pagination"></div></div><br style="clear:both" />');		
		    // Create duplicate of entire section below table		
			var tptable = $('#paging_table');		
			$('.pagination_wrapper').clone().insertAfter(tptable);			    
		    //show pagination
		    $('#paging_table tr').hide();
		    $('#paging_table tr.group1').show();				  
			  // Create pagination element		
			  $(".pagination").pagination(Pagination.nmb_of_items, {
				  items_per_page:Pagination.items_per_page,
				  num_display_entries: Pagination.entries,
				  callback:Pagination.showrows
			  });		    
		 }		
	}
};
var showrows = function(page_id, jq){ Pagination.showrows(page_id, jq); }


TeaserSlide = {
	left: 0,
	width: 0,
	itemWidth: 0,
	interrupt: false,

	init: function() {
		$("#newsteaser li").each(function() {
			TeaserSlide.width+= parseInt($(this).width());
		});
		TeaserSlide.itemWidth = $("#newsteaser").width();
		$("#newsteaser .sledge").css("width", (TeaserSlide.width*2));
		$("#newsteaser .sledge li").each(function() {
			$(this).clone().insertAfter("#newsteaser .sledge li:last");
		});
		window.setTimeout(function() {
				TeaserSlide.run();
			}, 4000);
		$("#newsteaser").hover(function() {
			TeaserSlide.interrupt= true;
		}, function() {
			TeaserSlide.interrupt= false;
		});
	},
	run: function() {
		if(!TeaserSlide.interrupt) {
			if((TeaserSlide.left*-1)>=TeaserSlide.width) {
				TeaserSlide.left = 0;
				$("#newsteaser .sledge").animate({
					left: TeaserSlide.left
				}, 0);
			}
			TeaserSlide.left -=  TeaserSlide.itemWidth;//10;
		}
		$("#newsteaser .sledge").animate({
			left: TeaserSlide.left
		}, 1000, "linear", function() {
			window.setTimeout(function() {
				TeaserSlide.run();
			}, 4000);
		});  
	}
}

