function changeClickableItem(id) {
    if ($("li[id^='item-select-'] a.act").parent().attr('id').split('-')[2] != id) {
        $('.clickable').fadeOut('slow');
        $('.clickable').css('display', 'none');
        $('.left-side li a.act').removeClass('act');
        $('li#item-select-' + id + ' a').addClass('act');
        $('#item-' + id).fadeIn('slow');
        $('#item-' + id).css('display', 'block');
    }
}

function changeTipsItem(id) {
    var curr = $("li[id^='tips-select-'] a.act").parent().attr('id').split('-')[2];
    if (curr != id) {
        $('#tips-box-' + curr).fadeOut('slow');
        $('#tips-box-' + curr).css('display', 'none');
        $('.pagination-tiny li a.act').removeClass('act');
        $('li#tips-select-' + id + ' a').addClass('act');
        $('#tips-box-' + id).fadeIn('slow');
    }
}
function switchProductTab(tab) {
    if (tab == 'comm') {
        $('li#tab-1 a').removeClass('act');
        $('li#tab-2 a').addClass('act');
        $('li#tab-3 a').removeClass('act');
        $('li#tab-4 a').removeClass('act');
        $('#comments-block').fadeIn('fast');
        $('#description-block').hide();
        $('#exe-block').hide();
        $('#lease-block').hide();
        $('.few-comments').hide();
        $('.comments-block-all .comments-list>div').show();
        $('.comments-block-all .tiny-filter').show();
        var comments_all = $('.comments-block-all .comments-list>div');
        var comment_all_holder = $('.comments-block-all .comments-list');
        $('.comments-block-all .all-comments').parent().show();

        if (comments_all.length > 25) {
            $('.comments-block-all .comments-list>div:gt(25)').hide();
            $('.tiny-filter').hide();
            $('.comments-block-all .all-comments').live('click', function() {
                comments_all.slideDown('slow');
                $('.tiny-filter').show();
                $('.comments-block-all .all-comments').parent().hide();
            });
        } else {
            $('.comments-block-all .all-comments').hide();
        }
    } else if (tab == 'desc') {
        $('li#tab-2 a').removeClass('act');
        $('li#tab-3 a').removeClass('act');
        $('li#tab-4 a').removeClass('act');
        $('li#tab-1 a').addClass('act');
        $('#description-block').fadeIn('fast');
        $('#lease-block').hide();
        $('#comments-block').hide();
        $('#exe-block').hide();
        $('.few-comments').show();
    } else if (tab == 'exe') {
        $('li#tab-1 a').removeClass('act');
        $('li#tab-2 a').removeClass('act');
        $('li#tab-3 a').addClass('act');
        $('#exe-block').fadeIn('fast');
        $('#lease-block').hide();
        $('#description-block').hide();
        $('#comments-block').hide();
    } else if (tab == 'lease') {
        $('li#tab-1 a').removeClass('act');
        $('li#tab-2 a').removeClass('act');
        $('li#tab-4 a').addClass('act');
        $('#lease-block').fadeIn('fast');
        $('#exe-block').hide();
        $('#description-block').hide();
        $('#comments-block').hide();
    }
}

function sortByDateAsc(a, b){
    var id_1 = parseInt(a.id.toString().split('-')[2]);
    var id_2 = parseInt(b.id.toString().split('-')[2]);
    return id_1 > id_2 ? 1 : -1;
};

function sortByDateDesc(a, b){
    var id_1 = parseInt(a.id.toString().split('-')[2]);
    var id_2 = parseInt(b.id.toString().split('-')[2]);
    return id_1 < id_2 ? 1 : -1;
};

function sortComments(type) {
    var sorted;
    if (type == 'asc') {
        $('.comments-block-all a.fr.block.tiny-filter').attr('onclick', '').unbind().click(function() { sortComments('desc'); return false; });
        $('.comments-block-all a.fr.block.tiny-filter').html('Seniausi');
        sorted = $(".comments-block-all div[id^='comment-id']").sort(sortByDateAsc);
        delete sorted;
    } else if (type == 'desc') {
        $('.comments-block-all a.fr.block.tiny-filter').attr('onclick', '').unbind().click(function() { sortComments('asc'); return false; });
        $('.comments-block-all a.fr.block.tiny-filter').html('Naujausi');
        sorted = $(".comments-block-all div[id^='comment-id']").sort(sortByDateDesc);
        delete sorted;
    }
    $(".comments-block-all div[id^='comment-id']").remove();
    $(".comments-block-all .comments-list").append(sorted);
    return false;
}

