﻿function getUrlVarsFrom(url, preventUnwantedVars) {
    var vars = [],
        hash;
    var hashes = url.slice(url.indexOf('?') + 1).split('#')[0].split('&');
    for (var i = 0; i < hashes.length; i++) {
        hash = hashes[i].split('=');
		if (preventUnwantedVars) {
			if (hash[0] != 'lg' && hash[0] != 'objectid' && hash[0] != 'tabindex' && hash[0] != 'page' && hash[0] != 'keyword'  && hash[0] != 'mediaid' && hash[0] != 'switchportal') {
        		vars.push(hash[0]);
        		vars[hash[0]] = hash[1];
			}
		} else {
			vars.push(hash[0]);
        	vars[hash[0]] = hash[1];
		}
    }
    return vars;
}


//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;
}




// Highlight current

function highlightCurrent() {

    $("ul.subnav a").each(function (index, domEle) {

        if (compareAssociativeArrays(getUrlVarsFrom(window.location.href, true), getUrlVarsFrom($(domEle).attr('href'), true))) {

            $(domEle).parents('ul.subnav li').children('a').addClass('active');
            var contId = $(domEle).parents('div.ui-tabs-panel').attr('id');
            menuTab = parseInt(contId.charAt(contId.length - 1)) - 1;
         
        
        }



    });

}


function setupMenu() {
	/**/
	$('div#tabs > ul > li > a').each(function(index,domEle){
	//    var gurl=$(domEle).attr('href');   
	
		$(domEle).attr('href','#tabs-' + (parseFloat(index) + 1))
			
		//alert(domEle);
		
		})

    // Initialize JQueryUI Tabs
    $("div#tabs").tabs({
        event: 'mouseover',select: function (event, ui) {
         
		    var url = $.data(ui.tab, 'load.tabs');
		   // alert(url);
            if (url) {
                location.href = url;
                return false;
            }
            return true;
		  /* */
		  
		  
        }

    });

  //  currentTabId = getUrlVarsFrom(window.location.href, true);//['tabid'];
  
  
  // Main menu links
$('div#tabs ul li a[href="#tabs-1"]').click(function () {
window.location = "index.php";
return false;
})
$('div#tabs ul li a[href="#tabs-2"]').click(function () {
window.location = "Introduction.php";
})
$('div#tabs ul li a[href="#tabs-3"]').click(function () {
window.location = "Dates_times.php";
})
$('div#tabs ul li a[href="#tabs-4"]').click(function () {
window.location = "http://www.tefaf.com/DesktopDefault.aspx?tabid=17";
})
$('div#tabs ul li a[href="#tabs-5"]').click(function () {
window.location = "http://www.tefaf.com/DesktopDefault.aspx?tabid=102";
})
$('div#tabs ul li a[href="#tabs-6"]').click(function () {
window.location = "news.php?cid=5";
})
$('div#tabs ul li a[href="#tabs-7"]').click(function () {
window.location = "news.php?cid=9";
})
$('div#tabs ul li a[href="#tabs-8"]').click(function () {
window.location = "contact.php";
})

  
  
  
 highlightCurrent();
/*

   

    if (!menuTab) {
        if (currentTabId == '1') menuTab = 0;
        if (currentTabId == '102') menuTab = 4;
		if (currentTabId == '34') menuTab = 5;
		if (currentTabId == '78') menuTab = 6;
		if (currentTabId == '31') menuTab = 7;
    }
	*/
    if (!menuTab) {
        menuTab = 0;
    }

    $("div#tabs").tabs('select', menuTab);

    var timeout;

    $('div#tabs a').mouseenter(function () {
        if (timeout) clearTimeout(timeout);
    })

    $('div#tabs').mouseleave(function () {
        timeout = setTimeout('$("div#tabs").tabs("select", menuTab)', 800);
    });
/*
    $('.noLink').click(

    function () {
        return false;

    });*/


}

/*
function handlePrintAndEmailLink() {
    $('a.optionLink').each(function (index, domEle) {
        $(domEle).attr('onclick', '');
        $(domEle).attr('href', '');
        switch ($(domEle).html()) {
        case 'Print':
            $(domEle).bind('click', function (e) {
                e.preventDefault();

                javascript: window.print();
                return false;
            })
            break;
        case 'Email':
            $(domEle).bind('click', function () {
                mail_str = "mailto:?subject= Check this link --> " + document.title;
                mail_str += "&body= " + document.title;
                mail_str += "... at: " + location.href;
                location.href = mail_str;
                return false;
            })
            break;
        }

    })

}
*/

/* Search Functions */

function submitQuickSearch() {
    var keyWords = document.getElementById('inpQuickSearch').value;
    document.location.href = document.location.href.split('?')[0] + '?tabid=82&keyword=' + keyWords;
}

