// Started out nice and small, but has grown into a ugly monster...

function contentFadeOut() {
    $(".textbox").fadeOut();
}

function shrinkFrame() {
	$("#frame").animate(
		{ width: "800px", left: "-303px", top: "-295px" }, 
		500,
		"linear",
		function() {
			setHeaderBannerZIndex(0);
		}
	);
	$("#flags").animate({ left: "70px" });
}
function growFrame(lang, origHomeWidth, origHomeLeft, origHomeTop) {
	$("#frame").animate(
		{ width: "1100px", left: "-64px", top: "0" },
		500,
		"linear",
		function() {
			var box_home = $("#box_"+lang+"_home");
			// First ensure that box is at correct placement (some browsers manipulate left/top after fadeOut)
			box_home.css({ width: origHomeWidth+"px", left: origHomeLeft, top: origHomeTop });
			// Now fade in
			box_home.fadeIn();
		}
	);
	$("#logo").animate({ width: "267px", height: "107px", right: "561px", top: "125px" });
	$("#flags").animate({ left: "173px" });
	setHeaderBannerZIndex(1);
}

function setHeaderBannerZIndex(index) {
	if (!(jQuery.browser.msie && jQuery.browser.version < 8)) {
		$("#header").css("z-index", index);
	}
}

function logoToRight() {
	$("#logo").animate({ width: "200px", height: "80px", right: "200px", top: "125px" });
}

function showFooter(top) {
	$("#footer:hidden").css({ top: top+"px"}).fadeIn();  // Not displayed => First position, then show
	$("#footer").animate({ top: top+"px"});
}

function swapMenuLanguage(languageCode) {
	$("#nav>li").each(function(){
		$(this).hide();
	});
	$("#nav>.nav-"+languageCode).each(function(){
		$(this).show();
	});

	// Make flag glow
	$("#flags img.current").removeClass("current");  // Reset
	$("#flags img."+languageCode).addClass("current");
}

