$(document).ready(function() {
	/// IMAGE BOX
    imageBoxFunctionalities();
    /// NAVIGATION
    mainNavigationFunctionalities();
    /// SIZE SWITCH
    sizeSwitchFunctionalities();
	/// EVENT GALLERY
    eventGalleryFunctionalities();
	/// EVENT FILTERS
    eventListFilterFunctionalities();
    /// FOCUS DETAIL
    focusDetailFunctionalities();
	/// FANCYBOX GALLERY
    fancyBoxFunctionalities();
	/// SCROLL TOP
    scrollTopFunctionalities();

    addParameterToAvoidCache();

});

var eventListBackLinkText = "";
function setEventListBackLinkText(v){
    eventListBackLinkText = v;
}
function focusDetailFunctionalities(){
    if($.getUrlVar("focusId")!=undefined){
        var $d = $("#"+$.getUrlVar("focusId"));
        if(eventListBackLinkText.length>2){$d.append("<br/><br/><a href='javascript:history.back()'>"+eventListBackLinkText+"</a>");}
        $d.addClass("focusedEvent");
        var t = $d.offset().top;
        $.scrollTo(t-22, 100);
    }
}
function scrollTopFunctionalities(){
    $(window).scroll(function() {
		if($(this).scrollTop() != 0) {
			$('#toTop').fadeIn();
		} else {
			$('#toTop').fadeOut();
		}
	});

	$('#toTop').click(function() {
		$('body,html').animate({scrollTop:0},800);
	});
}
function imageBoxFunctionalities(){
    $("#imgwrapper").mouseenter(function(){
        $(this).css("z-index",50);
    }).mouseleave(function(){
        $(this).css("z-index",20);
    });
}
function mainNavigationFunctionalities(){
    $("#l1Nav ul.sf-menu").superfish();
    $("#l1Nav ul ul li").each(function(){
        var t = $(this);
        if($("ul", t).size()>0){
            $("<span class='sn'>&gt;</span>").appendTo(t);
        }
    });
}

var mkbFontSize = 0;

function sizeSwitchFunctionalities(){
    mkbFontSize = $.cookie("site_size")*1;
    if(mkbFontSize>0){resizeFontStyles();}
    var b = $("#sizeSwitch #big");
    var m = $("#sizeSwitch #medium");
    var s = $("#sizeSwitch #small");
    b.click(function(){switchStyle(2)});
    m.click(function(){switchStyle(1)});
    s.click(function(){switchStyle(0)});
}
function switchStyle(size){
    mkbFontSize = size;
    resizeFontStyles();
    $.cookie("site_size",mkbFontSize,{ expires: 30, path: "/"} );
}
function resizeFontStyles(){
    $('body').css("font-size",13+mkbFontSize+"px");
    var b = $("#sizeSwitch .active").removeClass("active");
    switch(mkbFontSize){
        case 0: $("#sizeSwitch #small").addClass("active"); break;
        case 1: $("#sizeSwitch #medium").addClass("active"); break;
        case 2: $("#sizeSwitch #big").addClass("active"); break;
    }
}


var basePath = "/";
var sitePath = "/";

function setBasePath(bp){
    basePath = bp;
}
function setSitePath(sp){
    sitePath = sp;
    var h = filterHandlingScriptPath+"?ck="+new Date().getTime();
    $.get(h,{'siteHandle': sp});
}


var imgBoxSlideStepTime = 2000;
var imgBoxSlideFadeTime = 800;
function setImgBoxSlideStepTime(t){imgBoxSlideStepTime=t;}
function setImgBoxSlideFadeTime(t){imgBoxSlideFadeTime=t;}
var imgBoxCurrentImage = 0;
var imgBoxHandles = new Array();
var imgBoxTitles = new Array();

function initImgBoxSlideShow(){
    var imgbox = $("#imgwrapper");
    if(imgBoxHandles.length>1){
        $("<img id='box_nextimg' src='"+imgBoxHandles[1]+"' alt='"+imgBoxTitles[1]+"' title='"+imgBoxTitles[1]+"' />").appendTo(imgbox);
        window.setTimeout('showNextImgBoxImage()', imgBoxSlideStepTime);
    }
}
function showNextImgBoxImage(){
    imgBoxCurrentImage++;
    if(imgBoxCurrentImage>=imgBoxHandles.length){imgBoxCurrentImage=0;}
    var ci = $("#box_currentimg");
    var ni = $("#box_nextimg");
    ni.fadeIn(imgBoxSlideFadeTime,function(){
        ni.attr("id","box_currentimg");
        ci.attr("id","box_nextimg").fadeOut(0).attr("src",imgBoxHandles[imgBoxCurrentImage]).attr("alt",imgBoxTitles[imgBoxCurrentImage]).attr("title",imgBoxTitles[imgBoxCurrentImage]);
        window.setTimeout('showNextImgBoxImage()', imgBoxSlideStepTime);
    });
}


/* home */

function homeSetFunctionalities(){
    $('body').append('<div id="visibleHomeBackground"></div>');
    $('body').append('<div id="hiddenHomeBackground"></div>');
    $('#homeStatements').append('<div id="homeStatementsVisibleText"></div>');
    $('#homeStatements').append('<div id="homeStatementsHiddenText"></div>');
    var i = Math.floor(Math.random()*homeEntries.length);
    $("#homeStatementsVisibleText").html(homeEntries[i]);
    $('#visibleHomeBackground').css("background-image", "url('"+homeImages[i] +"')");
    nextHomeSetId = getNextHomeSetId(i);
    $('#hiddenHomeBackground').css("background-image", "url('"+homeImages[nextHomeSetId] +"')");
    $("#homeStatementsHiddenText").html(homeEntries[nextHomeSetId]);
    window.setTimeout('showNextHomeSet()',homeImgesStepTime);
}

