hs.graphicsDir = '/published/SC/html/scripts/repo_themes/cartrige/highslide/graphics/';
hs.outlineType = 'rounded-white';
hs.wrapperClassName = 'draggable-header';

function addCart(id, orig)
{
    if(!orig) qString = 'option_1=13'; else qString = '';
    
    $.get("/index.php?productID=" + id + "&action=add_product&ukey=cart&simple", qString, function(data){
        data = $.parseJSON(data);
        $("#cartLink").text("Корзина (" + data.count + ")");
        return hs.htmlExpand(document.getElementById("addCart_" + id), { objectType: 'iframe', align: 'center', width: 387 });
    });
}

function selectPhoto(id, photo, photoBig)
{
    $(".thumbImgActive").attr("class", "thumbImg");    
    $("div#thumbImg_" + id).attr("class", "thumbImgActive");
    $("a#photoLink").attr("href", photoBig); 
    $("a#photoLink img").attr("src", photo);        
}

$(function(){

    $("div[id^='productTab_']").css('font-size', '14px');
    $("div[id^='productTab_'] ul").css('padding', '0 0 10px 20px');
    
    $.each($("a[href^='#productTab']"), function(k, v){
        if($(v).text() == "Остерегайтесь подделок")
        {
            $("#productTabs").tabs({selected: k});   
        }
    });
    
    $("#sendOrder").live("click", function(){

        var name  = $("input[name=name]").attr("value");
        var phone = $("input[name=phone]").attr("value").match(/^([-0-9+\(\)\s]+)$/);

        if(!name)
        {
            $("#orderErrorPhone").hide();
            $("#orderErrorName").show();
        }
        else if(phone == null)
        {
            $("#orderErrorName").hide();
            $("#orderErrorPhone").show();
        }
        else
        {
            var qString = $("#orderForm").serialize();
            $.post("/ajax/sendOrder.php", qString, function(data){
                $('#response').html(data);
            });
        }
        
        return false;
    });
    
    $("#sendCallBackForm").live("click", function() {
        
        var name = $("input[name=name]").attr("value");
        var phone = $("input[name=phone]").attr("value").match(/^([-0-9+\(\)\s]+)$/);

        if(!name)
        {
            $("#callBackFormErrorPhone").hide();
            $("#callBackFormErrorName").show();
        }
        else if(phone == null)
        {
            $("#callBackFormErrorName").hide();
            $("#callBackFormErrorPhone").show();
        }
        else
        {
            var qString = $("#callBackForm").serialize();
            $.post("/ajax/sendCallBackForm.php", qString, function(data){
                $('#response').html(data);
            });
        }
        
        return false;    
    });
    
    $('#infoFirstName').change(function(){
        $("#shippingFirstName").attr("value",$(this).attr("value"));        
    });
    
    $('#infoLastName').change(function(){
        $("#shippingLastName").attr("value",$(this).attr("value"));        
    });
    
    $('form input.text_search').each(function() {
        InputHelperCreate(this, this.title);
    });

    $('input.inputSuggest').each(function() {
        InputHelperCreate(this, this.title);
    });

    $(".first.tabNone").live("click", function(){
        $(".second.tabAct").attr("class", "second tabNone");
        $(this).attr("class", "first tabAct");
        $("#tabs-2").hide();
        $("#tabs-1").show();
    });

    $(".second.tabNone").live("click", function(){
        $(".first.tabAct").attr("class", "first tabNone");
        $(this).attr("class", "second tabAct");
        $("#tabs-1").hide();
        $("#tabs-2").show();
    });

	//$(".tdPrice").hover(function(){$(this).css('background', '#F5F0F0')}, function(){$(this).css('background', '#F5F5F5')});

    if($("#regCheck").is(":checked"))
    {
        $("#newUser").show();
    }

    $("#regCheck").click(function(){
        if($(this).is(":checked"))
        {
            $("#newUser").show();
        }
        else
        {
            $("#newUser").hide();
        }
    });
    
    $("#allSearch").autocomplete({
        source: function( request, response ) {
            $.ajax({
                url: "/ajax/allSearch.php?categoryID=" + $("#categoryID").attr("value"),
                dataType: "json",
                data: {
                    query: request.term
                },
                success: function( data ) {
                    response( $.map( data, function( item ) {
                        return {
                            id: item.id,
                            value: item.value,
                            type: item.type
                        }
                    }));
                }
            });
        },
        minLength: 2,
        select: function( event, ui ) {
            
            if(ui.item.type == 1)
            {
                window.location = '/product/' + ui.item.id + '/';
            }
            else
            {
                $.post("/ajax/printerFilter.php?id=" + ui.item.id, function(data) {
                    $(".prevSearchResult").show();
                    $("#searchResult").hide('slow');
                    $("#printerFilterList").hide('slow');
                    $("#printerFilterList").html(data);
                    $("#printerFilterList").show('slow');
                });
            }
        }
    });

    $("#printerFilter").autocomplete({
        source: function( request, response ) {
            $.ajax({
                url: "/ajax/printerSearch.php?categoryID=" + $("#categoryID").attr("value"),
                dataType: "json",
                data: {
                    query: request.term
                },
                success: function( data ) {
                    response( $.map( data, function( item ) {
                        return {
                            id: item.id,
                            value: item.value
                        }
                    }));
                }
            });
        },
        minLength: 2,
        select: function( event, ui ) { 
             $.post("/ajax/printerFilter.php?id=" + ui.item.id, function(data) {
                $(".prevSearchResult").show();
                $("#searchResult").hide('slow');
                $("#printerFilterList").hide('slow');
                $("#printerFilterList").html(data);
                $("#printerFilterList").show('slow');
             });
        }
    });

    $("form.allCartrigeSearch input.text_search").autocomplete({
        source: function( request, response ) {
            $.ajax({
                url: "/ajax/allCartrigeSearch.php",
                dataType: "json",
                data: {
                    query: request.term
                },
                success: function( data ) {
                    response( $.map( data, function( item ) {
                        return {
                                id: item.id,
                                request: request.term,
                                label: item.type + " " + item.value,
                                value: item.id && item.id != "notfound" ? item.value : request.term
                        }
                    }));
                }
            });
        },
        minLength: 2,
        select: function( event, ui ) {            
            if(ui.item.id == "notfound")
            {
                return;    
            }
            else if(ui.item.id == 0)
            {
                window.location = '/search/?searchstring=' + ui.item.request;    
            }
            else
            {
                window.location = '/product/' + ui.item.id + '/';
            }
        }
    });

    $("form.allPrinterSearch input.text_search").autocomplete({
        source: function( request, response ) {
            $.ajax({
                url: "/ajax/allPrinterSearch.php",
                dataType: "json",
                data: {
                    query: request.term
                },
                success: function( data ) {
                    response( $.map( data, function( item ) {
                        return {
                                id: item.id,
                                request: request.term,
                                label: item.type + " " + item.value,
                                value: item.id && item.id != "notfound" ? item.value : request.term
                        }
                    }));
                }
            });
        },
        minLength: 2,
        select: function( event, ui ) {
            if(ui.item.id == "notfound")
            {
                return;    
            }
            else if(ui.item.id == 0)
            {
                window.location = '/search/?printers=1&searchstring=' + ui.item.request;    
            }
            else
            {
                window.location = '/product/' + ui.item.id + '/';
            }
        }
    });

    $(".prevSearchResult").click(function(){
        $("#printerFilter").val("");
        $(this).hide();
        $("#printerFilterList").hide('slow');
        $("#searchResult").show('slow');
        return false;
    });

    //hover states on the static widgets
    $('#dialog_link, ul#icons li').hover(
        function() { $(this).addClass('ui-state-hover'); },
        function() { $(this).removeClass('ui-state-hover'); }
    );
    
    $("#getWarning").click(function(){
        
        if(!$("#warning").text())
        {
            $.get("/ajax/getWarning.php", "productID=" + $(this).attr("rel"), function(data){
                $("#warning").html(data).show("slow");
            });    
        }
        else
        {
            $("#warning").show("slow");
        }
                
        return false;
    });
    
    $("#hideWarning").live("click", function(){
        $("#warning").hide("slow");
                
        return false;    
    });
});


function InputHelperIn ( obj, text )
{
    //если при фокусе значение поля равно значению подсказки, то чистим его и вешаем стили
    if ( obj.value == text )
    {
        $( obj )
            .css ( { color: '#000', fontStyle: 'normal' } )
            .val ( '' );
    }
}

function InputHelperOut ( obj, text )
{
    //если при потере фокуса значение поля равно пустоте или значению по умолчанию,
        //то пихаем в него текст подсказки и вешаем стили подсказки
    if ( obj.value == '' || obj.value == text )
    {
        $( obj )
            .css ( { color: '#b3b3b3', fontStyle: 'italic' } )
            .val ( text );
    }
}

function InputHelperCreate ( obj, text )
{
    //вешаем на поле эвенты. На фокус и потерю фокуса.
    $( obj )
        .bind ( 'focus', function () {
            InputHelperIn ( this, text );
        } )
        .bind ( 'blur', function () {
            InputHelperOut ( this, text );
        } );

    //первоначальный инит
    InputHelperOut ( obj, text );
}
