
var requiredMajorVersion = 8;
var requiredMinorVersion = 0;
var requiredRevision = 0;


$(document).ready(function() { 

    $(".ajax_status").bind("ajaxSend", function() {
       $(".ajax_status").addClass("ajax-loading");
    }).bind("ajaxComplete", function(){
       $(".ajax_status").removeClass("ajax-loading");
    });
    

    
    if ($(".imgzoom") && jQuery.isFunction(jQuery.fn.fancybox))
    $(".imgzoom").fancybox();
    
    
    if ($(".OptName"))
    $('body').click(function(e){
    
        //    document.title = e.pageX +', '+ e.pageY + ', ' + e.target.className;

        if ($(e.target).hasClass("stayVis") == false) {
        
            $(".OptName span").removeClass("clicked");
            $(".OptVal").removeClass("vis");
            
        }
    
    });
    
    
    
    /*if ($.browser.version != "6.0")
    //styleCheckboxesAndRadiobuttons();
    */

    if ($('.prmSelect')) {
        //$('.prmSelect').selectbox({debug: true});
    }

 if ($("#NaujasAtsiliepimas")) {
        $("#NaujasAtsiliepimas").submit(function() {
            
            var is_good = true;
            $('#NaujasAtsiliepimas .require').each(function(){
                if ($(this).val() == '') {
                    $(this).css({'border':'1px solid #DD0007'});
                    is_good = false;
                } else {
                    $(this).css({'border':'#ccc 1px solid'});
                }
            });
            if (is_good) {
                $('#post_form').attr('disabled','disabled');
            } else {
                $('#post_form').removeAttr('disabled');
            }
            //return false;
            return is_good;
        });
    }
    if ($("#NaujasSkelbimas")) {
        $("#NaujasSkelbimas").submit(function() {
            
            var is_good = true;
            $('#NaujasSkelbimas .require').each(function(){
                if ($(this).val() == '') {
                    $(this).css({'border':'1px solid #DD0007'});
                    is_good = false;
                } else {
                    $(this).css({'border':'#ccc 1px solid'});
                }
            });
            if (is_good) {
                $('#post_form').attr('disabled','disabled');
            } else {
                $('#post_form').removeAttr('disabled');
            }
            //return false;
            return is_good;
        });
    }
       if ($("#in_gift_form").attr("value") == "1") {
            var required_fields = ["forma_email", "forma_klausimas"];
            //CheckForm(required_fields);
             function showResponse(responseText, statusText)  { 
                 $("#webdev_form").fadeOut("fast");
                 $("#form_sent").fadeIn("fast", function(){});
            } 
             function validate(formData, jqForm, options) { 
                var usernameValue = $('#forma_email').fieldValue(); 
                var passwordValue = $('#forma_klausimas').fieldValue(); 
                if (!usernameValue[0] || !passwordValue[0]) { 
                    alert('Užpildykite privalomus laukus');
                    //$(".butt_submit").bind ('click', uzklausaClick);
                    $('#clone').remove();
                    $('.butt_submit').removeAttr('style');
                    return false; 
                }
             }
            $("#webdev_form").submit(function() {
                $(this).ajaxSubmit({ beforeSubmit: validate, success: showResponse });
                return false;
            });
            function uzklausaClick (event) {
                $(this).clone(false).insertAfter(this).attr('id','clone');
                $(this).css({'display':'none'});
                event.preventDefault();
                $("#webdev_form").trigger('submit');
                /*$("#webdev_form").submit();*/
                //$(this).unbind('click',uzklausaClick);
            };
            $(".butt_submit").bind ('click', uzklausaClick);
            $(".text_input, .input_textarea, select").focus(function(){
                $(this).addClass("focused");
            });
            
            $(".text_input, .input_textarea, select").blur(function(){
                $(this).removeClass("focused");
            });            
       }    

    
    $("#log_container").bind("click", function(e){
        ShowLog();
    });
    
    if ($("#login_form")) {
    
        $(".login_input").bind("click blur focus", function(e){
            if ($(this).val() == "Prisijungimo vardas" || $(this).val() == "Password") $(this).val("");
        });
        
        $("#login_form").submit(function() {
            var post_data = $("#login_form").serialize();
            $.post("index.php?page=users&act=check_login&ajax=1", post_data, function(data){

                    if ($.trim(data) == "") {
                        $("#user_box").load("index.php?page=users&act=login&ajax=1");
                    } else {
                        ShowLog();
                        $("#log").html(data);
                    }
            
            });
            return false;
        });

    }
    
    
    if ($("#AddToCartButton"))
    $("#AddToCartButton").click(function(){

        var post_data = $("#catalog_item_form").serialize();
            
        $.post("index.php?page=cart&act=insert&ajax=1", post_data, function(data){
        
              $("#cart_box").load("index.php?page=cart&act=show_box&ajax=1");
              
        });
        
        }
        
    );
    

    if ($("#CompareButton"))
    $("#CompareButton").click(function(){

            var post_data = $("#catalog_item_form").serialize();
                
            $.post("index.php?page=cart&act=add_compare&ajax=1", post_data, function(data){
            
                    if ($.trim(data) == "") {
                        $("#compare_box").load("index.php?page=cart&act=show_compare_box&ajax=1");
                    } else {
                        ShowLog();
                        $("#log").html(data);
                    }
            
                  
                  
            });
        
    });
    

    
    if ($("#order_form"))
    $("#order_form").submit(function() {
        
            var post_data = $("#order_form").serialize();
            
            $.post("index.php?page=cart&act=check_delivery_data&ajax=1", post_data, function(data){
            
                    if ($.trim(data) == "") {
                        

                        window.location.href=$("#order_form").attr("action");
                        
                    } else {
                        ShowLog();
                        $("#log").html(data);
                    } 
                    
                    

            });

            return false;

    });
    
    
    
    if ( $("#password_form"))
    $("#password_form").submit(function() {
        
            var post_data = $("#password_form").serialize();
            
            $.post("./lt/mod/users/recover_password.htm?ajax=1", post_data, function(data){

                    if ($.trim(data) == "") {
                        $("#log_inline").load("index.php?page=users&act=message&ajax=1&password_sent=1");
                        $("#password_form").fadeOut("slow");
                        
                    } else {

                        $("#log_inline").html(data);
                        
                    }
            
            });
            return false;
    });
    
    
    
    
    if ($("#favoritesForm")) {
        $("#favoritesForm").submit(function() {

            var post_data = $("#favoritesForm").serialize();
            $.post("index.php?page=cart&act=add_favorites&ajax=1", post_data, function(data){

                    if ($.trim(data) == "") {
                    
                        var submit_url = $("#submit_url").val();
                        window.location.href = submit_url;

                    } else {
                        
                        $("#log").html(data);
                        ShowLog();
                    }
            
            });
            return false;
        });
        
    }
    
    
    
    if ($(".cart_summary")) {
    
        $(".cart_summary").submit(function() {
        
            if ($("#agree").attr("checked") == false) return false;
            
            var post_data = $("#cart_form").serialize();
            $.post("index.php?page=cart&act=check_summary&ajax=1", post_data, function(data){

                    
                    window.location.href=$(".cart_summary").attr("action");
                    if ($.trim(data) == "") {
                    
                        window.location.href=$(".cart_summary").attr("action");

                    } else {
                        
                        $("#log").html(data);
                        ShowLog();
                    }
            
            });
            return false;
        });
        
    }
    
    
    
    
    if ($("#registration_form")) {

    
            if ($("#is_new_registration").val() != "1") {
                if ($("input[name='buyer_type']").val() == "1") {
                    highlight_1();
                } else if ($("input[name='buyer_type']").val() == "2") {
                    highlight_2();
                }
            }
    
            $("#registration_form").submit(function() {
            
            
                if (($("#regnewmod").val() == "1") && ($("#agree").attr("checked") == false)) return false;
                
                
            
                var post_data = $("#registration_form").serialize();
                
                $.post("index.php?page=users&act=check_registration&ajax=1", post_data, function(data){

                        if ($.trim(data) == "") {
                        
                            var is_new_reg = $("#is_new_registration").val();
                            
                            if (is_new_reg == 1) {
                                $("#log_inline").load("./lt/mod/users/message.htm?ajax=1&user_registration_ok=1");
                                $("#registration_form").fadeOut("slow");
                            } else {
                                $("#log_inline").load("./lt/mod/users/message.htm?ajax=1&user_update_ok=1");
                                $("#registration_form").fadeOut("slow");
                            }

                        } else {
                            ShowLog();
                            $("#log").html(data);
                        } 

                });

                return false;

        });
        
    }
        

});


