/* ajax loading script */

// redirect for right page
function redirect(hash) {
	var url = window.location.href;
	var lastStr = url.substr((url.length-1),1)
	if(lastStr != "/" && (!hash)) {
		var newUrl = url.split("/");
		newUrl = url.replace(newUrl[newUrl.length-1],"");
		window.location.href = newUrl;
	}
}

// set hash -> triggers load
function loadIt(url,colorScheme) {
	url = url.replace('index.php?article_', '');
	url = url.split('&');
	var lang = window.location.hash.split('~');
	if(lang[2]) {
		lang = lang[2].substr((lang[2].length - 1));
	} else {
		lang = "0";
	}
	window.location.hash = "~"+url+"~lang="+lang+"~topic="+colorScheme;
}

// set Active link
function setActive(obj) {
    $("ul#menu li").removeClass("active");
    window.setTimeout( function() {
        obj.parent().addClass("active");
    }, 150);    	
}

// create divs and load content
function loadContent(theHref,colorScheme) { 
	
	// stop slideshow
	if($("body").hasClass("home") && typeof slideShowId != "undefined") {
		clearInterval(slideShowId);
	}
	
	// get content
    var toLoad = theHref+' #frame div.load';
    var toLoadA = theHref+' #arrows div';        	
    var loaded = "false";
    var loadedA = "false";
    
    // load new content
    $('#frame').after('<div id="newFrame" style="display:none;"></div>');
    $('#newFrame').load(toLoad,'', function () {
    	loaded = "true";
		// tracking		
		var pageTitle = $('#newFrame').find(".pageTitle").attr("id");
		var newUrl = theHref.replace("index.php?article_","")
		var pageHref = "http://www.rehfeld-fotografie.de/#~"+newUrl+"~lang=0~topic="+colorScheme;
		window.setTimeout(function() { 
		    trackPiwik(pageHref,pageTitle); 
		    $("title").html("Rainer Rehfeld Fotografie | "+pageTitle);
		},900);
    });
    
    $('#arrows').after('<div id="newArrows" style="display:none;"></div>');
    $('#newArrows').load(toLoadA,'', function () {
    	loadedA = "true";    	
    });
    
    // blend out old content
    if (colorScheme == "meta") {
   		$("#logo").fadeTo(200, 0);
   	} else {
   		$("#logo").css({width: "0px"});
   	}
    $('#arrows,#frame').stop().fadeTo(200,0);
    
    // longer transition with new color
    if(colorScheme == $("body").attr("class")) {    	
		// load new content and kill old content	
		loaderId = setInterval(function() {
			showNewContent(loaded,loadedA,colorScheme);
				if(delay == 30) { // blend in Loader after 1,5 sec
				    showLoader();
				}
		}, 50);
    } else {    
    	window.setTimeout(function() {   
			changeColor(colorScheme,1200);		
			// load new content and kill old content
			window.setTimeout(function() {	
				loaderId = setInterval(function() {
					showNewContent(loaded,loadedA,colorScheme);
					if(delay == 30) { // blend in Loader after 1,5 sec
						showLoader();
					}
				}, 50);
			},400);
		},100);    
    }	
}  


function showNewContent(loaded,loadedA,colorScheme) {
    if (loaded == "true" && loadedA == "true") {
    	
    	clearInterval(loaderId);
    	hideLoader(); 
    	
    	// get old navi
    	var oldNavi = $('#frame').children("#content").children("#navi");
    	  		 	
    	// adjusting new content     	
    	$('#newFrame').children("#content").children("#navi").remove();
    	$('#newFrame').children("#content").children("#info").after(oldNavi);    	    	
    	
    	// infohover
    	if($('#frame #infolink').hasClass("done")){
    		$('#newFrame #infolink').addClass("done");
    	}
    	    	
    	// remove old content
    	$('#arrows,#frame').empty().remove();
    	
    	// change links 
    	$('#newArrows').attr("id", "arrows");
    	$('#arrowL a,#arrowR a,#thumbs a,').click(function(){	
    		return false;
   		}); 
   		$('#thumbs a').eq(0).addClass("active");
   		 		
    
		// extern links
		$('a[href^="http://"]').attr("target", "_blank");
   		
   		// change id
   		$('#newFrame').attr("id","frame").removeClass("home free prof meta").addClass(colorScheme);
   		naviOut();   		   	
    	
    	// positioning new frame
    	adjustSize($("#frame"),1);
    	window.setTimeout(function() {
    		// logo
   			if (colorScheme == "home" || colorScheme == "meta") {   				
   				$("#logo").hide().css({width: "340px"}).fadeTo(1200, 1, "easeOutQuad");
   			}  
    		$('#frame').fadeTo(1000, 1, "easeInOutQuad"); 
    		window.setTimeout(function() { 				
				$("#info").hover(
					function() {
						infoIn();
					},
					function() {
						infoOut();
					}
				);
				if(colorScheme == "home") {
					slideShowId = setInterval("slideShow()", 4500);
				} else {
					$('#arrows').fadeTo(600,1);
				}    			
    			$('#arrowL a,#arrowR a').click(function(){    
    				nextImage($(this),imgArray);
    			});
    			$('#thumbs a,#imagelink').click(function(){
    				nextImage($(this),imgArray);
    			});				   			 			
    		},400);
    	},200);    		
    	imgPos = 0; 
    	checkText($("#frame"),imgPos);  	
    	
    	// get images and preload
    	imgArray = new Array();    	
    	for(i=0;i<$("#frame .imgArray a").length;i++) {
    		imgArray[i] = $(".imgArray a").eq(i).attr("href");
    	}   	
		preload(imgArray);
        
    } else {
    	delay++;
    }
}

function showLoader() {
	$('body').prepend('<div id="loader"><div></div><div></div><div></div><div></div></div>');
	loader = true;
	loaderAni(0);
}

function hideLoader() {
	$("#loader").fadeOut(300, function(){
		$("#loader").empty().remove();
		loader = false;
    	delay = 0;
	});
}

function loaderAni(i) {
	if(loader) {
		$("#loader div").eq(i).animate({opacity: "1"},200,function() {
			$(this).animate({opacity: "0"},700);
		});	
		if (i<3) {
			i++;
			var time = i*50;
		} else {
			i=0;
			var time = 800;
		}	
		window.setTimeout(function(){
			loaderAni(i);
		}, time);
	}
}


// tracking
function trackPiwik(url,title) {
	window.setTimeout(function() { 
		try {	   
		    piwikTracker.setCustomUrl(url);	   
		    piwikTracker.setDocumentTitle(title);	   
		    piwikTracker.trackPageView();	   
		    piwikTracker.enableLinkTracking();	   		
		}	catch(err) {	   
		    //Piwik funktioniert nicht	   
		}
	}, 900);
}