function getLanguage() {
	var hash = window.location.hash;
	hash = hash.replace(/^#/, '');
	if (hash.substring(0, 3)==='en_') {
		// English mode
		return 'en';
	} else {
		// Swedish mode
		return 'sv';
	}
}

function effectuateDeepLinking() {
	// Deep linking
	var hash = window.location.hash;
	hash = hash.replace(/^#/, '');
	switch (hash) {
	    case 'sv_orginal':
	    case 'en_orginal':
	    case 'sv_grafiskablad':
	    case 'en_grafiskablad':
	    case 'sv_biografi':
	    case 'en_biografi':
	    case 'sv_pressklipp':
	    case 'en_pressklipp':
	    case 'sv_kontakt':
	    case 'en_kontakt':
	        $("#nav_" + hash ).trigger("click");
	        break;
		default:
			setHeaderBannerZIndex(1);
	}
}


var $nav, $lang;

$.fn.animationClick = function(callback, target, fetchWithAjax) {

    var boxTarget = target ? $('#box_'+target) : null;
    var stringTarget = target ? target : null;
	
    return this.click(function(e) {
        
        if (!$(this).hasClass("current")) {
                
			$nav.removeClass("current");
            $(this).addClass("current");
            
            contentFadeOut();
            shrinkFrame();
			
            callback();

			if (boxTarget) {
				if (fetchWithAjax) {
					// Fetch the content first
					$.get('ajax/'+target+'.php', function(data) {

						boxTarget.html(data);
						boxTarget.fadeIn();
						showFooter(boxTarget.offset().top + boxTarget.height() + 50);
						//$(".image_gallery a").slimbox();
					});
				} else {
					// Just show elements allready in HTML
					boxTarget.fadeIn();
					showFooter(boxTarget.offset().top + boxTarget.height() + 50);			
				}
			}

			window.location.hash = stringTarget;
            
        }
        
        return false;
                
    });
    
}


function bootstrapGallery() {
	// Config (now supports language-specific graphics) - TODO Make language-specific graphics
	var config = {
		fixedNavigation:true,
		imageLoading: 'css/lightbox-ico-loading.gif',
		imageBtnClose: 'css/lightbox-btn-close.gif',
		imageBtnPrev: 'css/lightbox-btn-prev.gif',
		imageBtnNext: 'css/lightbox-btn-next.gif',
		imageBlank: 'css/lightbox-blank.gif',
		txtImage: '',
		txtOf: '/'
	};
	var configSv = config;
	configSv.imageBtnClose = 'css/lightbox-btn-close.gif';
	configSv.imageBtnPrev = 'css/lightbox-btn-next.gif';
	configSv.imageBtnNext = 'css/lightbox-btn-prev.gif';
	
	// Lightbox groups
	$('#box_sv_orginal .image_gallery a[rel*=lightbox]').lightBox(configSv);
	$('#box_en_orginal .image_gallery a[rel*=lightbox]').lightBox(config);
	$('#box_sv_grafiskablad .image_gallery a[rel*=lightbox]').lightBox(configSv);
	$('#box_en_grafiskablad .image_gallery a[rel*=lightbox]').lightBox(config);
	
	// Add hover-opacity-effect to gallary
	/*var dimmedOpacity = 0.67;
	$(".pic_frame").css({opacity: dimmedOpacity}).hover(
		function() {
			$(this).animate({opacity: 1}, 'fast')
		}, 
		function () {
			$(this).animate({opacity: dimmedOpacity}, 'fast')
		}
	);*/
	
}

jQuery(function() {
	
	$nav = $("#nav a");
	$lang = getLanguage();
	
	// Ensure correct menu
	swapMenuLanguage($lang);
	$(".welcome_box").hide();
	$("#box_"+$lang+"_home").show();
	
	var $welcomeBox = $("#box_"+$lang+"_home");
    
    var origHomeWidth = 300, // or could test after window.load
        origHomeLeft = $welcomeBox.position().left,
        origHomeTop = $welcomeBox.position().top;

	// Setup logo
	$("#logo").click(function(e) {
	    var langCode = getLanguage();
	    if (!$("#nav_"+langCode+"_home").hasClass("current")) {
	        $("#nav a").removeClass("current");
	        $("#nav_"+langCode+"_home").addClass("current");
			$("#footer").fadeOut();
			swapMenuLanguage(langCode);
	        contentFadeOut();
			growFrame(langCode, origHomeWidth, origHomeLeft, origHomeTop);
	    }
	});

	// Setup Swedish
	$(".nav_sv_home").click(function(e) {
        if (!$(this).hasClass("current")) {
            $("#nav a").removeClass("current");
            $("#nav_sv_home").addClass("current");
			$("#footer").fadeOut();
			swapMenuLanguage('sv');
            contentFadeOut();
			growFrame('sv', origHomeWidth, origHomeLeft, origHomeTop);
        }
    });
		
	$("#nav_sv_orginal").animationClick(function() {
        logoToRight();
    }, "sv_orginal", false);
    
	$("#nav_sv_grafiskablad").animationClick(function() {
        logoToRight();
    }, "sv_grafiskablad", false);

	$("#nav_sv_biografi").animationClick(function() {
        logoToRight();
    }, "sv_biografi", false);
	
	$("#nav_sv_pressklipp").animationClick(function() {
        logoToRight();
    }, "sv_pressklipp", false);

	$("#nav_sv_kontakt").animationClick(function() {
        logoToRight();
    }, "sv_kontakt", false);


	// Setup English
	$(".nav_en_home").click(function(e) {
        if (!$(this).hasClass("current")) {
            $("#nav a").removeClass("current");
            $("#nav_en_home").addClass("current");
			$("#footer").fadeOut();
			swapMenuLanguage('en');
            contentFadeOut();
			growFrame('en', origHomeWidth, origHomeLeft, origHomeTop);
        }
    });
		
	$("#nav_en_orginal").animationClick(function() {
        logoToRight();
    }, "en_orginal", false);

	$("#nav_en_grafiskablad").animationClick(function() {
        logoToRight();
    }, "en_grafiskablad", false);

	$("#nav_en_biografi").animationClick(function() {
	    logoToRight();
	}, "en_biografi", false);

	$("#nav_en_pressklipp").animationClick(function() {
        logoToRight();
    }, "en_pressklipp", false);

	$("#nav_en_kontakt").animationClick(function() {
        logoToRight();
    }, "en_kontakt", false);


	// Deep linking
	effectuateDeepLinking();
	
	bootstrapGallery();
	
});