function ShowLog() {

    if ($("#log_container").css("display") != "none") {
        $("#log_container").fadeOut();
    } else {
        $("#log_container").fadeIn();
    }

}









function RemoveFilter(Id) {
    
    $(".filter_input_"+Id).attr("value", "");
    $(".filter_input_"+Id).attr("checked", "");
    
    $("#OptName"+Id+" span").removeClass("active");

}





function activateFilter(Id) {

        var check_size = 0;
        check_size = $("#OptVal"+Id).find("input[@type=checkbox][@checked]").size();
        check_size2 = $("#OptVal"+Id).find("input[@type=text][value!='']").size(); 
        check_size = check_size + check_size2;
        
        if (check_size > 0) {
            $("#OptName"+Id+" span").addClass("active");
        } else {
            $("#OptName"+Id+" span").removeClass("active");
        }
        
}







function SelectFilter(Id) {

    if ($("#OptVal"+Id)) {
        
        if ($("#OptVal"+Id).hasClass("vis") == false) {
            
            $(".OptName span").removeClass("clicked");
            $(".OptVal").removeClass("vis");
            
            el_offset = $("#OptName"+Id).position();
            $("#OptVal"+Id).css("top", el_offset.top + 19);
            $("#OptVal"+Id).css("left", el_offset.left);
            $("#OptVal"+Id).addClass("vis");
            $("#OptName"+Id+" span").addClass("clicked");

        }
        else {
        
            $(".OptName span").removeClass("clicked");
            $(".OptVal").removeClass("vis");
            
        }
        
    }

}





    function CheckField(Field, required_fields) {

        if ((( $('[name=' + Field + ']').val() == "" )) && ( InArray(Field, required_fields) )) {
        
            $("#"+Field+"_ast").addClass("status_error");
        
            return true;
            
        } else {
        
            return false;
        
        }

    }
    
    function InArray(needle, haystack, strict) {

        var found = false, key, strict = !!strict;

        for (key in haystack) {
            if ((strict && haystack[key] === needle) || (!strict && haystack[key] == needle)) {
                found = true;
                break;
            }
        }

        return found;
    }


    function CheckForm(required_fields) {
    
        $(".asterix > span").removeClass("status_error");
        var allow_submit = true;
            
        if (required_fields.length == 0) return false;
        
        
        
        
        
        for (i=0;i < required_fields.length; i++) {
            if (CheckField(required_fields[i], required_fields))
            allow_submit = false;
        }
        
        if ($("#in_gift_form").attr("value") == "1") {
        
            if ($("#agreed").attr("checked") == "") allow_submit = false;
        
        }

        
        if (allow_submit) {
            //$(".butt_submit").attr("disabled", "");
            return true; 
        }
        else {
            return false;
        }
    
    }









