var stageState = 0;

function resetURL() {
	var url = $(this).attr("href");
	history.pushState({path: url}, "", "/");
	$("title").replaceWith("<title>" + defaulttitletag + "</title>");
	return true;
}
function closestage() {
	$('#mainstage').slideUp('slow',function(){
		$('#mainstage #mainstage-content').html("");
		
		var target=$("#arbeiten").offset().top;
		var duration=200;
		var easing='swing';
		if($.browser.safari) var animationSelector='body:not(:animated)';
		else var animationSelector='html:not(:animated)';
		$(animationSelector).animate({ scrollTop: target }, duration, easing, function() {});
		
	});
	stageState = 0;
	
	resetURL();
}
var pproject;
function showstage(id) {
	pproject = id;
	getProject(id,0);
}
function nextproject() {
	getProject(pproject,1);
}
function prevproject() {
	getProject(pproject,-1);
}
function getProject(id,scr) {
	var target=$("#mainstage-entry").offset().top + 40;
	var duration=700;
	var easing='swing';
	if($.browser.safari) var animationSelector='body:not(:animated)';
	else var animationSelector='html:not(:animated)';

	// animate to target and set the hash to the window.location after the animation
	//var cheight = $('#mainstage #mainstage-content').height();
	//$('#mainstage #mainstage-content').html('<div style="height:' + cheight + 'px" class="loader"></div>');

	$(animationSelector).animate({ scrollTop: target }, duration, easing, function() {});
	  

	  	$("#mainstage #mainstage-content").show().css({visibility: "hidden"});
	  	$('#mainstage #mainstage-content').load('/api/projekte.html?id=' + id + '&s=' + scr, function() {
	  	// $('#mainstage').show();
	  	  if (stageState != 1) {
	  		  $('#mainstage').slideDown('slow', function() {
	  		  	stageState = 1;
	  		  	$("#mainstage #mainstage-content").hide().css({visibility: "visible"}).fadeIn("fast");
	  		  });
	  		}
	  		else {
	  			$("#mainstage #mainstage-content").hide().css({visibility: "visible"}).fadeIn("fast");
	  		}
	  	});

} 


function preloader() {
	// Preloader rein
	$('#mainstage .scrollable img').each(function(index,value){
		var background = $(value).attr('src');
		$(value).attr('src',"/layout/css/transparent.gif");
		$(value).css('background-image',"url(/layout/css/loader.gif)");
		var imageObj = new Image();
		$(imageObj).attr("src", background).load(function(response) {
			$(value).css('background-image',"none");
			$(value).attr('src',background);
		});
	});
}

$(document).ready(function() {
	
	$("#impressum").hide("fast");
	$("#navigation").fadeIn("slow");
	
	
	$(window).scroll(function() {
		$("#navigation").hide();
		$("#navigation").fadeIn("slow");
	}); 
	
	$("#lnk-imprint").click(function() {
		resetURL();
		$('#impressum').toggle(function() {
			if ($(this).css("display") == "block") {;
				$("#lnk-imprint").html("Impressum ausblenden");
				$("#lnk-imprint").addClass("close");
			}
			else {
				$("#lnk-imprint").html("Impressum einblenden");
				$("#lnk-imprint").removeClass("close");
			}
		});
	});
	
   $('a[href*=#]').click(function() {

      // duration in ms
      var duration=700;

      // easing values: swing | linear
      var easing='swing';

      // get / set parameters
      var newHash=this.hash;
      var target=$(this.hash+', a[name='+this.hash.slice(1)+']').offset().top;
      var oldLocation=window.location.href.replace(window.location.hash, '');
      var newLocation=this;

      // make sure it's the same location      
      if(oldLocation+newHash==newLocation)
      {
         // set selector
         if($.browser.safari) var animationSelector='body:not(:animated)';
         else var animationSelector='html:not(:animated)';

         // animate to target and set the hash to the window.location after the animation
         $(animationSelector).animate({ scrollTop: target }, duration, easing, function() {
            // add new hash to the browser location
            window.location.href=newLocation;
         });

         // cancel default click action
         return false;
      }
   });
});

function initialize_Tabs(id) {	
	google.maps.event.trigger(map, 'resize');
	map.setZoom(map.getZoom());
	map.setCenter(yakamaraLL);
	
	// Customer Scroller, Init
	if ($(id + ' .scrollable').length >= 1) {
		var api = $(id + ' .scrollable').data("scrollable");
		api.seekTo(0,1);
	}
	
}

function homeCarInit(carousel) {
	$("#header #homeslider li").css("visibility","visible");
    $('#homeslider .jcarousel-control a').bind('click', function() {
        carousel.scroll(jQuery.jcarousel.intval(jQuery(this).text()));
			$("#homeslider .jcarousel-control a").removeClass("selected");
			$(this).addClass("selected");
        return false;
    });
};
function homeCarLoad(instance,state) {
    var item = instance.first;
    if (state != "init" && item > 1) {
    	// Which Grey Element should be highlighted 
    	var teiler = item - 1;
    	$('#homeslider .jcarousel-control li').removeClass("selected");
    	$('#homeslider .jcarousel-control li').eq(teiler).addClass("selected");
    }
    else {
    	$('#homeslider .jcarousel-control li').removeClass("selected");
    	$('#homeslider .jcarousel-control li').eq(0).addClass("selected");
    }
};

jQuery(document).ready(function() {
	items = $('#homeslider .slide').length;
	if (items > 1) {
	    jQuery("#homeslider").jcarousel({
	    	initCallback: homeCarInit,
	    	itemLoadCallback: homeCarLoad,
	        scroll: 1,
	        auto: 10,
	        visible: 1,
	        wrap: 'last'
	    });
	}
});

/**
/ THIRD FUNCTION
* getPageSize() by quirksmode.com
*
* @return Array Return an array with page width, height and window width, height
*/
function ___getPageSize() {
	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 { // 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;
	}
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
	return arrayPageSize;
};
/**
/ THIRD FUNCTION
* getPageScroll() by quirksmode.com
*
* @return Array Return an array with x,y page scroll values.
*/
function ___getPageScroll() {
	var xScroll, yScroll;
	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;	
	}
	arrayPageScroll = new Array(xScroll,yScroll);
	return arrayPageScroll;
};
