// JavaScript Document

var q = $.parseQuery();
var action = 'head';

function CheckInput(x){
    if(x.length < 3	){
        alert("Pre vyhladávanie musíte zadat minimálne 3 znaky!");
        return false;
    }else{
        return true;
    }
}

function checkEmail(adresa)
{
    var pozice_zavinace = adresa.indexOf("@");
    if (pozice_zavinace < 0)
        return false;
    var cast_pred_zavinacem = adresa.substring(0,pozice_zavinace);
    var cast_po_zavinaci = adresa.substring(pozice_zavinace+1,adresa.length);
    if (cast_po_zavinaci.indexOf("@") >= 0)
        return false;
    if (cast_pred_zavinacem.length <= 0)
        return false;
    if (cast_po_zavinaci.length <= 0)
        return false;
    var pozice_posledni_tecky = cast_po_zavinaci.lastIndexOf(".");
    if (pozice_posledni_tecky < 0)
        return false;
    var pocet_znaku_za_posledni_teckou = cast_po_zavinaci.length - pozice_posledni_tecky - 1;
    if (pocet_znaku_za_posledni_teckou < 2  ||  pocet_znaku_za_posledni_teckou > 3)
        return false;
    var pozice_dvou_tecek_vedle_sebe = adresa.indexOf("..");
    if (pozice_dvou_tecek_vedle_sebe >= 0)
        return false;
    if (cast_pred_zavinacem.charAt(0) == "."  ||  cast_pred_zavinacem.charAt(cast_pred_zavinacem.length-1) == ".")
        return false;
    if (cast_po_zavinaci.charAt(0) == "."  ||  cast_po_zavinaci.charAt(cast_po_zavinaci.length-1) == ".")
        return false;
    return true;
}
 
function checkBulletin(form){
    if(checkEmail(form.mail.value)){
        return true;
    }
    else{
        alert("Zadali ste neplatnú emailovú adresu!");
        return false;
    }
}

function loginCheck(thisForm){
    if(thisForm.login.value	== '' || thisForm.pwd.value	== '') {
        alert('Aby ste sa mohli prihlásiť musíte zadať meno a heslo.');
        return false;
    }
    return true;
}

function searchFocus(id)
{
    id.style.color = '#4a4a4a';
    id.value = '';
}

function checkPassForm(thisForm){

    var re = new RegExp("^[^.]+(\.[^.]+)*@([^.]+[.])+[a-z]{2,4}$");

    if(thisForm.login.value == "") {
        alert('Musíte zdať meno aby ste mohli pokračovať!');
        thisForm.login.focus();
        return false;
    } else if(thisForm.email.value == "") {
        alert('Musíte zdať email aby ste mohli pokračovať!');
        thisForm.email.focus();
        return false;
    } else if(!re.test(thisForm.email.value)) {
        alert('Emailová adresa nieje akceptovateľná systémom!');
        thisForm.email.focus();
        return false;
    } else {
        return true;
    }
}

/* product subcat */

function setSubCatValues(str) {			
    var	values = str.split(":");
    var product = $('#pid_' + values[0]);
    if(product) {
        var code = $('#code_' + values[0]);
        if(code) {
            var price = $('#price_' + values[0]);
            // var kurz = $('#kurz_' + values[0]);
            var code_view = $('#code_view_' + values[0]);
            var amount = $('#amount_' + values[0]);
            var amount_session = $('#amount_session_' + values[0]);
            var kosik = $('#kosik_' + values[0]);
            var test = $('#test_' + values[0]);
            var fixedp = calcDiscount(parseFloat(values[1])).toFixed(2);
								
            product.val(calcDiscount(values[1]).toFixed(2));
            code.val(values[2]);
            code_view.text(values[2]);
            price.text(fixedp.replace('.', ','));
            if(values[3] > 0){
                kosik.attr("src", "images/page/kosik_add.gif");
                kosik.attr("onclick", "return confirmProductAdd('" + values[0] + "')");
                test.val(1);
            }
            else{
                kosik.attr("src", "images/page/kosik.gif");
                kosik.attr("onclick", "");
                test.val(0);
                values[3] = 1;
            }
			
            amount.val(values[3]);
            amount_session.val(values[3]);
        }
    }
}

function setSubCatItem(element) {
    var data = element.attr('rel');
    var name = element.find('div.bsname').text();
    var values = data.split(":");
    $('#txt_' + values[0]).text(name);
    setSubCatValues(data);
}

function setSubCatFirstItem(pid) {						
    var el = $('#_sel_' + pid + ' a:first');
    setSubCatItem(el);
}

function confirmProductAdd(id) {
    var add = $('#amount_' + id);
    var amount = $('#amount_session_' + id);
    var msg = "Zvolený tovar sa už v košíku nachádza v počte ks : " + amount.val();
    msg += "\nPrajete si jeho množstvo zvýšiť o " + add.val() + " ks ?";
    return confirm(msg);
}

/* .end */

/* action */

function loadAction(test) {
    if(test == action) {
        $("#bigAction").show();
    }
}