function ChangeOrder(val) {

    $("input[@name='order']").attr("value", val);

    $("#CatalogForm").submit();
}

function ChangeSort(val) {

    if (val == 'created') $("input[@name='order']").attr("value", 'DESC');
    if (val == 'final_price') $("input[@name='order']").attr("value", 'ASC');
    if (val == 'title') $("input[@name='order']").attr("value", 'ASC');
    if (val == 'no') $("input[@name='order']").attr("value", 'ASC');
    
    $("input[@name='sort1']").attr("value", val);
    $("#CatalogForm").submit();

}

function ChangePerPage(val) {

    $("input[@name='perpage']").attr("value", val);
    $("#CatalogForm").submit();

}



function MakeFavorite(obj, id) {


    if ($(obj).is(':checked')) {
        $.post("index.php?page=users&ajax=1&act=insert_favorite&id=" + id, '', function(data){ });    
    } else {
        $.post("index.php?page=users&ajax=1&act=remove_favorite&id=" + id, '', function(data){ });
    }

    return false;
    
}

function UseBonusPoints(obj) {

    if ($(obj).is(':checked')) {
        $.post("index.php?page=cart&ajax=1&act=bonus_points&use=1", '', function(data){
        
        if ($("#cart_main")) $("#cart_main").load("index.php?page=cart&ajax=1");
        
        });    
    } else {
        $.post("index.php?page=cart&ajax=1&act=bonus_points&use=0", '', function(data){
        if ($("#cart_main")) $("#cart_main").load("index.php?page=cart&ajax=1");
        });
    }
    
    
       
    
}

