Cufon.replace('#menucontent div.menuitem a');
Cufon.replace('h1');
Cufon.replace('.front_rightbox.lgrey h2');
Cufon.replace('#contentLeft div.lefthead');
Cufon.replace('#frontMainFrame div.lefthead');
Cufon.replace('#subContentLeft div.leftmediaheader');
Cufon.replace('#banners div.bannerContainer td');
Cufon.replace('#editordata div.kkkBoxWarning h2');

//Cufon.replace('.bannerContainer p, .bannerContainer h1, .bannerContainer h2');
//Cufon.replace('.tabText a'); // Pitää lisätä Cufon myös tab-vaihtoon, että väri vaihtuu

$(document).ready(function(){          
    // Date picker
    /* Finnish initialisation for the jQuery UI date picker plugin. */
    /* Written by Harri Kilpi? (harrikilpio@gmail.com). */
    $.datepicker.regional['fi'] = {
        closeText: 'Sulje',
        prevText: '&laquo;Edellinen',
        nextText: 'Seuraava&raquo;',
        currentText: 'T&auml;n&auml;&auml;n',
        monthNames: ['Tammikuu','Helmikuu','Maaliskuu','Huhtikuu','Toukokuu','Kes&auml;kuu',
        'Hein&auml;kuu','Elokuu','Syyskuu','Lokakuu','Marraskuu','Joulukuu'],
        monthNamesShort: ['Tammi','Helmi','Maalis','Huhti','Touko','Kes&auml;',
        'Hein&auml;','Elo','Syys','Loka','Marras','Joulu'],
        dayNamesShort: ['Su','Ma','Ti','Ke','To','Pe','Su'],
        dayNames: ['Sunnuntai','Maanantai','Tiistai','Keskiviikko','Torstai','Perjantai','Lauantai'],
        dayNamesMin: ['Su','Ma','Ti','Ke','To','Pe','La'],
        dateFormat: 'dd.mm.yy', firstDay: 1,
        isRTL: false};
    /* Swedish initialisation for the jQuery UI date picker plugin. */
    /* Written by Anders Ekdahl ( anders@nomadiz.se). */
    $.datepicker.regional['sv'] = {
        closeText: 'Stäng',
        prevText: '&laquo;Förra',
        nextText: 'Nästa&raquo;',
        currentText: 'Idag',
        monthNames: ['Januari','Februari','Mars','April','Maj','Juni',
        'Juli','Augusti','September','Oktober','November','December'],
        monthNamesShort: ['Jan','Feb','Mar','Apr','Maj','Jun',
        'Jul','Aug','Sep','Okt','Nov','Dec'],
        dayNamesShort: ['Sön','Mån','Tis','Ons','Tor','Fre','Lör'],
        dayNames: ['Söndag','Måndag','Tisdag','Onsdag','Torsdag','Fredag','Lördag'],
        dayNamesMin: ['Sö','Må','Ti','On','To','Fr','Lö'],
        dateFormat: 'dd.mm.yy', firstDay: 1,
        isRTL: false};
    $.datepicker.setDefaults($.datepicker.regional['fi']);
    $('div.date input.datepicker').datepicker({ 
                                        showOn: 'button',
                                        buttonImage: '/extension/nvkeskuskauppakamari/design/nvsite/images/calendar_month.png', 
                                        buttonImageOnly: true,
                                        dateFormat: 'dd-mm-yy',
                                        changeMonth: true,
                                        changeYear: true,
                                        onSelect: function(dateText, inst) {
                                            val_array = dateText.split('-');
                                            $(this).siblings('input.year').val(val_array[2]);
                                            $(this).siblings('input.month').val(val_array[1]);
                                            $(this).siblings('input.day').val(val_array[0]);
                                        },
                                        beforeShow: function(input) {
                                            val_string = '';
                                            val_today = new Date();
                                            input_day = $(this).siblings('input.day').val();
                                            input_month = $(this).siblings('input.month').val();
                                            input_year = $(this).siblings('input.year').val();
                                            val_string += (input_day > 0 ? input_day : val_today.getDate()) + '-';
                                            val_string += (input_month > 0 ? input_month : val_today.getMonth() + 1) + '-';
                                            val_string += (input_year > 0 ? input_year : val_today.getFullYear());
                                            $(this).val(val_string);
                                        }});

    var d = new Date();
    $('form input.textDatepicker').datepicker({ 
                                        showOn: 'both',
                                        buttonImage: '/extension/nvkeskuskauppakamari/design/nvsite/images/calendar_month.png', 
                                        buttonImageOnly: true,
                                        dateFormat: 'dd.mm.yy',
                                        changeMonth: true,
                                        changeYear: true,
                                        yearRange: (d.getFullYear() - 80) + ':' + (d.getFullYear() + 2)
                                        });

    
    // ATA Carnet tool filter datepickers
    $('#ataDatefilterStart, #ataDatefilterEnd').datepicker({ 
        showOn: 'both',
        buttonImage: '/extension/nvkeskuskauppakamari/design/nvsite/images/calendar_month.png', 
        buttonImageOnly: true,
        dateFormat: 'dd.mm.yy',
        changeMonth: true,
        changeYear: true,
        yearRange: '1980:' + (d.getFullYear())
    });

    // Tiedotteet navi
    $('#tiedoteNav ul:not(#ulselected)').hide();
    $('#tiedoteNav div.header').bind('click', function() {
        if($(this).attr('id') == 'hselected') {
            $(this).removeAttr('id');
            $('#tiedoteNav ul').slideUp(100); //hide();
        }
        else {
            $('#hselected').removeAttr('id');
            $('#ulselected').removeAttr('id').slideUp(100); //hide();
            $(this).attr('id', 'hselected');
            $(this).next('ul').attr('id', 'ulselected').slideDown(100); //show();
        }
        return false;
    });

    // Custom tag banners
    $('#banners div.bannerContainer .bannerTable').each(function() {
        var $last_link = $(this).find('a:last');
        if ( $last_link.attr('href') ) {
            var href_string = $last_link.attr('href');
            $last_link.removeAttr('href');
            $(this).bind('click', function() {
                if($last_link.attr('target') == '_blank') {
                  var newWindow = window.open(href_string);
                  newWindow.focus();
                }
                else window.location = href_string;   
            });
            $(this).css('cursor', 'pointer');
        }
    });

    // Menu ToolTips
    /** DISABLED DUE TO QTIP INCOMPATIBILITY WITH JQUERY 1.4.2
    $('#menucontent div.showToolTip a[title], img.helpIcon[title]').qtip({ style: { name: 'light', tip: true }, position: { corner: { target: 'topMiddle', tooltip: 'bottomLeft' } } }).find('img').removeAttr('alt');
    **/

    // AjaxLoad image
    $("#ajaxLogin").after("<div id='ajaxLoader'><img /></div>");
    $("#ajaxLoader img").attr("src", window.loadingImg.src);
    loginLoaded();

    // Facebox
    $('a[rel*=facebox]').facebox();

    // Ansiomerkkihakemus
    if ($('#ansiomerkkihakemus tr.attribute_1095 input:checked').val() == '0')
    {
        showToimitusPosti();
    }
    else
    {
        showToimitusNouto();
    }
    $('#ansiomerkkihakemus tr.attribute_1095 input').bind('click', function() {
        if ($(this).val() == '0')
        {
            showToimitusPosti();
        }
        else
        {
            showToimitusNouto();
        }
    });

    // Animoidut karusellit
    //$("div.frontAnimation").carousel( { loop:true, autoSlide:true, effect:"fade" } );
    /*
    $("#imgcontainer").cycle({ 
        timeout:       5000,  // milliseconds between slide transitions (0 to disable auto advance) 
        speed:         2000,  // speed of the transition (any valid fx speed value) 
        next:          null,  // id of element to use as click trigger for next slide 
        prev:          null,  // id of element to use as click trigger for previous slide 
        before:        null,  // transition callback (scope set to element to be shown) 
        after:         null,  // transition callback (scope set to element that was shown) 
        height:       'auto', // container height 
        sync:          1,     // true if in/out transitions should occur simultaneously 
        fit:           0,     // force slides to fit container 
        pause:         0,     // true to enable "pause on hover" 
        delay:         0,     // additional delay (in ms) for first transition (hint: can be negative) 
        slideExpr:     null   // expression for selecting slides (if something other than all children is required) 
        }
    );
    $("div.bannerContainer").cycle({ 
        timeout:       3000,  // milliseconds between slide transitions (0 to disable auto advance) 
        speed:         500,  // speed of the transition (any valid fx speed value) 
        next:          null,  // id of element to use as click trigger for next slide 
        prev:          null,  // id of element to use as click trigger for previous slide 
        before:        null,  // transition callback (scope set to element to be shown) 
        after:         null,  // transition callback (scope set to element that was shown) 
        height:       'auto', // container height 
        sync:          1,     // true if in/out transitions should occur simultaneously 
        fit:           0,     // force slides to fit container 
        pause:         1,     // true to enable "pause on hover" 
        delay:         0,     // additional delay (in ms) for first transition (hint: can be negative) 
        slideExpr:     null   // expression for selecting slides (if something other than all children is required) 
    }
    );
    */

    // Overlay for LKV/LVV enrollment
    

    if ($('#enrollSaving').length > 0) {
    
      $('#enrollSaving').overlay({
        mask: {
  		    color: '#ffffff',
  		    loadSpeed: 200,
  		    opacity: 0.5
  	    },
  	    closeOnClick: false,
        top: '40%',
        load: false,
        onLoad: function() {
          return true;
        }
      });
      $('#EnrollButton').bind('click', function() {
        var api = $('#enrollSaving').data("overlay");
        api.load();
      })
      
    }
});

