$(document).ready(function(){
	
	// Maintenance mode: To enable it, uncomment the code on styles/www.tefaf.com.css
	if (getUrlVarsFrom(document.location.href, false)['testmode'] == '1') {
		setTimeout("maintenanceMode()",500);
		
	}
	
	
	initWebsite();
	
	$('.inner-gallery').fancybox({
		
		'centerOnScroll' : true,
		'titleShow' : true,
		'showNavArrows' : true,
		
	});
	
});

function initWebsite() {
	highlightCurrent();
	if($('a').length > 0 ) 
	$('a').smoothScroll();
	
	
	
	
	/*$('ul#bottomNav a').each(function(index, domEle){
		//$('.top').html('ul#bottomNav a[href=' + $(domEle).attr('href') + ']');
		if ( $(domEle).attr('href').substring(0,1) == "#")
		
		//$(domEle).css('border', '1px solid #333')
		$('ul#bottomNav a[href=' + $(domEle).attr('href') + ']').smoothScroll();
		
		
		})*/
	
	
	switch (getUrlVarsFrom(document.location.href, true)['tabid']) {
    case '19':
        /* Intro Page */
        initIntro();
        break;
    }
	
	
	$(window).load(function(){
		if($('.content').length > 0 ) {
			contentHeight = $('.content').height();
			setContentHeight();
			$(window).resize(function() {
				setContentHeight();
			});
		}
			
	
	})
	
}


// When on maintenance mode, enable visibility when the url var testmode == 1
function maintenanceMode() {
	
	document.styleSheets[0].disabled = true;
	$('a').each(function(index,domEle){
		
		if ($(domEle).attr('href') != null && $(domEle).attr('href').indexOf('javascript') == -1)
		
		if ($(domEle).attr('href').indexOf('?')==-1) {
			$(domEle).attr('href', $(domEle).attr('href') + '?testmode=1');
		} else {
			$(domEle).attr('href', $(domEle).attr('href') + '&testmode=1')
		}
	})
	
	
setTimeout("initWebsite()",1000);
}

function getUrlVarsFrom(url, preventUnwantedVars) {
    var vars = [],
        hash;
    var hashes = url.slice(url.indexOf('?') + 1).split('&');
    for (var i = 0; i < hashes.length; i++) {
        hash = hashes[i].split('=');
        if (preventUnwantedVars == 1) {
            if (hash[0] != 'lg' && hash[0] != 'objectid' && hash[0] != 'tabindex' && hash[0] != 'page' && hash[0] != 'keyword' && hash[0] != 'mediaid') {
                if (hash[0] == 'categoryid' && hash[1] == 0) {} else {
                    vars.push(hash[0]);
                    vars[hash[0]] = hash[1];
                }
            }
        } else {
            vars.push(hash[0]);
            vars[hash[0]] = hash[1];
        }
    }
    return vars;
}


var contentHeight;
function setContentHeight() {
	var headerHeight;
	$('#TopPane .top').length > 0 ? headerHeight = 225 : headerHeight = 0;
	if ($(window).height() - headerHeight > contentHeight)
	if ($('.content').length > 0) $('.content').css('height', $(window).height() - headerHeight + 'px');
}