function RemoveFavorite(id) {

    $.post("index.php?page=users&ajax=1&act=remove_favorite&id=" + id, '', function(data){
        $("#maintext").load("index.php?page=users&act=favorites&ajax=1");
    });

}

function remove_cart_item(item_nr) {
        $.post("index.php?page=cart&act=remove_item&id=" + item_nr, '', function(data){
            if ($("#cart_box")) $("#cart_box").load("index.php?page=cart&act=show_box&ajax=1");
            if ($("#cart_main")) $("#cart_main").load("index.php?page=cart&ajax=1");
       
        });
}


function remove_compare_item(item_nr) {
        $.post("index.php?page=cart&act=remove_compare_item&id=" + item_nr, '', function(data){
            if ($("#compare_box")) $("#compare_box").load("index.php?page=cart&act=show_compare_box&ajax=1");
            if ($("#compare_main")) $("#compare_main").load("index.php?page=cart&act=compare&ajax=1");
       
        });
}



function MakeImgActive(no) {

    var sel_title = $("#gallery_img_title_"+no).val();
    var sel_img_path = $("#gallery_img_path_"+no).val();
    var sel_thumb_path = $("#gallery_thumb_path_"+no).val();
    
    $("#gallery_main_img").attr("title", sel_title);
    $("#gallery_main_img").attr("href", sel_img_path);
    $("#gallery_main_img").css("background-image", 'url('+sel_thumb_path+')');

    $(".miniImg").attr("rel", "efor");
    $("#miniImg"+no).attr("rel", "");

    $(".miniImg").removeClass("activeImg");
    $("#miniImg"+no).addClass("activeImg");

}

function UpdateCart() {

        var post_data = $("#cart_form").serialize();
        $.post("index.php?page=cart&act=update_counts&ajax=1", post_data, function(data){
        
        if ($("#cart_box")) $("#cart_box").load("index.php?page=cart&act=show_box&ajax=1");
        if ($("#cart_main")) $("#cart_main").load("index.php?page=cart&ajax=1");
       
    });
    
}


function OfferSubmit() {

    var post_data = $("#catalog_item_form").serialize();
    
    $.post("index.php?page=cart&act=offer_price&ajax=1", post_data, function(data){
            ShowLog();
            $("#log").html(data);
    });
    
}


function open_wnd(exp_bar,width,height,url,status_bar,scrolls) {
        winName = "_blank";
        theURL = url;
        page_height = (height != '')?height:600;
        page_width = (height != '')?width:500;
        toolbar = (exp_bar == 1)?'yes':'no';
        scrollbars = (scrolls == 1)?'yes':'auto';
        statusbar = (status_bar == 1)?'yes':'no';
        page_top = Math.round((screen.height-page_height)/2);
        page_left = Math.round((screen.width-page_width)/2);
        size = "width="+page_width+",height="+page_height+",top="+page_top+",left="+page_left;
        features = 'status='+statusbar+',toolbar='+toolbar+',scrollbars='+scrollbars+',resizable=yes,'+size;
        window.open(theURL,winName,features);
}

function CartOpenTab3() {
    $("#order_form").submit();
}

function highlight_1() {
    $("#field5, #field6, #field7, #field9, #field10, #field8, #field11, #field0b").addClass("inactive");
    $("#field1, #field2, #field15, #field0a").removeClass("inactive");
}

function highlight_2() {
    $("#field1, #field2, #field9, #field10, #field11, #field15, #field0a").addClass("inactive");
    $("#field5, #field6, #field7, #field8, #field11, #field0b").removeClass("inactive");
}

