// JavaScript Document
//var popupLink;

$(document).ready(function() {
    Menu('menu');
    sideMenuHover();
    $('.openPopup').click(function() { showPopup($(this), null); return false; });
    $('#closePopup').click(function() { hidePopup(); return false; });

    //$('.seriesLink').click(function() { showBestelPopup(); return false; });
    $('.closeForm').click(function() { hidePopup(); return false; });
});

function doCufon() {
    Cufon.replace('h1', { fontFamily: 'Dax', hover: true });
    Cufon.replace('h2', { fontFamily: 'Dax', hover: true });
    Cufon.replace('#h2 h2', { fontFamily: 'Dax', fontWeight:'bold'});
    Cufon.replace('h4', { fontFamily: 'Dax', hover: true });
    Cufon.replace('#headerMenuLeft a', { fontFamily: 'Dax', hover: true });
    Cufon.replace('#headerMenuRight a', { fontFamily: 'Dax', hover: true });
    Cufon.replace('#menu a', { fontFamily: 'Dax', hover: true });
    Cufon.replace('#cufonLinks a', { fontFamily: 'Dax', hover: true });
    Cufon.replace('#cufonI', { fontFamily: 'Dax', hover: true });    
    Cufon.replace('#filter', { fontFamily: 'Dax', hover: true });
    Cufon.replace('.contentImageBlock div.text h2, .contentImageBlock div.text h3', { fontFamily: 'Dax', hover: true, textShadow: '	#333 1px 1px' });
}

function browserType() {
    var agent = navigator.userAgent;
    var browser;
    if (agent.lastIndexOf('MSIE') != -1) browser = 'MSIE';
    else if (agent.lastIndexOf('Firefox') != -1) browser = 'Firefox';
    else if (agent.lastIndexOf('Safari') != -1) browser = 'Safari';
    else if (agent.lastIndexOf('Opera') != -1) browser = 'Opera';
    else browser = 'Unknown';
    return browser;
}

function versionMSIE() {
    var agent = navigator.userAgent;
    var version;
    if (agent.lastIndexOf('MSIE') != -1) {
        if (agent.lastIndexOf('5.0') != -1) version = 5;
        if (agent.lastIndexOf('5.5') != -1) version = 55;
        if (agent.lastIndexOf('6.0') != -1) version = 6;
        if (agent.lastIndexOf('7.0') != -1) version = 7;
        if (agent.lastIndexOf('8.0') != -1) version = 8;
    };
    return version;
}

function checkSearch(obj, startValue) {    
	if (obj.value == startValue) {
		obj.value = '';
		obj.className = 'trueColor';
		return;
	}
	if (obj.value == '') {
		obj.value = startValue;
		obj.className = '';
		return;
	}
	obj.className = 'trueColor';
}

function checkTextarea(obj, startValue) {        
    if ($(obj).text() == startValue) {
        $(obj).text('');
        return;
    }    
    if (!(browserType() == 'Safari') && $(obj).text() == '') {
        $(obj).text(startValue);
        return;
    }
}

function removePasswordSrtring(obj, startValue) {
    if (obj.value == '') {
        $('#password2').focus();
        $('#password2').css('visibility', 'visible');
        obj.className = 'trueColor';
        return;
    }
}

function checkPassword(obj) {
    $(obj).css('visibility', 'hidden');
    $('#password1').focus();
    if (versionMSIE() == '6')
        $('.login div').css('margin-top', '0px');        
}

function searchSubmit(e, obj, menuId) {
	if (e.keyCode == 13) {		
		return false;
	}
}

function focusInSelect(obj, startValue) {
    obj.className = 'trueColor';
    return;
}

function focusOutSelect(obj, startValue) {    
    if (obj.selectedIndex == 0)
        obj.className = '';
    return;
}

