$(document).ready(function(){
    $("#sidebar ul.categories li ul").css({"display" : "none"});
    $("#sidebar ul.categories li a.active").parent().find("ul").css({"display" : "block"});
    
    $("#reg_user").submit(function(){
        var flag = false;
        $("#reg_user input").each(function(){
            if($(this).val() == ""){
                $(this).unbind();
                $(this).css({"border": "red 1px solid"});
                if($(this).attr("type") != "password") $(this).val("Это поле не может быть пустым");
                $(this).click(function(){$(this).val("")});
                $(this).change(function(){if($(this).val() != ""){ $(this).css({"border":"1px #777 solid"}); $(this).unbind(); }});
                flag = true;
            }
        });
        if(flag == true) return false;
    });
    
    
    $("#sidebar ul.categories li a").click(function(){
        
        if($(this).hasClass("active")){
            $(this).parent().find("ul").css({"display" : "none"});
            $(this).removeClass("active");
        }
        else{
            $(this).parent().find("ul").css({"display" : "block"});
            $(this).addClass("active");
        }
        $("#sidebar ul.categories li ul li a").unbind();
        
        return false;
    });
    
    $("ul.catalog li").click(function(){
        document.location = $(this).find("a").attr("href");
    });
    
    
    $("#sidebar ul.categories li ul li a").unbind();
    
    // Корзина -->
    $("#add_basket").click(function(){
        var params = "";
        $("#params select").each(function(){
            params = params + $(this).attr("name") + ":" + $(this).val() + ";";
        });
        $.get("/eshop/json_add_to_basket/" + $("#itemId").val() + "/1/" + params, "");
        $.get("/eshop/custom_get_basket/", "", UpdateBasket);
        
        return false;
    });
    
    $("a.remove").click(function(){
        var id = $(this).attr("rel");
        id = id.split("_");
        id = id[1];
        
        $.get("/eshop/custom_remove_from_basket/"+id, "", UpdateBasket);
        
        return false;
    });
    
    $("input.count_change").change(function(){
        changeCount(this);
    });
    
    $("#order_form").submit(function(){
        if(parseInt($("input[name=delivery_address][checked]").val())  > 0 ){
        }
        else{
            $("input[name=delivery_address]").parent().parent().css({"border" : "1px solid red"});
            $("input[name=delivery_address]").parent().parent().append("<li class = \"uwaga\">Выберите адрес доставки.</li>");
            return false;    
        }
        
        
    });
    
    //<--
    
    // Галерея -->
    $("div.photos a[rel=thumb]").click(function(){
        $("body").prepend("<div class = \"overlay\"></div>");
        var pageSize = getPageSize();
        $("div.overlay").css({"height": pageSize[1] + "px"});
        $("#content").prepend("<div class = \"big_photo\"><a class = \"close\" href = \"#\" title = \"Закрыть окно\">Закрыть</a> <img id = \"big_photo\" src = \"/images/blank.gif\" alt = \"\"/> <div id = \"thumbs\"></div></div>");
        $("#big_photo").attr({"src" : $(this).attr("href")});
        $("div.big_photo").css({"display" : "block"});
        var thumbs = "";
        $("#thumbs").empty();
        $("a[rel=thumb]").each(function(){
            $("#thumbs").append($(this).clone());
        });
        $("#thumbs a[rel=thumb]").click(function(){
            $("#big_photo").attr({"src" : $(this).attr("href")});
            return false;
        });
        $("div.big_photo .close").click(function(){
            $("div.overlay").remove();
            $("div.big_photo").remove();
            return false;
        });
        
        return false;
    });
    $(window).resize(function(){
        var pageSize = getPageSize();
        $("div.overlay").css({"height": pageSize[1] + "px"});
    });
    // <--
    
});
    