// Ansiomerkkihakemus, toimitustapavalinta
var toimitusPosti = new Array("attribute_1102",
                              "attribute_1099",
                              "attribute_1101",
                              "attribute_1104",
                              "attribute_1106",
                              "attribute_1108");
var toimitusNouto = new Array("attribute_1109",
                              "attribute_1110",
                              "attribute_1113",
                              "attribute_1115",
                              "attribute_1117",
                              "attribute_1121");

function showToimitusPosti()
{
    for (i in toimitusPosti)
    {
        $('#ansiomerkkihakemus tr.' + toimitusPosti[i]).show();
    }
    for (i in toimitusNouto)
    {
        $('#ansiomerkkihakemus tr.' + toimitusNouto[i]).hide();
    }
}

function showToimitusNouto()
{
    for (i in toimitusPosti)
    {
        $('#ansiomerkkihakemus tr.' + toimitusPosti[i]).hide();
    }
    for (i in toimitusNouto)
    {
        $('#ansiomerkkihakemus tr.' + toimitusNouto[i]).show();
    }
}

// AjaxLoad Image
var loadingImg = new Image();
loadingImg.src = "/extension/nvkeskuskauppakamari/design/nvsite/images/ajax-loader.gif";

// **************************
// AJAX LOGIN FUNCTIONS START
function showLoadingImg() {
    // $("#ajaxLoader").css({top: Math.max(($("#ajaxLogin").height() - loadingImg.height) / 2 + 10, 10) + "px"});
    $("#ajaxLoader").fadeIn("fast");
}