function showPopup(popupLink, colorClassCustom) {    
    $('#coverDiv').fadeIn("slow");
    $('#popupEmail').fadeIn("slow");
    
    var h = $('.footer').attr('offsetTop') + $('.footer').attr('offsetHeight') + 10;
    var w = $('body').attr('scrollWidth');
   
    $('#coverDiv').css('height', h + 'px');
    $('#coverDiv').css('width', w + 'px');

    var h1 = 370;
    var h2 = $('html').attr('scrollTop');
    if (browserType() == 'Safari') h2 = window.pageYOffset;
    
    //alert(h2);
        
    $('#popupEmail').css('top', h1 + h2 + 'px');

    var popupParent = $(popupLink).parent().parent().parent();

    var concert = "";    
    var conductor = "";    
    var stage = "";    
    var city = "";
    var colorClass = "";
    var ticketsLink = "";
    
    concert = $('h3', popupParent).text();    
    conductor = $('td.conductor', popupParent).text();
    stage = $('td.stage', popupParent).text();
    city = $('td.city', popupParent).text();
    ticketsLink = popupLink.attr("tl") || "";

    if (colorClassCustom != null) colorClass = colorClassCustom;
    else if (city == "Eindhoven") colorClass = "orange";
    else if (city == "‘s Hertogenbosch") colorClass = "purple";
    else if (city == "Tilburg") colorClass = "blue";
    else if (city == "Breda") colorClass = "green";

    /*
    if ($('.orange', popupParent).length != 0) colorClass = "orange";
    else if ($('.rose', popupParent).length != 0) colorClass = "rose";
    else if ($('.lightBlue', popupParent).length != 0) colorClass = "lightBlue";
    else if ($('.green', popupParent).length != 0) colorClass = "green";
    */
    
    var text = "U verlaat nu onze website om tickets te bestellen voor " +
               "<b>‘" + concert + "’</b> "+
               "onder leiding van " +
               "<b>" + conductor + "</b> " +
               "in " +
               "<span class='" + colorClass + "'>" + stage + " te " + city + ".</span>";

    $('#popupEmail p:first').html(text);
    document.getElementById('external').setAttribute("href", ticketsLink);
    document.getElementById('external').setAttribute("target", "_blank");
}

function inspect(obj, maxLevels, level)
{
  var str = '', type, msg;

    // Start Input Validations
    // Don't touch, we start iterating at level zero
    if(level == null)  level = 0;

    // At least you want to show the first level
    if(maxLevels == null) maxLevels = 1;
    if(maxLevels < 1)     
        return '<font color="red">Error: Levels number must be > 0</font>';

    // We start with a non null object
    if(obj == null)
    return '<font color="red">Error: Object <b>NULL</b></font>';
    // End Input Validations

    // Each Iteration must be indented
    str += '<ul>';

    // Start iterations for all objects in obj
    for(property in obj)
    {
      try
      {
          // Show "property" and "type property"
          type =  typeof(obj[property]);
          str += '<li>(' + type + ') ' + property + 
                 ( (obj[property]==null)?(': <b>null</b>'):('')) + '</li>';

          // We keep iterating if this property is an Object, non null
          // and we are inside the required number of levels
          if((type == 'object') && (obj[property] != null) && (level+1 < maxLevels))
          str += inspect(obj[property], maxLevels, level+1);
      }
      catch(err)
      {
        // Is there some properties in obj we can't access? Print it red.
        if(typeof(err) == 'string') msg = err;
        else if(err.message)        msg = err.message;
        else if(err.description)    msg = err.description;
        else                        msg = 'Unknown';

        str += '<li><font color="red">(Error) ' + property + ': ' + msg +'</font></li>';
      }
    }

      // Close indent
      str += '</ul>';

    return str;
}

function hidePopup() {    
    $('#coverDiv').fadeOut("slow");
    $('#popupEmail').fadeOut("slow");
    $('.BestelFormulier').fadeOut("slow");
}

function showBestelPopup() {
    $('#coverDiv').fadeIn("slow");
    $('.BestelFormulier').fadeIn("slow");

    var h = $('.footer').attr('offsetTop') + $('.footer').attr('offsetHeight') + 10;
    var w = $('body').attr('scrollWidth');

    $('#coverDiv').css('height', h + 'px');
    $('#coverDiv').css('width', w + 'px');

    if (browserType() == 'Opera') {
        $('.BestelFormulier .table1 td').css('border-top', '1px solid #e3e3e3');
        $('.BestelFormulier .table1 .foot1 td').css('border-top', '1px solid #e3e3e3');
        $('.BestelFormulier .table1 .foot2 td').css('border-top', '1px solid #afafaf');
        $('.BestelFormulier .table1 .foot3 td').css('border-top', '1px solid #afafaf');
    }
}

function sideMenuHover() {
    $('#menu a').hover(
        function() {            
                $(this).css('color', '#004A8F');
                Cufon.replace('#menu a', { fontFamily: 'Dax', hover: true });            
        },
        function() {            
            if (!$(this).parent().hasClass("item_selected")) {
                $(this).css('color', '#696969');
                Cufon.replace('#menu a', { fontFamily: 'Dax', hover: true });
            }
        });
}