function UpdateBasket(data, status){
    if($(data).find("total").text() == "0"){
        $("#basket").html("<p>Корзина пуста</p><ul class = \"basket\"></ul>");
        $("div.basket_details table").remove();
        $("div.basket_details").append("<p>Корзина пуста</p>");
    }
    else{
        var listItem = "";
        var tableItem = "";
        $(data).find("item").each(function(){
            listItem += "<li> \n<a href=\"" + $(this).find("element_path").text() + "\">" + $(this).find("name").text() + "</a> \n<a rel = \"rem_" + $(this).find("id").text() + "\" href=\"#\" class = \"remove\"><img src = \"/images/shop_cancel_button.gif\" alt = \"удалить\"/></a> \n <br /> " + $(this).find("price").text() + " р., " + $(data).find("icount").text() + " шт. \n </li>";
            tableItem += "<tr><td>" +
                    "<a rel = \"rem_" + $(this).find("id").text() + "\" href=\"#\" class = \"remove\"><img src = \"/images/shop_cancel_button.gif\" alt = \"удалить\"/></a>" +
                    "</td>" +
                    "<th><a href=\"" + $(this).find("element_path").text() + "\">" + $(this).find("name").text() + "</a></th>" +
                    "<th>" + $(this).find("comments").text() + "</th>" +
                    "<td>" + $(this).find("price").text() + " р.</td>" +
                    "<td><input type = \"text\" class = \"count_change\" name = \"count_" + $(this).find("id").text() + "\" value = \"" + $(this).find("icount").text() + "\" /></td>" +
                    "<td>" + $(this).find("price_total").text() + " р.</td></tr>";            
        });
        $("ul.basket").html(listItem);
        $("div.basket_details table tbody").html(tableItem);
        
        if(parseInt($("span#total").html()) >= 1){
            $("span#total").html($(data).find("total").text());
            $("#total_details").html($(data).find("total").text() + " р.");
            
            $(".delivery").html($(data).find("delivery").text());
        }
        else{
            $("div#basket").append("<div class = \"total\">Итого: <span id = \"total\">" + $(data).find("total").text() + "</span> р.</div><a href = \"/basket/\"><img src = \"/images/shop_order_button.gif\" alt = \"Перейти к оформлению\" /></a>");
        }
        
        $("div#basket").find("p").remove();
        
        $('a.remove').unbind();
        $('a.remove').click(function(){
                
                var id = $(this).attr("rel");
                id = id.split("_");
                id = id[1];
                $.get("/eshop/custom_remove_from_basket/"+id, "", UpdateBasket);
                
                return false;
        });
        
        $("input.count_change").change(function(){
            changeCount(this);
        });
        
    }
}


function changeCount(inputObj){
    var new_count = 1;
    var id = $(inputObj).attr("name").split("_");
    id = id[1];
        
    if(parseInt($(inputObj).val()) > 0){
        new_count = parseInt($(inputObj).val());
    }
    else{
        if(parseInt($(inputObj).val()) == 0){
            $.get("/eshop/custom_remove_from_basket/"+id, "", UpdateBasket);
            return 0;
        }
        else{
            new_count = 1;    
        }
            
    }
    $.get("/eshop/custom_change_count/"+id+"/"+new_count, "", UpdateBasket);
    return 0;
}


function getPageSize(){
    var xScroll, yScroll;
   
    if (window.innerHeight && window.scrollMaxY) {
       xScroll = window.innerWidth + window.scrollMaxX;
       yScroll = window.innerHeight + window.scrollMaxY;
    } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
       xScroll = document.body.scrollWidth;
        yScroll = document.body.scrollHeight;
    } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
        xScroll = document.body.offsetWidth;
        yScroll = document.body.offsetHeight;
    }
   
    var windowWidth, windowHeight;
   
    if (self.innerHeight) { // all except Explorer
        if(document.documentElement.clientWidth){
           windowWidth = document.documentElement.clientWidth;
        }
        else {
           windowWidth = self.innerWidth;
        }
    windowHeight = self.innerHeight;
    }
    else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
       windowWidth = document.documentElement.clientWidth;
       windowHeight = document.documentElement.clientHeight;
    }
    else if (document.body) { // other Explorers
       windowWidth = document.body.clientWidth;
       windowHeight = document.body.clientHeight;
    }
   
    // for small pages with total height less then height of the viewport
    if(yScroll < windowHeight){
       pageHeight = windowHeight;
    }
    else {
       pageHeight = yScroll;
    }
   
   
    if(xScroll < windowWidth){
       pageWidth = xScroll;
    }
    else {
       pageWidth = windowWidth;
    }
   
   
    arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)
    return arrayPageSize;
}