function submitCategory() {
    var url = document.getElementById('selCategory').value;
    document.location.href = document.location.href.substr(0, document.location.href.indexOf('DesktopDefault.aspx')) + url;
}

function entsub(e) {
    var evt = window.event ? e.keyCode : e.which;
    var keyPressed = evt;

    if (keyPressed == 13) {
        submitQuickSearch();
        return false;
    } else {
        return true;
    }
}

/*
function handlePlaceHolderText() {
    $('input[type=text]').focus(function () {
        if ($(this).val() == $(this).attr('defaultValue')) {
            $(this).val('');

        }
    });

    $('input[type=text]').blur(function () {
        if ($(this).val() == '') {
            $(this).val($(this).attr('defaultValue'));

        }
    });


    $('textarea').focus(function () {
        if ($(this).val() == $(this).attr('defaultValue')) {
            $(this).val('');

        }
    });

    $('textarea').blur(function () {
        if ($(this).val() == '') {
            $(this).val($(this).attr('defaultValue'));

        }
    });

}
*/
function fixSector(){
	
	
	$('table.sectors img').each(function (index, domEle) {
     	if ($(domEle).attr('src').indexOf('gif') == -1 && $(domEle).attr('src').indexOf('jpg') == -1 && $(domEle).attr('src').indexOf('png') == -1) {
			
			$(domEle).remove();
			
        }
    });
	
	
	$('table.sectors span.text_mainpages').each(function (index, domEle) {

        if ($(domEle).text().substr(0, 5) == 'TEFAF') {
			
			$(domEle).attr('style', 'display:inline');
			$(domEle).append(' / ');
            $(domEle).prependTo('p#standLocation');
			
        }



    });
	
	
} 


$(document).ready(function () {
  //  handlePlaceHolderText();
    setupMenu();
    //handlePrintAndEmailLink();
	swap();
	/*
		switch(getUrlVarsFrom(window.location.href, false)['tabid']) {
		case '10':
			fixSector();
			break;
		}
		*/
})



/***********************************************
 * Dynamic Countdown script- &#169; Dynamic Drive (http://www.dynamicdrive.com)
 * This notice MUST stay intact for legal use
 * Visit http://www.dynamicdrive.com/ for this script and 100s more.
 ***********************************************/

function cdtime(container, targetdate) {
    if (!document.getElementById || !document.getElementById(container)) return;
    this.container = document.getElementById(container);
    this.currentTime = new Date();
    this.targetdate = new Date(targetdate);
    this.timesup = false;
    this.updateTime();
}
cdtime.prototype.updateTime = function () {
    var thisobj = this;
    this.currentTime.setSeconds(this.currentTime.getSeconds() + 1);
    setTimeout(function () {
        thisobj.updateTime()
    }, 1000); //update time every second
}
cdtime.prototype.displaycountdown = function (baseunit, functionref) {
    this.baseunit = baseunit;
    this.formatresults = functionref;
    this.showresults();
}
cdtime.prototype.showresults = function () {
    var thisobj = this;
    var timediff = (this.targetdate - this.currentTime) / 1000 //difference btw target date and current date, in seconds;
    if (timediff < 0) { //if time is up
        this.timesup = true;
        this.container.innerHTML = this.formatresults();
        return;
    }
    var oneMinute = 60; //minute unit in seconds
    var oneHour = 60 * 60; //hour unit in seconds
    var oneDay = 60 * 60 * 24; //day unit in seconds
    var dayfield = Math.floor(timediff / oneDay);
    var hourfield = Math.floor((timediff - dayfield * oneDay) / oneHour);
    var minutefield = Math.floor((timediff - dayfield * oneDay - hourfield * oneHour) / oneMinute);
    var secondfield = Math.floor((timediff - dayfield * oneDay - hourfield * oneHour - minutefield * oneMinute));
    if (this.baseunit == "hours") { //if base unit is hours, set "hourfield" to be topmost level
        hourfield = dayfield * 24 + hourfield;
        dayfield = "n/a";
    }
    else if (this.baseunit == "minutes") { //if base unit is minutes, set "minutefield" to be topmost level
        minutefield = dayfield * 24 * 60 + hourfield * 60 + minutefield;
        dayfield = hourfield = "n/a";
    }
    else if (this.baseunit == "seconds") { //if base unit is seconds, set "secondfield" to be topmost level
        var secondfield = timediff;
        dayfield = hourfield = minutefield = "n/a";
    }
    this.container.innerHTML = this.formatresults(dayfield, hourfield, minutefield, secondfield);
    setTimeout(function () {
        thisobj.showresults()
    }, 1000); //update results every second
}