function HistoriePageStyles() {
    var historyItems = $('.hisrotyItem');
    var maxH = 0;

    for (var i = 0; i < $(historyItems).length; i++) {        
        if (((i + 1) % 3) == 0) {
            $(historyItems[i]).css('margin-right', '0px');
            $(historyItems[i]).css('margin-left', '1px');

            maxH = 0;
            maxH = maxOfThree(historyItems[i - 2].offsetHeight, historyItems[i - 1].offsetHeight, historyItems[i].offsetHeight); 
            
            $(historyItems[i - 2]).css('height', maxH + 'px');
            $(historyItems[i - 1]).css('height', maxH + 'px');
            $(historyItems[i]).css('height', maxH + 'px');    
        }
    }
}

function intranetPageStyles() {
    var intranetRows = $('.intranet');
        
    for (var j = 0; j < $(intranetRows).length; j++) {
        intranetItems = $(intranetRows[j]).find('.intranetItem');
        intranetLink = $(intranetRows[j]).find('.musikLink');
        
        L = $(intranetItems).length;

        maxH = 0;
        maxH = maxOfThree($(intranetItems[L - 3]).attr('offsetHeight'), $(intranetItems[L - 2]).attr('offsetHeight'), $(intranetItems[L - 1]).attr('offsetHeight'));

        $(intranetItems[L - 3]).css('height', maxH + 'px');
        $(intranetItems[L - 2]).css('height', maxH + 'px');
        $(intranetItems[L - 1]).css('height', maxH + 'px');
        $(intranetLink).css('height', maxH + 'px');

        for (var i = 0; i < L; i++) {            
            if (((i + 1) % 3) == 0) {
                $(intranetItems[i]).css('margin-right', '0px');
                $(intranetItems[i]).css('margin-left', '1px');
                
                maxH = 0;                                
                maxH = maxOfThree(intranetItems[i - 2].offsetHeight, intranetItems[i - 1].offsetHeight, intranetItems[i].offsetHeight);
                
                $(intranetItems[i - 2]).css('height', maxH + 'px');
                $(intranetItems[i - 1]).css('height', maxH + 'px');
                $(intranetItems[i]).css('height', maxH + 'px');                             
            }
        }
        
        intranetItems = null;            
    }
}


function maxOfThree(h1, h2, h3) {    
    maxH = 0;
    if (h1 >= h2 && h1 >= h3) maxH = h1;
    if (h2 >= h1 && h2 >= h3) maxH = h2;
    if (h3 >= h1 && h3 >= h2) maxH = h3;
    h1 = h2 = h3 = 0;
    
    return maxH;    
}

function checkRelation(obj) {
    if ($(obj).attr('checked')) {
        $('.relation').removeClass('passive');        
        $('.relation input').removeAttr('disabled');
    }
    else {
        $('.relation').addClass('passive');
        $('.relation input').get(0).value = ''; 
        $('.relation input').attr('disabled', 'disabled');        
    }    
}

function loginStylesFix() {
    if (browserType() == 'MSIE' && versionMSIE() != '8') {
        $('.login').css('padding-top', '11px');
        $('.login').css('height', '98px');
        $('.login h2').css('margin-bottom', '5px');
        $('.login div').css('margin-top', '0px');
        $('.login input#password2').css('margin-top', '-31px');
        if(versionMSIE() == '6')
            $('.login div').css('margin-top', '5px');        
    }
}

function contactStylesFix() {
    if (browserType() == 'MSIE' && versionMSIE() == '8') {    
        $('.contactForm select').css('padding-bottom', '3px');
    }
    if (browserType() == 'MSIE' || browserType() == 'Opera') {
        $('.contactForm select').css('background-color', '#e3e3e3');
    }    
    if (browserType() == 'MSIE' && versionMSIE() != '8') {
        $('.contactForm a').css('height', '13px');
    }
}

