Util = {};

function newsletter(){ $(".newsletter_colorbox").colorbox({iframe:true, innerWidth:340, innerHeight:550}); }
$(document).ready(function () {	
		
	/* content functions */
	if( $('#paging_table').length > 0 ){
		Pagination.init();
	}
	if( $('#slide_gallery').length > 0 ){
		Slidegallery.init();
	}
	if( $('#slider_area').length > 0 ){
		Projectslide.init();
	}
	if( $('#project_detail_box').length > 0 ){
		Projectdetailbox.init();
	}
	if($('#overlay').length > 0){Overlay.init();}
	
	$(".special_colorbox").colorbox({iframe:true, innerWidth:350, innerHeight:340});	
	//newsletter
	if($(".newsletter_colorbox").length > 0){ newsletter(); }
	
	//Unterseiten von Rechtliches
	if( $('#contentitems_container').length > 0){
		$('#contentitems_container h2').click(function(){
			$('#contentitems_container li.active').removeClass('active');
			$(this).parent().addClass('active');
		});
	}	
	
	/* Search */
	Search.init();
	
	/* Key-Bind */
	$(document).bind('keydown','Ctrl+m', function(){
		 window.location ="http://mail.koop.at/webmail";
	});	
		
	/* NAVIGATION */
	if(!browser.isIE) {
		$('#navigation li.l1').hover(function(){
				$('#navigation li .subnav').css('visibility','hidden'); 
				$(this).children('.subnav').css('visibility','visible'); //.show();		
			},
			function(){
				$('#navigation li .subnav').css('visibility','hidden');
				$('#navigation li.active .subnav').css('visibility','visible');//.show();
			}
		);
	}	
	
	/* Switch Location-Data*/
	$('#location_contact .contact_data span').hide();
	$('#location_contact .contact_data span.graz').show();
	$('#location_contact .locations span').hover(function(){
		var span_class = $(this).attr('class');
		$('#location_contact .contact_data span').hide();
		$('#location_contact .contact_data span.'+span_class).show();
	},function(){});
	
	/* TEAM */
	 if( $('#team_navigation').length > 0 ){			
			/* Teamnavigation */
			$('#team_navigation li').click(
				function(){			
					$('#team_navigation li.active').removeClass('active');
					$(this).addClass('active');
					$('#team_container .team_pic').css('opacity', '1');
					//$('#team_container .team_pic:hover').css('cursor', 'pointer');
					
					var id = $(this).attr('id');
					$('#team_container .team_pic').each(
						function(i){
							if( !$(this).hasClass(id) ){
								 $(this).css('opacity', '0.3');
								 $(this).removeClass('clickable');
								 $(this).unbind('click');			 
							}else{
								$(this).addClass('clickable');
								$(this).click(function(){
									$('.team_detail_box').hide();
									var id = $(this).attr('id');
									$('.'+id).fadeIn();
								});			
							}
						}
					);										
				}						
			);
			/* Teamndetail */	
			$('#thumbs_container .clickable').click(function(){
				$('.team_detail_box').hide();
				var id = $(this).attr('id');
				$('.'+id).fadeIn();
			});							
			$('.team_detail_box .close').click(function(){
				$(this).parent().fadeOut();
			});						
		}		
	
	$("a[href]").each(function(){	 	
	 	if($(this).attr("href") != "" &&	 	
	 	   $(this).attr("href").indexOf(window.location.hostname) < 0 &&
		   $(this).attr("href").indexOf("http://") >= 0 	 	   
	 	 ){
	 		$(this).attr("target", "_blank");
	 	 }
	 
	 });
 }); 

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');
			if( !$('#project_detail').length > 0  ){
				$('.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:4,
				  callback:Pagination.showrows,
				  num_edge_entries: 2,
				  ellipse_text: '...'			  
			  });		    
		 }		
	}
};
//shwohrows = function(page_id, jq){ Pagination.showrows(page_id, jq); };
Slidegallery = {
	num_items: 0,
	item_with: 0,
	sledge_width: 0,
	current_pos: 0,
	animation_time:750,
	init: function(){
		Slidegallery.num_items = $('.sledge .slide_item').length;
		Slidegallery.item_with = $('.sledge .slide_item').width();
		Slidegallery.sledge_width = Math.round(Slidegallery.num_items *  Slidegallery.item_with );		
		$('.sledge').width(Slidegallery.sledge_width);		
		
		//add Event-Listener
		$('#slidebuttons .prev').click(function(){
			Slidegallery.prev();
		});
		$('#slidebuttons .next').click(function(){
			Slidegallery.next();
		});
		
		//deactivate Prev-Button
		$('#slidebuttons .prev').hide();		
	},
	next: function(){
		Slidegallery.current_pos++;
		Slidegallery.make_animation();
		
		//activate Prev-Button
		$('#slidebuttons .prev').show();		
		//end of slide?
		if( Slidegallery.current_pos == Slidegallery.num_items-1 ){
			$('#slidebuttons .next').hide();
		}
	},
	prev: function(){
		Slidegallery.current_pos--;
		Slidegallery.make_animation();
		
		//activate Prev-Button
		$('#slidebuttons .next').show();		
		//end of slide?
		if( Slidegallery.current_pos <= 0 ){
			$('#slidebuttons .prev').hide();
		}
	},
	jump: function( pos ){
		$(".sledge").clearQueue();
		Slidegallery.current_pos = pos;			
		$('#slidebuttons .prev').show();			
		$('#slidebuttons .next').show();		
		
		//end of slide?
		if( Slidegallery.current_pos >= Slidegallery.num_items-1 ){			
			$('#slidebuttons .next').hide();
		}
		//end of slide?
		if( Slidegallery.current_pos <= 0 ){	
			$('#slidebuttons .prev').hide();
		}
		
		Slidegallery.make_animation();	
	},
	make_animation: function(){
		var val =Slidegallery.current_pos * Slidegallery.item_with;
		$(".sledge").animate({'left': '-'+val}, Slidegallery.animation_time);
	}
};