/*!
* jQuery Smooth Scroll Plugin v1.3
*
* Date: Wed Dec 1 15:03:21 2010 -0500
* Requires: jQuery v1.3+
*
* Copyright 2010, Karl Swedberg
* Dual licensed under the MIT and GPL licenses (just like jQuery):
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*
*
*
*
*/
(function(b){function k(e){var c=[],a=false;this.each(function(){if(!(this==document||this==window)){var d=b(this);if(d.scrollTop()>0)c.push(this);else{d.scrollTop(1);a=d.scrollTop()>0;d.scrollTop(0);a&&c.push(this)}}});if(e==="first"&&c.length)c=[c.shift()];return c}function l(e){return e.replace(/^\//,"").replace(/(index|default).[a-zA-Z]{3,4}$/,"").replace(/\/$/,"")}var m=l(location.pathname);b.fn.extend({scrollable:function(){return this.pushStack(k.call(this))},firstScrollable:function(){return this.pushStack(k.call(this,
"first"))},smoothScroll:function(e){e=e||{};var c=b.extend({},b.fn.smoothScroll.defaults,e);this.die("click.smoothscroll").live("click.smoothscroll",function(a){var d=b(this),f=location.hostname===this.hostname||!this.hostname,g=c.scrollTarget||(l(this.pathname)||m)===m,i=this.hash,h=true;if(!c.scrollTarget&&(!f||!g||!i))h=false;else{f=c.exclude;g=0;for(var j=f.length;h&&g<j;)if(d.is(f[g++]))h=false;f=c.excludeWithin;g=0;for(j=f.length;h&&g<j;)if(d.closest(f[g++]).length)h=false}if(h){c.scrollTarget=
e.scrollTarget||i;c.link=this;a.preventDefault();b.smoothScroll(c)}});return this}});b.smoothScroll=function(e,c){var a,d,f,g=0;d="offset";var i={};if(typeof e==="number"){a=b.fn.smoothScroll.defaults;f=e}else{a=b.extend({},b.fn.smoothScroll.defaults,e||{});if(a.scrollElement){d="position";a.scrollElement.css("position")=="static"&&a.scrollElement.css("position","relative")}f=c||b(a.scrollTarget)[d]()&&b(a.scrollTarget)[d]()[a.direction]||0}a=b.extend({link:null},a);if(a.scrollElement){d=a.scrollElement;
g=d.scrollTop()}else d=b("html, body").firstScrollable();i["scroll"+{top:"Top",left:"Left"}[a.direction]]=f+g+a.offset;d.animate(i,{duration:a.speed,easing:a.easing,complete:function(){a.afterScroll&&b.isFunction(a.afterScroll)&&a.afterScroll.call(a.link,a)}})};b.smoothScroll.version="1.3.1";b.fn.smoothScroll.defaults={exclude:[],excludeWithin:[],offset:0,direction:"top",scrollElement:null,scrollTarget:null,afterScroll:null,easing:"swing",speed:400}})(jQuery);



//MENU FUNCTIONS

function nrKeys(a) {
    var i = 0;
    for (key in a) {
        i++;
    }
    return i;
}

function compareAssociativeArrays(a, b) {
    if (a == b) {
        return true;
    }
    if (nrKeys(a) != nrKeys(b)) {
        return false;
    }
    for (key in a) {
        if (a[key] != b[key]) {
            return false;
        }
    }
    return true;
}




function handleLanguageMenu() {

    $("ul.langMenu > li > a").each(function (index, domEle) {
        var currentLg = getUrlVarsFrom(window.location.href, false)['lg'],
            newLg = getUrlVarsFrom($(domEle).attr('href'), false)['lg'];
        if (currentLg != newLg) {

            if (currentLg === undefined) {
                if (getUrlVarsFrom(window.location.href, false).length > 1) {
                    $(domEle).bind('click', function () {
                        window.location = window.location.href + '&lg=' + newLg;
                        return false;
                    })
                }
            } else {
                $(domEle).bind('click', function () {
                    window.location = window.location.href.replace('lg=' + currentLg, 'lg=' + newLg);
                    return false;
                })
            }

        } else {
            $(domEle).bind('click', function () {
                window.location = window.location.href;
                return false;
            })

        }

    })

}


function highlightCurrent() {
    //check for matches on menu
    $("ul.mainMenu a").each(function (index, domEle) {
        if (compareAssociativeArrays(getUrlVarsFrom(window.location.href, 1), getUrlVarsFrom($(domEle).attr('href'), 1))) {
            $(domEle).addClass('active');
        }
    })


}


/*Intro Slideshow
*/
	var currentImage;
    var currentIndex = -1;
    var interval;
	var myTimer;
	
    function showImage(index){
        if(index < $('#slideshow div').length){
        	var indexImage = $('#slideshow div')[index]
			$(indexImage).css({'display':'block', 'opacity':1, 'z-index':1});
            if(currentImage){   
            	if(currentImage != indexImage ){
                    $(currentImage).css({'z-index':2});
                    
                    $(currentImage).fadeOut(1500, function() {
						
					    $(this).css({'display':'none','z-index':1})
					});
                }
            }
            
            currentImage = indexImage;
            currentIndex = index;
            //$('#thumbs li').removeClass('active');
            //$($('#thumbs li')[index]).addClass('active');
        }
    }
    
    function showNext(){
        var len = $('#slideshow div').length;
        var next = currentIndex < (len-1) ? currentIndex + 1 : 0;
		clearTimeout(myTimer);
    	myTimer = setTimeout("showNext()", 10000);
        showImage(next);
		
    }
    
    
    
    function initIntro() {
	    myTimer = setTimeout("showNext()", 4000);
		showNext(); //loads first image
      
		
		$('.menu ul li a').each(function (index, domEle) {
                $(domEle).hover(function () {
                    
					clearTimeout(myTimer);
					
                    showImage($(domEle).parent().index());

                }, function () {
					
   					myTimer = setTimeout("showNext()", 4000);
					
					})
		})
	}

/*End intro slideshow
*/


