(function($) {
    var imgList = [];
    $.extend({
        preload: function(imgArr, option) {
            var setting = $.extend({
                init: function(loaded, total) {},
                loaded: function(img, loaded, total) {},
                loaded_all: function(loaded, total) {}
            }, option);
            var total = imgArr.length;
            var loaded = 0;
            
            setting.init(0, total);
            for(i = 0; i < imgArr.length; i++) {
                imgList.push($("<img />")
                    .attr("src", imgArr[i])
                    .load(function() {
                        loaded++;
                        setting.loaded(this, loaded, total);
                        if(loaded == total) {
                            setting.loaded_all(loaded, total);
                        }
                    })
                );
            }
            
        }
    });
})(jQuery);

jQuery.extend(jQuery.validator.messages, {
    required: "Pole je povinné.",
    email:    "Pole je zadané v nesprávnom tvare.",
    number:   "Pole musí byť číslo."
});

$(document).ready(function(){
    if ($('.bubleWrap').length > 0){
        $('.bubleWrap').hide();
    }

    if ($('#daya1').length > 0){
        displayDatesA();
    }
    
    if ($('#timeb1').length > 0){
        displayDatesB();
    }
    
    if ($('.kontaktCB').length > 0){
        $('.kontaktCB').hide();
        $('.wrapperForBubble').mouseleave(function(){
            $('.kontaktCB').hide();
        });
    }
    
    if ($("#order_form").length > 0){
        $("#order_form").validate();
    }

    $('.orderSteps a').click(function(){
        $("#order_form").submit();
    });

    if ($(".obsah").length > 0 && $(".obsah_right").length > 0){
        var obsahHeight = $(".obsah").height();
        var obsah_rightHeight = $(".obsah_right").height();
        var heightToSet = (obsahHeight >= obsah_rightHeight) ? obsahHeight : obsah_rightHeight;
        $(".obsah").height(heightToSet);
    }
    
    var prices1 = {
            0: '(0,04 € za stranu)',
            1: '(0,20 € za stranu)'
    };
    var prices2 = {
            0: '(0,07 € za stranu)',
            1: '(0,38 € za stranu)'
    };
    if ($('#navrh').length > 0){
        navrhValue = $(this).val();
        prices = (navrhValue == '2') ? prices2 : prices1;
        $('#bwNumbers').text(prices[0]);
        $('#colorNumbers').text(prices[1]);
    }
    if ($('#navrh').length > 0){
        $('#navrh').change(function(){
            navrhValue = $(this).val();
            prices = (navrhValue == '2') ? prices2 : prices1;
            $('#bwNumbers').text(prices[0]);
            $('#colorNumbers').text(prices[1]);
        })
    }
});

//$(document).ready(function(){
//    $("#order_form").validate();
//});

function showContactBubble(bubbleId, type){
    $('.kontaktCB').hide();
    
    if (type){
        switch (type){
            case 1 :
                $('#' + bubbleId).css({
                    bottom: '50px',
                    left: '150px'
                });
                $('#' + bubbleId).show();
                $('#' + bubbleId).mouseleave(function(){
                    $(this).hide();
                });
                break;
            case 2 :
                $('#' + bubbleId).css({
                    bottom: '-50px',
                    left: '150px'
                });
                $('#' + bubbleId).show();
                $('#' + bubbleId).mouseleave(function(){
                    $(this).hide();
                });
                break;
        }
    } else {
        $('#' + bubbleId).css({
            bottom: '-50px',
            left: '150px'
        });
        $('#' + bubbleId).show();
        $('#' + bubbleId).mouseleave(function(){
            $(this).hide();
        });
    }
}

function displayDatesA(){
    currentWDay = getObjectDayA('+2', false, false);
    currentTime = getObjectTime('+2', false);
    $('#daya1').text(currentWDay);
    $('#timea1').text(currentTime);
    
    secondWDay = getObjectDayA('+3', false, true);
    secondTime = getObjectTime('+3', true);
    $('#daya2').text(secondWDay);
    $('#timea2').text(secondTime);
    
    secondWDay = getObjectDayA('+2', secondWDay, false);
    secondTime = '09:00';
    $('#daya3').text(secondWDay);
    $('#timea3').text(secondTime);
    
    setTimeout("displayDatesA()", 1000);
}

function displayDatesB(){
    currentWDay = getObjectDayB('+2', false);
    currentTime = getObjectTimeB('+2');
    $('#dayb1').text(currentWDay);
    $('#timeb1').html(currentTime);
    
    secondWDay = getObjectDayB('+2', true);
    secondTime = '<img src="images/diplomovka/c_0.png" alt="" border="0" /><img src="images/diplomovka/c_9.png" alt="" border="0" /><img src="images/diplomovka/c_p.png" alt="" border="0" /><img src="images/diplomovka/c_0.png" alt="" border="0" /><img src="images/diplomovka/c_0.png" alt="" border="0" /><img src="images/diplomovka/c_p.png" alt="" border="0" /><img src="images/diplomovka/c_0.png" alt="" border="0" /><img src="images/diplomovka/c_0.png" alt="" border="0" />';
    $('#dayb2').text(secondWDay);
    $('#timeb2').html(secondTime);
    
    setTimeout("displayDatesB()", 1000);
}