function hideLoadingImg() {
    $("#ajaxLoader").fadeOut("fast");
}

function loginLoaded() {
    /** DISABLED DUE TO QTIP INCOMPATIBILITY WITH JQUERY 1.4.2
    $("#ajaxLogin form input[title]").qtip({ style: { name: 'light', tip: true }, position: { corner: { target: 'bottomMiddle', tooltip: 'topMiddle' } } });
    **/
    $("#ajaxLogin form").bind('submit', function() {
        $('#loginBox div.ajaxWarning').hide();
        sendLogin();
        //return false;
    });
}

function sendLogin() {
    showLoadingImg(); 
    $("#ajaxLogin").fadeTo("fast", 0.33);

    /*
    $.post("/ezjscore/run/user/login?mode=ajax", $("#ajaxLogin form").serialize(), function(data){
        if(data === "") window.location.reload();
        else if(jQuery.trim(data).substr(1, 4) != 'form') {
            window.location = "/user/login";
        }
        else {
            $("#ajaxLogin form input[title]").qtip("destroy");
            $("#ajaxLogin form input[title]").qtip({ style: { name: 'light', tip: true }, position: { corner: { target: 'bottomMiddle', tooltip: 'topMiddle' } } });
            hideLoadingImg();
            $("#loginBox").html(data);
            $('#loginBox div.ajaxWarning').hide();
            $("#ajaxLogin").fadeTo("fast", 1);
            $('#loginBox div.ajaxWarning').fadeIn();
            loginLoaded();
        }
    }); 
    */
}

function logout() {
    showLoadingImg();
    $("#ajaxLogin").fadeTo("fast", 0.33);
    $.post("/ezjscore/run/user/logout", $("#ajaxLogin form").serialize(), function(data){
        window.location.reload();
    }); 
}

function login() {
    $("#loginBox:hidden:not(:animated)").fadeIn("fast", function() {
        $('#id1').focus();
    });
    $("#loginBox:visible:not(:animated)").fadeIn("fast", function() {
        $("#ajaxLogin form").submit();
    });
    //$("#loginBox:visible:not(:animated)").fadeOut("fast", function() {
    //    $(this).find('div.ajaxWarning').hide();
    //});
}
// AJAX LOGIN FUNCTIONS END
// -------------------------

/**
* Function : dump()
* Arguments: The data - array,hash(associative array),object
*    The level - OPTIONAL
* Returns  : The textual representation of the array.
* This function was inspired by the print_r function of PHP.
* This will accept some data as the argument and return a
* text that will be a more readable version of the
* array/hash/object that is given.
*/
function dump(arr,level) {
var dumped_text = "";
if(!level) level = 0;

//The padding given at the beginning of the line.
var level_padding = "";
for(var j=0;j<level+1;j++) level_padding += "    ";

if(typeof(arr) == 'object') { //Array/Hashes/Objects
 for(var item in arr) {
  var value = arr[item];
 
  if(typeof(value) == 'object') { //If it is an array,
   dumped_text += level_padding + "'" + item + "' ...\n";
   dumped_text += dump(value,level+1);
  } else {
   dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
  }
 }
} else { //Stings/Chars/Numbers etc.
 dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
}
return dumped_text;
} 

$(document).ready(function() {
	$('a.image-lightbox').prettyPhoto();
});