function show_discount_code(item) {
            
            if ($(item).attr("checked")) {
                $("#discount_input").fadeIn();
            } else {
                $("#discount_input").fadeOut();
            }
}






    function AddToCart(id) {
    
        $("#product_id").attr("value", id);

        var post_data = $("#CatalogForm").serialize();
            
        $.post("index.php?page=cart&act=insert&ajax=1", post_data, function(data){
        
              $("#cart_box").load("index.php?page=cart&act=show_box&ajax=1");
              
        });
        
    }
        
        
        
        

    function Compare(id) {
    
        $("#product_id").attr("value", id);

            var post_data = $("#CatalogForm").serialize();
                
            $.post("index.php?page=cart&act=add_compare&ajax=1", post_data, function(data){
            
                    if ($.trim(data) == "") {
                        $("#compare_box").load("index.php?page=cart&act=show_compare_box&ajax=1");
                    } else {
                        ShowLog();
                        $("#log").html(data);
                    }
            
                  
                  
            });
        
        }
        
        






function styleCheckboxesAndRadiobuttons() {


    var elements = document.getElementsByTagName('input');
    for (i = 0; i < elements.length; i++) {
        if (elements[i].type == 'checkbox' || elements[i].type == 'radio') {    
            switch (elements[i].className) {

                case 'FavoriteCheckbox':
                    createAlternateCheckboxOrRadiobutton(elements[i], i);
                break;

            }
        }
    }
}

function createAlternateCheckboxOrRadiobutton(element, num) {
    var defaultClass = element.className + '-default';
    // Hide the original checkbox.
    element.style.display='none';
    
    // creates the new alternate checkbox
    var altElement = document.createElement('div');
    
    if (element.checked == true) {
        altElement.className = element.className;
    } else {
        altElement.className = defaultClass;
    }
    
    altElement.id = 'alt-' + element.id;
    

    
    element.parentNode.appendChild(altElement);

    // Handles onclick event.
    altElement.onclick = function(){
    
        if (element.checked != true) {
            if (element.type == 'radio') {
              handleOnClickForRadiobuttons(element.name);
            }
            altElement.className = element.className;
            element.checked = true;
            $.post("index.php?page=users&ajax=1&act=insert_favorite&id=" + element.value, '', function(data){     });    
        } else {
            if (element.type == 'radio') {
                handleOnClickForRadiobuttons(element.name);
            }
            altElement.className = defaultClass;
            element.checked = false;
            $.post("index.php?page=users&ajax=1&act=remove_favorite&id=" + element.value, '', function(data){     });
        }

        
    }
    
    
    
}

function handleOnClickForRadiobuttons(name) {
    var radiobuttons = document.getElementsByName(name);
    for (i = 0; i < radiobuttons.length; i++) {
            if (radiobuttons[i].type == 'radio') {
            var altRadiobutton = document.getElementById('alt-' + radiobuttons[i].id);
            altRadiobutton.className = radiobuttons[i].className + '-default';
        }
    }
}




/**
* hoverIntent r5 // 2007.03.27 // jQuery 1.1.2+
* <http://cherne.net/brian/resources/jquery.hoverIntent.html>
* 
* @param  f  onMouseOver function || An object with configuration options
* @param  g  onMouseOut function  || Nothing (use configuration options object)
* @author    Brian Cherne <brian@cherne.net>
*/
(function($){$.fn.hoverIntent=function(f,g){var cfg={sensitivity:7,interval:100,timeout:0};cfg=$.extend(cfg,g?{over:f,out:g}:f);var cX,cY,pX,pY;var track=function(ev){cX=ev.pageX;cY=ev.pageY;};var compare=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);if((Math.abs(pX-cX)+Math.abs(pY-cY))<cfg.sensitivity){$(ob).unbind("mousemove",track);ob.hoverIntent_s=1;return cfg.over.apply(ob,[ev]);}else{pX=cX;pY=cY;ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}};var delay=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);ob.hoverIntent_s=0;return cfg.out.apply(ob,[ev]);};var handleHover=function(e){var p=(e.type=="mouseover"?e.fromElement:e.toElement)||e.relatedTarget;while(p&&p!=this){try{p=p.parentNode;}catch(e){p=this;}}if(p==this){return false;}var ev=jQuery.extend({},e);var ob=this;if(ob.hoverIntent_t){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);}if(e.type=="mouseover"){pX=ev.pageX;pY=ev.pageY;$(ob).bind("mousemove",track);if(ob.hoverIntent_s!=1){ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}}else{$(ob).unbind("mousemove",track);if(ob.hoverIntent_s==1){ob.hoverIntent_t=setTimeout(function(){delay(ev,ob);},cfg.timeout);}}};return this.mouseover(handleHover).mouseout(handleHover);};})(jQuery);