function getNextHomeSetId(i){
    var n = Math.floor(Math.random()*homeEntries.length);
    if(n==i){
        do{
            n = Math.floor(Math.random()*homeEntries.length);
        }while(n==i)
    }
    return n;
}
function showNextHomeSet(){
    var vbg = $("#visibleHomeBackground");
    var hbg = $("#hiddenHomeBackground");
    var vstm = $("#homeStatementsVisibleText");
    var hstm = $("#homeStatementsHiddenText");
    if(homeImagesIsCrossFading){
        /* crossfade */
        vstm.fadeOut(homeImagesFadeTime);
        vbg.fadeOut(homeImagesFadeTime,function(){
            displayNextHomeSet();
        });
        hbg.fadeIn(homeImagesFadeTime);
        hstm.fadeIn(homeImagesFadeTime);

    }else{
        /* no crossfade */
        vstm.fadeOut(homeImagesFadeTime);
        vbg.fadeOut(homeImagesFadeTime,function(){
            displayNextHomeSet();
        });

    }
}
function displayNextHomeSet(){
    var vbg = $("#visibleHomeBackground");
    var hbg = $("#hiddenHomeBackground");
    var vstm = $("#homeStatementsVisibleText");
    var hstm = $("#homeStatementsHiddenText");
    vbg.css("background-image", "url('"+homeImages[nextHomeSetId] +"')");
    vstm.html(homeEntries[nextHomeSetId]);
    if(homeImagesIsCrossFading){
        hbg.hide();
        hstm.hide();
        vbg.show();
        vstm.show();
        prepareNextHomeSet();
    }else{
        vstm.fadeIn(homeImagesFadeTime);
        vbg.fadeIn(homeImagesFadeTime,function(){
            prepareNextHomeSet();
        });
    }

}
function prepareNextHomeSet(){
    nextHomeSetId = getNextHomeSetId(nextHomeSetId);
    $('#hiddenHomeBackground').css("background-image", "url('"+homeImages[nextHomeSetId] +"')");
    $("#homeStatementsHiddenText").html(homeEntries[nextHomeSetId]);
    window.setTimeout('showNextHomeSet()',homeImgesStepTime);
}



/* helper methods */


function eventListFilterFunctionalities(){
    $(".filterswitch").click(function(){
        var t = $(this);
        var n = t.attr("id");
        var pn = n.substring(0,n.lastIndexOf("_"));
        pn = pn.substring(pn.lastIndexOf("_")+1);
        n = n.substring(n.length-1);
        var sel = t.parent().find($(".eventtype_"+n));
        t.hasClass();
        if(t.hasClass("switch_on")){
//            sel.fadeOut();
            sel.slideUp(500, function(){checkVisibleEventEntries(pn)});
        }else{
            sel.slideDown(500, function(){checkVisibleEventEntries(pn)});
//            sel.fadeIn();
        }
        t.toggleClass("switch_on").toggleClass("switch_off");
    });
}
function checkVisibleEventEntries(pn){
    var c = $("#el_"+pn).find($('.eventListEntry:visible')).length;
    if(c==0){
        $("#el_"+pn).find(".feedback_filter").slideDown();
    }else{
        $("#el_"+pn).find(".feedback_filter").slideUp();

    }
}
function eventGalleryFunctionalities(){
    $(".eventGallery a").fancybox({
        'overlayColor'		: '#fff',
        'margin'       : 0,
        'padding'       : 0,
        'width'         : 600,
        'height'        : 400,
        'autoScale'        : false,
        'hideOnContentClick' : true,
        'showCloseButton'    : false,
        'autoDimensions'     : false,
        'titleFormat': formatInlineGalleryTitle,
        'overlayOpacity'	: 0.5
    });
}
function formatInlineGalleryTitle(title, currentArray, currentIndex, currentOpts) {
//    document.title = "";
    $("#collection_thumbnav img").each(function(){
        var t = $(this);
        var n = t.attr("id").substring(2);
        if(n==currentIndex){$(this).addClass("active");}else{$(this).removeClass("active");}
//        document.title += n +"/"+currentIndex+" ... ";

    });
    return (title && title.length ?  '<div id="currentImageTitle">' + title + '</div>' : '' );
}

function fancyBoxFunctionalities(){
    $(".gallery a, a.fancyBox").fancybox({
        'overlayColor'		: '#7b7c7e',
        'overlayOpacity'	: 0.75,
        'titlePosition' 	: 'over'
    });
}

function addParameterToAvoidCache(){
/* append url parameter to avoid mgnl cache */
    $(".kclink").each(function(){
        var $l = $(this).attr("href");
        if($l.indexOf("?")==-1){
            $l+="?";
        }else{
            $l+="&";
        }
        $l+="kc"+new Date().getTime();
        $(this).attr("href", $l)
    });
}

$.extend({
  getUrlVars: function(){
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
      hash = hashes[i].split('=');
      vars.push(hash[0]);
      vars[hash[0]] = hash[1];
    }
    return vars;
  },
  getUrlVar: function(name){
    return $.getUrlVars()[name];
  }
});
/* decryp addresses*/
function dcmadr(nnnn){
    var a = "";
    for(i=0,m=nnnn.length;i < m;i++){
        if(i%3==0){
            a += String.fromCharCode(nnnn.substr(i, 3));
        }
    }
    location.href=(a);
}