/*
function hideSubmenu() {
    $('.subnav').css('display', 'none');
}
*/

function productAddToCart(id) {
  $.ajax({
            type: "GET",
            url: "/cart/add/" + id + "/" + $('#product-info').val(),
            dataType: "json",
            complete: function() {
                $popup = $('#product_added');
                $popup.find('.add-to-cart-product-ttl').html($('.left-side .ttl-1').text());
                popup_block($popup);
                $('#product_added').show();
            }
        });
    if ($('#warranty').length && $('#warranty').val() != '-') {
      $.ajax({
                type: "GET",
                url: "/cart/add_warranty/" + id + "/" + $('#warranty').val(),
                dataType: "json",
                complete: function() {
                    $popup = $('#product_added');
                    popup_block($popup);
                    $('#product_added').show();
                }
            });
    }
    return false;
}

function warrantyAddToCart(id, months) {
  $.ajax({
            type: "GET",
            url: "/cart/add_warranty/" + id + "/" + months,
            dataType: "json"
        });
    
    $popup = $('#warranty_added');
    popup_block($popup);
    $('#warranty_added').show();
    return false;
}

var popupStatus = 0;
function popup_block($popup)
{
    var windowWidth = document.documentElement.clientWidth;
    var windowHeight = document.documentElement.clientHeight;
    var popupHeight = $popup.height();
    var popupWidth = $popup.width();
        $popup.css({
            "position": "absolute",
            "top": windowHeight/2-popupHeight/2,
            "left": windowWidth/2-popupWidth/2
        });

    if(popupStatus==0){
        $("#backgroundPopup").fadeIn("slow");
        $("#backgroundPopup").css('opacity', 1.0);
        $popup.fadeIn("slow");
        popupStatus = 1;
    }

}

function popup_close($popup){
    if(popupStatus==1){
        $("#backgroundPopup").fadeOut("slow");
        $popup.fadeOut("slow");
        popupStatus = 0;
    }
}


function changeTopOffers() {
    if($(".list-6 a.act").length) {
        var active = $(".list-6 a.act").parent().attr('id').split('-')[2];
        var count = $(".list-6 a").size();
        var next = (active == count) ? 1 : (parseInt(active)+1);
        changeClickableItem(next);
    }
}

jQuery.fn.sort = function() {
   return this.pushStack([].sort.apply(this, arguments), []);
};

$('div .delivery .city .title').live('click', function() {
        if ($(this).parent().find('.address_info:visible').length == 0) {
            $(this).parent().find('.address_info').css('border-bottom', '1px solid #DCE5A7');
            $(this).parent().find('.address_info:last').css('border-bottom', 'none');
            $('div .delivery .city').find('.title').css('border-bottom', 'none');
            $('div .delivery .city').find('.address_info:visible').slideUp();
            $(this).parent().find('.address_info').slideDown();
            $(this).parent().find('.title').css('border-bottom', '1px solid #DCE5A7');
            var coord = $(this).parent().find('.coord').val();
            if (coord) {
                    if (GBrowserIsCompatible()) {
                        map = new GMap2($(this).parent().find('.map')[0]);
                        map.setCenter(new GLatLng(coord.split(',')[0], coord.split(',')[1]), 15); 
                        map.addControl(new GSmallZoomControl());
                    var marker = new GMarker(new GLatLng(coord.split(',')[0], coord.split(',')[1]));
                    map.addOverlay(marker);
                    }
            }
        } else {
            $(this).parent().find('.address_info').slideUp();
            $('div .delivery .city').find('.title').css('border-bottom', 'none');
        }
});

function popup_close($popup){
    if(popupStatus==1){
        $("#backgroundPopup").fadeOut("slow");
        $popup.fadeOut("slow");
        popupStatus = 0;
    }
}

$(function() {
    $('.offer-price .tac a').click(function() {
        $.ajax({
            type: "GET",
            url: "/offer_price/?id=" + $('#product-info').attr('name').split('_')[1] + '&price=' + $('#offer-opt-1').val() + '&email=' + $('#offer-opt-2').val() + '&check=' + $('#offer-chk').attr('checked')
        });
        popup_close($('.offer-price'));
    });
});