function addZero(digit){
    digit = (digit < 10) ? '0' + digit : digit;
    
    return digit;
}

function calcTime(offset) {

    // create Date object for current location
    d = new Date();
    
    // convert to msec
    // add local time zone offset 
    // get UTC time in msec
    utc = d.getTime() + (d.getTimezoneOffset() * 60000);
    
    // create new Date object for different city
    // using supplied offset
    nd = new Date(utc + (3600000*offset));
    
    // return time as a string
    return nd;//.toLocaleString();

}

function getObjectTime(offset, second){
    var bratislavaDate = calcTime(offset);
    
    var now = new Date(bratislavaDate);
    
    var h = now.getHours();
    var m = now.getMinutes();
    
    if (second){
        if (h < 9 || (h > 16 && m >= 0)){
            h = 9;
            m = 0;
        }
    }
    
    h = addZero(h);
    m = addZero(m);
        
    return h + ':' + m;
}

function getObjectTimeB(offset){
    var bratislavaDate = calcTime(offset);
    
    var now = new Date(bratislavaDate);
    
    var h = now.getHours();
    var m = now.getMinutes();
    var s = now.getSeconds();
        
    h = addZero(h).toString();
    m = addZero(m).toString();
    s = addZero(s).toString();
    
    hStr1 = '<img src="images/diplomovka/c_' + h[0] + '.png" alt="" border="0" />';
    hStr2 = '<img src="images/diplomovka/c_' + h[1] + '.png" alt="" border="0" />';
    mStr1 = '<img src="images/diplomovka/c_' + m[0] + '.png" alt="" border="0" />';
    mStr2 = '<img src="images/diplomovka/c_' + m[1] + '.png" alt="" border="0" />';
    sStr1 = '<img src="images/diplomovka/c_' + s[0] + '.png" alt="" border="0" />';
    sStr2 = '<img src="images/diplomovka/c_' + s[1] + '.png" alt="" border="0" />';
    allStr = hStr1 + hStr2 + 
                '<img src="images/diplomovka/c_p.png" alt="" border="0" />' +
                mStr1 + mStr2 +
                '<img src="images/diplomovka/c_p.png" alt="" border="0" />' + 
                sStr1 + sStr2;
   
    return allStr;
}

function getObjectDayA(offset, next, second){
    var bratislavaDate = calcTime(offset);

    var now = new Date(bratislavaDate);
    var wday = now.getDay();
    
    if (second){
        var h = now.getHours();
        var m = now.getMinutes();
        if (h > 16 && m >= 0){
            wday += 1;
        }
    }

    slovakWeekDaysVV = {
            'Nedeľa' : 0,
            'Pondelok' : 1,
            'Utorok' : 2,
            'Streda' : 3,
            'Štvrtok' : 4,
            'Piatok' : 5,
            'Sobota' : 6
    }
    
    if (next){
        wday = slovakWeekDaysVV[next] + 1;
    }
    
    if (wday == '0' || wday == '6' ){
        wday = 1;
    }
    
    slovakWeekDays = {
            0: 'Nedeľa',
            1: 'Pondelok',
            2: 'Utorok',
            3: 'Streda',
            4: 'Štvrtok',
            5: 'Piatok',
            6: 'Sobota'
    }
    
    return slovakWeekDays[wday];
}

function getObjectDayB(offset, next){
    var bratislavaDate = calcTime(offset);

    var now = new Date(bratislavaDate);
    var wday = now.getDay();
    
    if (next){
        var h = now.getHours() + 1;
        var m = now.getMinutes();
        if (h > 16 && m >= 0){
            wday += 1;
        }
        wday += 1;
    }
    
    if (wday == '0' || wday == '6'){
        wday = 0; // before was here 1
    }
    else if (wday == '7'){
        wday = 2;
    }
    
    slovakWeekDays = {
            0: 'NE',
            1: 'PO',
            2: 'UT',
            3: 'ST',
            4: 'ŠT',
            5: 'PI',
            6: 'SO'
    }
    
    return slovakWeekDays[wday];
}

/**
* zisti ci zadana hodnota je cislo
*/    
function isNumber(pocet){
	if(isNaN(pocet))
    {
    	return alert('Musíte zadať číselnú hodnotu');
   	}else{
   		if (pocet > 100) {
   			return alert('Môžete zadať maximálne 100 dní');
   		} else {
   			return true;
   		}
   		
   	}
}

function CB_ExternalFunctionLoad()
{
	  return false;
}

function showBubble(bubbleClass)
{
    $('.' + bubbleClass).show();
}

function hideBubble(bubbleClass)
{
    $('.' + bubbleClass).hide();
}