function formatresults() {
    if (this.timesup == false) { //if target date/time not yet met
        //var displaystring = "<span class='text_small_white3'>The 2012 Fair will open in " + arguments[0] + " days " + arguments[1] + " hours " + arguments[2] + " minutes " + arguments[3] + " seconds</span>";
		var displaystring = "<span class='text_small_white3'>2012年博览会将在" + arguments[0] + "天" + arguments[1] + "小时" + arguments[2] + "分" + arguments[3] + "秒</span>";
		//2012年博览会将在194天18小时0分55秒The 2012 Fair will open in 194 days 18 hours 0 minutes 55 seconds
    }
    else { //else if target date/time met
        var displaystring = ""; //Don't display any text
        //alert("The 2010 Edition of TEFAF Maastricht has ended!"); //Instead, perform a custom alert
    }
    return displaystring;
}





function MM_swapImgRestore() { //v2.0

  if (document.MM_swapImgData != null)

    for (var i=0; i<(document.MM_swapImgData.length-1); i+=2)

      document.MM_swapImgData[i].src = document.MM_swapImgData[i+1];

}

 

function MM_preloadImages() { //v2.0

  if (document.images) {

    var imgFiles = MM_preloadImages.arguments;

    if (document.preloadArray==null) document.preloadArray = new Array();

    var i = document.preloadArray.length;

    with (document) for (var j=0; j<imgFiles.length; j++) if (imgFiles[j].charAt(0)!="#"){

      preloadArray[i] = new Image;

      preloadArray[i++].src = imgFiles[j];

  } }

}
 

function MM_swapImage() { //v2.0

  var i,j=0,objStr,obj,swapArray=new Array,oldArray=document.MM_swapImgData;

  for (i=0; i < (MM_swapImage.arguments.length-2); i+=3) {

    objStr = MM_swapImage.arguments[(navigator.appName == 'Netscape')?i:i+1];

    if ((objStr.indexOf('document.layers[')==0 && document.layers==null) ||

        (objStr.indexOf('document.all[')   ==0 && document.all   ==null))

      objStr = 'document'+objStr.substring(objStr.lastIndexOf('.'),objStr.length);

    obj = eval(objStr);

    if (obj != null) {

      swapArray[j++] = obj;

      swapArray[j++] = (oldArray==null || oldArray[j-1]!=obj)?obj.src:oldArray[j];

      obj.src = MM_swapImage.arguments[i+2];

  } }

  document.MM_swapImgData = swapArray; //used for restore

}


function swap() {


    var imageName = 'header-' + (Math.floor(Math.random() * 3) + 1) + '.png';
    $('#header').css('background', 'url("http://www.tefaf.com/media/tefaf-2011-media/image/background/header-banner/' + imageName + '") no-repeat scroll left top transparent');


	/*
    var numImages = 4;
    rndImg = new Array("/img/header_Tefaf_2011_1.jpg", "/img/header_Tefaf_2011_2.jpg", "/img/header_Tefaf_2011_3.jpg", "/img/header_Tefaf_2011_4.jpg");
    x = (Math.floor(Math.random() * numImages));
    randomImage = (rndImg[x]);
    //document.getElementById("TopPane").style.background = "url(" + randomImage + ") no-repeat";
    document.getElementById("header").style.background = "url(http://www.tefaf.com/media/tefaf-2011-media/image/background/header-banner/header-3.png) no-repeat";
    //$('#TopPane').css('background','url("media/Tefaf_2011/header_Tefaf_2011_1.png")');   
	*/
}


// FONT SIZE FUNCTIONS

function fontsizeup() {
  active = getActiveStyleSheet();
  switch (active) {
    case 'A-' : 
      setActiveStyleSheet('A');
      break;
    case 'A' : 
      setActiveStyleSheet('A+');
      break;
    case 'A+' : 
      break;
    default :
      setActiveStyleSheet('A-');
      break;
  }
}

function fontsizedown() {
  active = getActiveStyleSheet();
  switch (active) { 
    case 'A+' : 
      setActiveStyleSheet('A');
      break;
    case 'A' : 
      setActiveStyleSheet('A-');
      break;
    case 'A-' : 
       break;
    default :
      setActiveStyleSheet('A-');
      break;
  }
}

function setActiveStyleSheet(title) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
}

function getActiveStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
  }
  return null;
}

function getPreferredStyleSheet() {
  return ('A-');
}

function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

window.onload = function(e) {
  var cookie = readCookie("style");
  var title = cookie ? cookie : getPreferredStyleSheet();
  setActiveStyleSheet(title);
}

window.onunload = function(e) {
  var title = getActiveStyleSheet();
  createCookie("style", title, 365);
}

var cookie = readCookie("style");
var title = cookie ? cookie : getPreferredStyleSheet();
if (title == 'null') {
  title = getPreferredStyleSheet();
}

setActiveStyleSheet(title);

function MM_openBrWindow(theURL,winName,features) { //v2.0
window.open(theURL,winName,features);
} 