function showAction(test) {
    if(test != action) {
        if(action == 'head') {
            $("#bigAction").hide();
            $("#bigAction2").show();
            action = 'content';
        } else if(action == 'content') {
            $("#bigAction2").hide();
            $("#bigAction").show();
            action = 'head';
        }
    }
}

function moveAction(load) {
    var w = $(window).width();
    var test = action;
    if(w < 1600) {
        test = 'content';
    } else {
        test = 'head';
    }

    if(load) {
        loadAction(test);
    }
    showAction(test);
}

/* .end */

$(function (){
    $('.ps_btn_a').click(function () {
        var sel = $(this).attr('rel');
        var test = $('#' + sel ).css('display');
        if(test == 'none') {
            $('.box_select:visible').hide();
            $('#' + sel ).slideDown('fast');
        }
        else {
            $('#' + sel ).hide();
        }
    });
	
    $('body').click(function () {
        $('.box_select').hide();
    });
	
    $('div.box_select_option').hover(function () {
        $(this).addClass('box_select_option_hover');
        // thumb
        var data = $(this).parent("a").attr('rel');
        var values = data.split(":");
        var image = $(this).find("img").attr("src");
        $('#_thumb_' + values[0]).find("img").attr("src", image)
        $('#_thumb_' + values[0]).show();
    }, function() {
        $(this).removeClass('box_select_option_hover');
        // thumb
        var data = $(this).parent("a").attr('rel');
        var values = data.split(":");
        $('#_thumb_' + values[0]).hide();
    });
	
    $('div.box_select a').click(function () {
        setSubCatItem($(this));
    });

    $("#tree").treeview({
        collapsed: true,
        animated: "medium",
        prerendered: false,
        persist: "item",
        unique: true
    });

    $("#actionScrollSmall").mbScrollable({
        width:520,
        elementsInPage:3,
        elementMargin:0,
        shadow:false,
        height:"auto",
        controls:"#controlSmall",
        slideTimer:600,
        autoscroll:true,
        scrollTimer:4000
    });

    $('#actionScrollSmall .box').hover(function() {
        var aid = $(this).find("a").attr("rel");
        if(aid != 'none') {
            var countOfPage = 3;
            var count = $('#actionScrollSmall .box').size();
            var pos = $(this).position();
            var sort = ((Number(pos.left) + 173.33) / 173.33).toFixed(0);
            var num = sort % 3;
            if(num == 0) {
                num = 3;
            }
            if((count % 3) != 0) {
                var idx = $("#controlSmall .smallPageId").text();
                var data = idx.split('_');
                if(data[0] == data[1]) {
                    var pcount = data[0] * countOfPage;
                    num += pcount - count;
                    if(num > 3) {
                        num -= 3;
                    }
                }
            }
            var margin = num * 173.33 - 140;
            $("#" + aid).css('margin-left', margin + 'px').fadeIn();
        }
    }, function() {
        var aid = $(this).find("a").attr("rel");
        $("#" + aid).fadeOut();
    });

    $('.time').jclock();
	
});

$(document).ready(function () {								
		
    $("#menu-main ul.sf-menu").superfish({
        speed: 			500,
        delay: 			400,
        autoArrows:    	false,
        dropShadows:   	false
    });    

    $("a[rel^='lightbox']").prettyPhoto({
        animationSpeed: 'fast', /* fast/slow/normal */
        padding: 50, /* padding for each side of the picture */
        opacity: 0.75, /* Value betwee 0 and 1 */
        showTitle: true, /* true/false */
        allowresize: true, /* true/false */
        counter_separator_label: '/', /* The separator for the gallery counter 1 "of" 2 */
        theme: 'light_rounded', /* light_rounded / dark_rounded / light_square / dark_square */
        hideflash: false, /* Hides all the flash object on a page, set to TRUE if flash appears over prettyPhoto */
        modal: false, /* If set to true, only the close button will close the window */
        changepicturecallback: function(){}, /* Called everytime an item is shown/changed */
        callback: function(){} /* Called when prettyPhoto is closed */
    });    

    $("#actionScrollBig").mbScrollable({
        width:432,
        elementsInPage:1,
        elementMargin:0,
        shadow:false,
        height:"auto",
        controls:"#controlBig",
        slideTimer:600,
        autoscroll:true,
        scrollTimer:4000
    });

    $("#actionScrollBig2").mbScrollable({
        width:432,
        elementsInPage:1,
        elementMargin:0,
        shadow:false,
        height:"auto",
        controls:"#controlBig2",
        slideTimer:600,
        autoscroll:true,
        scrollTimer:4000
    });

     $("#actionScrollSmall, #actionScrollBig, #actionScrollBig2").hover(function() {
         $(this).mbStopAutoscroll();
     }, function() {
         $(this).mbAutoscroll();
     });


    // vyhladavanie
    var sq = $("#sq_string").val();
    $("#sq_string").focus(function() {
        var val = $(this).val();
        if(val == sq) {
            $(this).val('');
        }
    });

    $("#sq_string").blur(function() {
        var val = $(this).val();
        if(val == '') {
            $(this).val(sq);
        }
    });    

    $(window).resize(function() {
        moveAction(0);
    });
    
    $(window).load(function() {
        moveAction(1);        
    });    

});