Projectslide = {
	actImage : 0,
	imageWidth: 345,
	animationTime: 1500,
	nrImages: 0,
	imageContainerWidth:0,
	nextMove: true,
	prevMove: true,
	init: function(){
		window.setTimeout(function(){
			$('#slider_area .slider_info').fadeOut();
		}, 2000);		
		Projectslide.nrImages = $(".ovbox").length;
		$("#project_container > div").css("width", Projectslide.nrImages * (13 + 286 + 13 + 33)); // padding + width + padding + margin-right							  
		Projectslide.imageContainerWidth = $("#project_container div").css("width");
		Projectslide.prevMove = false;
		$("#prev").css('visibility', 'hidden');		
		$("#slider").slider({
			min: 0,
			max: (Projectslide.nrImages-3),
			value: Projectslide.actImage,
			change: function(event, ui) {
				$("#project_container div").clearQueue();
				Projectslide.moveToImage(ui.value);
			}
		});	
		$("#prev").click( function() {
			Projectslide.prevImage();
			return false;			
		});	
		$("#next").click( function() {
			Projectslide.nextImage();
			return false;			
		});		
		$("#next, #prev").hover(
			function () { 
				$(this).css('background-position', '0 -22px').css('cursor','pointer');
			}, function () {
				$(this).css('background-position', '0 0').css('cursor','default');
			}
		);	
	},
	moveToImage: function(newImage) {
		Projectslide.actImage = newImage;		
		// linkes Ende
		if(Projectslide.actImage == 0) {
			$("#prev").css('visibility', 'hidden');
			Projectslide.prevMove = false;
		}		
		// rechtes Ende
		if(Projectslide.actImage == (Projectslide.nrImages-3) ) {
			Projectslide.nextMove = false;
			$("#next").hide();
		}		
		// Sichtbarschalten der next-Pfeile
		if((!Projectslide.nextMove) && (Projectslide.actImage != (Projectslide.nrImages-3))) {
			Projectslide.nextMove = true;
			$("#next").show();
		}		
		// Sichtbarschalten der prev-Pfeile
		if((!Projectslide.prevMove) && (Projectslide.actImage != 0)) {
			Projectslide.prevMove = true;
			$("#prev").css('visibility', 'visible');
		}
		var val = "0px";
		if( Projectslide.actImage != 0 ) {
			val = "-" + (Projectslide.actImage * Projectslide.imageWidth) + "px";		
		}
		
		$("#project_container div").animate({"left": val}, Projectslide.animationTime);
	},
	prevImage: function(){
		// Sichtbarschalten der next-Pfeile
		if(!Projectslide.nextMove) {
			Projectslide.nextMove = true;
			$("#next").show();
		}		
		if( Projectslide.actImage > 0 ) {
			Projectslide.actImage--;
			var val = "-" + (Projectslide.actImage * Projectslide.imageWidth) + "px";			
			$("#project_container div").animate({"left": val}, Projectslide.animationTime);
			$( "#slider" ).slider( "option", "value", Projectslide.actImage );
		}		
		// linkes Ende
		if(Projectslide.actImage == 0) {
			$("#prev").css('visibility', 'hidden');
			Projectslide.prevMove = false;
		}
	},
	nextImage: function(){
			if(!Projectslide.prevMove) {
				Projectslide.prevMove = true;
				$("#prev").css('visibility', 'visible');
			}			
			if( Projectslide.actImage < (Projectslide.nrImages-(1+2)) ) {
				Projectslide.actImage++;
				var val = "-" + (Projectslide.actImage * Projectslide.imageWidth) + "px";					
				$("#project_container div").animate({"left": val}, Projectslide.animationTime);
				$( "#slider" ).slider( "option", "value", Projectslide.actImage );				
				// rechtes Ende
				if(Projectslide.actImage == (Projectslide.nrImages-3) ) {
						//alert("ende");
						Projectslide.nextMove = false;
						$("#next").hide();
				}
			} 
	}
};