function bestelStylesFix() {        
    if (browserType() == 'MSIE' && versionMSIE() == '8') {
        $('.BestelFormulier .checkbox input').css('margin-left', '-9px');
        $('.BestelFormulier .musikLink h2 img').css('margin-bottom', '2px');        
        $('.centerPart .seriesLink img').css('margin-bottom', '2px');
        $('.centerPart .seriesLink img').css('position', 'relative');        
    }

    if (browserType() == 'Safari') {
        $('.BestelFormulier .musikLink h2 img').css('top', '2px');        
    }
    
    if (browserType() == 'MSIE' && versionMSIE() != '8') {        
        $('.BestelFormulier .checkbox input').css('margin-left', '-3px');             
    }
    if (browserType() == 'Opera') {
        $('.BestelFormulier .checkbox input').css('margin-left', '-6px');
    }

    if (browserType() == 'Opera') {    
        $('.BestelFormulier .table1 td').css('border-top', '1px solid #e3e3e3');
        $('.BestelFormulier .table1 .head td').css('border-top', '1px solid #fff');        
        $('.BestelFormulier .table1 .foot1 td').css('border-top', '1px solid #e3e3e3');
        $('.BestelFormulier .table1 .foot2 td').css('border-top', '1px solid #afafaf');
        $('.BestelFormulier .table1 .foot3 td').css('border-top', '1px solid #afafaf');
    }


    //green form checkbox's styles (form2)
    if (browserType() == 'MSIE' && versionMSIE() == '8') 
    {
        $('.BestelFormulier .table1 .checkbox input').css('margin-top', '2px');
        $('.BestelFormulier .table1 .foot3 input').css('top', '2px');
    }
    if (browserType() == 'MSIE' && versionMSIE() != '8') {
        $('.BestelFormulier .table1 .checkbox input').css('margin-right', '2px');
        $('.BestelFormulier .table1 .checkbox img').css('margin-right', '0px');
        $('.BestelFormulier .table1 .checkbox img').css('margin-top', '2px');
    }            
    if (browserType() == 'Opera') { 
        $('.BestelFormulier .table1 .checkbox input').css('margin-right', '2px');
        $('.BestelFormulier .table1 .checkbox img').css('margin-right', '0px');
    }
    if (browserType() == 'Firefox') {
        $('.BestelFormulier .table1 .foot3 input').css('top', '2px');
    }
    if (browserType() == 'Safari') {
        $('.BestelFormulier .table1 .foot3 input').css('top', '-1px');
    }

    //with star styles (form3)
    if ((browserType() == 'MSIE' && versionMSIE() != '8') || (browserType() == 'Opera')) {
        $('.BestelFormulier .withStar .checkbox input').css('margin-right', '10px');
        $('.BestelFormulier .withStar .checkbox img').css('margin-right', '4px');
    }

    //form4 - SteunHetOrkest_nieuwevrienden(aanmelden)
    if (browserType() == 'Opera') {
        $('.centerPart .BestelFormulier .checkbox input').css('left', '-7px');
    }
    if (browserType() == 'MSIE' && versionMSIE() != '8') {
        $('.centerPart .BestelFormulier .checkbox input').css('left', '-3px');
    }
    if (browserType() == 'Opera') {
        $('.centerPart .BestelFormulier .checkbox input').css('left', '-7px');
    }
    if (browserType() == 'Firefox') {
        $('.centerPart .BestelFormulier .checkbox input').css('margin-top', '1px');
    }
    if (browserType() == 'Safari') {
        $('.centerPart .BestelFormulier .checkbox input').css('margin-top', '0px');
    }

    //form5 - Intranet_Orkestplanning

    if (browserType() == 'Opera') {
        $('.tableIntekenlijst select').css('margin-bottom', '-3px');
        $('.tableIntekenlijst select').css('height', '23px');
    }
    if (browserType() == 'MSIE' && versionMSIE() == '8') {
        $('.tableIntekenlijst select').css('padding-top', '3px');
        $('.tableIntekenlijst select').css('padding-bottom', '3px');
    }
    if (browserType() == 'MSIE' && versionMSIE() == '7') {
        $('.tableIntekenlijst select').css('margin-bottom', '-2px');
    }
    if (browserType() == 'MSIE' && versionMSIE() == '6') {
        $('.tableIntekenlijst select').css('margin-bottom', '2px');
    }
}


function StylesFix() {    
    if (browserType() == 'MSIE' && versionMSIE() == '8') {        
        $('.orkestplaning a img').css('margin-bottom', '2px');
        $('.orkestplaning a img').css('position', 'relative');
    }

    if (browserType() == 'MSIE' && versionMSIE() == '6') {
        $('.orkestplaning .musikLink h2').css('width', '100px');        
    }
    
}

function onLoadFunction() {
    doCufon();
    loginStylesFix();
    contactStylesFix();
    bestelStylesFix();
    HistoriePageStyles();
    intranetPageStyles();
    StylesFix();
}