Projectdetailbox = {
	init: function(){
		
		$('#project_detail_box li').click(function(){
			if( !$(this).hasClass('active') ){
				$('#project_detail_box li.active').removeClass('active');			
				$(this).addClass('active');
			}
		});
		$('#project_detail_box .btn_close').click(function(){			
			var boxWidth = $('#project_detail_box').width();
			var offset = $('#project_detail_box .btn_open').width();		
			$('#project_detail_box').animate({'right': - ( boxWidth - offset )}, 500, function(){ $('#project_detail_box .btn_open').show(); });
			$('#project_detail_box ul h3').hide();
		});
		$('#project_detail_box .btn_open').click(function(){
			$('#project_detail_box .btn_open').hide();
			$('#project_detail_box ul h3').show();			
			$('#project_detail_box').animate({'right': 0 }, 500);
			$('#project_detail_box').removeClass('closed');
			
		});
		
	}
};

/* --------------------------- */
/* search ------------------- */
/* --------------------------- */
Search= {
	active: false,	
	init: function() {		
		$('#search_container').hover(function(){ $(this).addClass('active'); });		
		$("#search").hover(function() {
			$(this).get(0).focus();
		});
		$("#search").keyup(function() {
			Search.go();
		}).click(function() {			
			Search.go();
		});
		$("#search_container .close span").click(function() {
			Search.hideResults();
		});
	},
	go: function() {
		if($("#search").val().length>=3) {
		
			$.ajax({
				url: '/helper/search/',
				data: 'term='+$("#search").val(),
				type: 'GET',
				dataType: 'html',
				timeout: 8000,
				success: function(html) {
					$("#searchresults .list").html(html);
					Search.showResults();
				}
			});
			
		} 
	},
	showResults: function() {
		if(!Search.active) {			
			$("#searchresults .list").css("top", '-'+$("#searchresults").height()+35);
			$("#searchresults").show();
			$("#searchresults .list").animate({
				top: '0px'
			}, 500, 'swing', function() {
				Search.active= true;
			});
		}
	},
	hideResults: function() {	$("#searchresults").show();
		$("#searchresults .list").css('top', ($("#searchresults .list").height()+50)*-1);
		$("#searchresults .list").html('');
		Search.active= false;
		$("#search_container").removeClass('active');
		$("#searchresults").hide();
	}
};

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;
	}
};
Overlay= {
	init: function(url) {	
		//$("#shadow").show();	
		//$("#shadow").css('display', 'block');						
		Overlay.set();
		$("#overlay").click(function(){
			$(this).hide();
			window.location = '/';
		});
		window.setTimeout(function(){
			$('#overlay').fadeOut();
			window.location = '/';
		}, 5000);		
		
	},
	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];
		}	
		$("#overlay").css('left', parseInt( (width-$("#overlay img").width())/2)+'px').css('height',$("#overlay img").height()+'px').css('width',$("#overlay img").width()+'px');
		
	}
}


