/* Для специального JS-кода конкретных пректов */
function InitizlizeAnimatedLogo()
{
    swfobject.embedSWF("/swf/header_logo.swf", "animated_logo", 1000, 173, "9.0.115.0", "",
    {
        wmode: "transparent"
    },
    {
        wmode: "transparent"
    }
    );    
}

/**
* @todo Check this out. 
*/
function SetOverlayAlbumPosition()
{
    $("div.overlay_album:visible").css('top', $(window).scrollTop()-440);
}

function CloseOverlay()
{
    var url = location.href.split('#');
    if(url.length == 2)
        location.href = url[0];
    else
        location.reload();
}

function InitializeCollectorsAndCatalogue()
{
    if(jQuery.browser.msie && (jQuery.browser.version.substr(0,1) == "6"))
        {
        return;
    }

    $("div.album_image_loupe").each(function(){
        $(this).css("left", ($(this).parent().children("a").children("img").offset().left - $(this).parent().offset().left + 10) + "px");
    });

    $("a.for_collectors").click(function(){
        $("#main_overlay").show().attr("rel", $(this).attr("rel"));
        $($(this).attr("rel")).show();
        SetOverlayAlbumPosition();
        return false;
    });

    $("a.for_catalogue").click(function(){
        $("#main_overlay").show().attr("rel", $(this).attr("rel"));
        $($(this).attr("rel")).show();
        SetOverlayAlbumPosition();
        jQuery.ajax({
            type: "post",
            dataType: "json",
            data: "script=set_clicks&shopId="+1+"&itemId="+$(this).attr("rel").slice(15),
            url: "/ajax/",
            success: function(data, textStatus)
            {
            },
            error: function()
            {
                
            }
        });
        return false;
    });



    $(window).scroll(function () { 
        // SetOverlayAlbumPosition();
    });

    var url = location.href.split('#');
    if(url.length == 2)
        {
        url = url[1].split('-');
        if(url.length == 2 && url[0] == 'select')
            {
            $("a[rel=#overlay_album_"+url[1]+"]").click();
        }
    }
    /***/


    $("#main_overlay_switch_to_dark").click(function(){
        $("#main_overlay").removeClass("overlay_light").addClass("overlay_dark");
        $("div.overlay_album").removeClass("light_overlay_album").addClass("dark_overlay_album");
        $("div.overlay_album_small_photos").removeClass("light_overlay_album_small_photos").addClass("dark_overlay_album_small_photos");
        $("div.active_overlay_album_small_photo").parent().removeClass("light_overlay_album_small_photos").addClass("dark_overlay_album_small_photos");
        /*$("div.active_overlay_album_small_photo").css('background', 'url("/img/overlay/active.bg.dark.gif") center bottom no-repeat')*/
        return false;
    });

    $("#main_overlay_switch_to_light").click(function(){
        $("#main_overlay").removeClass("overlay_dark").addClass("overlay_light");
        $("div.overlay_album").removeClass("dark_overlay_album").addClass("light_overlay_album");
        $("div.overlay_album_small_photos").removeClass("dark_overlay_album_small_photos").addClass("light_overlay_album_small_photos");
        $("div.active_overlay_album_small_photo").parent().removeClass("dark_overlay_album_small_photos").addClass("light_overlay_album_small_photos");
        /*$("div.active_overlay_album_small_photo").css('background', 'url("/img/overlay/active.bg.light.gif") center bottom no-repeat')*/
        return false;
    });

    //$("#main_overlay_close_button").click(function(){
    $("div.close_button").click(function(){
        $("#main_overlay").hide();
        $($("#main_overlay").attr("rel")).hide();
    });


    $(".main_overlay_close_button_for_catalogue").click(function(){
        CloseOverlay();
    });

    $("div.overlay_album_small_photo a").click(function(){
        $(this).parent().parent().parent().children("div.overlay_album_big_photo").children("img").attr("src", $(this).children("img").attr("rel"));
        $(this).parent().parent().children("div.active_overlay_album_small_photo").removeClass("active_overlay_album_small_photo");
        $(this).parent().addClass("active_overlay_album_small_photo");
        return false;
    });

    $("div.catalogue_item_image").mouseenter(function(){
		var id = $(this).children('a:visible').attr('data-id');
        $(this).children("div.catalogue_item_hidden_image[data-id="+id+"]").show();
    });

    $("div.catalogue_item_image").mouseleave(function(){
		var id = $(this).children('a:visible').attr('data-id');
        $(this).children("div.catalogue_item_hidden_image[data-id="+id+"]").hide();
    });
	
	$('.catalogue_item_buttons a').click(function(){
		$(this).parent().children('a').removeClass('active');
		$(this).addClass('active');
		$(this).parent().parent().children('.catalogue_item_image').children('a').hide();
		$(this).parent().parent().children('.catalogue_item_image').children('a[data-id=' + $(this).attr('data-id') + ']').show();
		$(this).parent().parent().children('.catalogue_item_title').children('.catalogue_item_price').children('a').attr('data-id', $(this).attr('data-id')).attr('href', '/v-roznitsu/cart/?action=add&item_id=' + $(this).attr('data-id'));
		return false;
	});
	
	$('.catalogue_overlay_item_buttons a').click(function(){
		$(this).parent().children('a').removeClass('active');
		$(this).addClass('active');
		$(this).parent().parent().children('img').hide();
		$(this).parent().parent().children('img[data-id=' + $(this).attr('data-id') + ']').show();
		$(this).parent().parent().parent().find('.overlay_buy').children('a').attr('data-id', $(this).attr('data-id')).attr('href', '/v-roznitsu/cart/?action=add&item_id=' + $(this).attr('data-id'));
		return false;
	});
	
	$('.catalogue_item_details_buttons a').click(function(){
		$(this).parent().children('a').removeClass('active');
		$(this).addClass('active');
		$(this).parent().parent().children('a').hide();
		$(this).parent().parent().children('a[data-id=' + $(this).attr('data-id') + ']').show();
		$('.catalogue_item_details_buy').attr('data-id', $(this).attr('data-id')).attr('href', '/v-roznitsu/cart/?action=add&item_id=' + $(this).attr('data-id'));
		return false;
	});

    $("div.catalogue_group").mouseenter(function(){
        $(this).addClass("hover_catalogue_group");
    });

    $("div.catalogue_group").mouseleave(function(){
        $(this).removeClass("hover_catalogue_group");
    });

    $("div.main_overlay_for_catalogue").click(function(){
        /*$(this).hide();
        $("div.overlay_album").hide();*/
        CloseOverlay();
    });

    $("div.main_overlay_for_collectors").click(function(){
        $(this).hide();
        $("div.overlay_album").hide();
    });
}

function InitializeTables()
{
    $("#content_wrapper_td table.shop_cart_table tr:last-child").addClass("last_tr");
}

function InitializeLeftCatalogue()
{
    if(jQuery.browser.msie && (jQuery.browser.version.substr(0,1) == "6" || jQuery.browser.version.substr(0,1) == "7"))
        {
        $("#left_catalogue div.menu_item, #left_catalogue div.submenu_item").unbind("mouseenter");
        $("#left_catalogue div.menu_item, #left_catalogue div.submenu_item").unbind("mouseleave");
    }
}

/*function InitializeRightBasket()
{
$("#right_basket div.content").each(function(){
$(this).css("height", ($(this).height() + $("#little_cart").height()) + "px")
});
}*/

function InitializeOnPageSelect()
{
    $("#on_page").change(function(){
        $("#filter_form").submit();
    });
}

function InitializeOrderForm()
{
    $("#address_step_2").submit(function(){
        var errorMessage = '';

        if($('#country').val() == 0)
            errorMessage += 'Выберите страну.<br/>';
        if($('#location').val() == 0)
            errorMessage += 'Выберите область.<br/>';
        if($('#sel_city').val() == 0)
            errorMessage += 'Выберите город.<br/>';
        if($('#full_address').val().length === 0)
            errorMessage += 'Введите улицу, дом, квартиру.<br/>';
        if($('#site_users_surname').val().length === 0)
            errorMessage += 'Введите фамилию.<br/>';
        if($('#site_users_name').val().length === 0)
            errorMessage += 'Введите имя.<br/>';
        if($('#site_users_patronymic').val().length === 0)
            errorMessage += 'Введите отчество.<br/>';
        if($('#site_users_phone').val().length === 0)
            errorMessage += 'Введите телефон.<br/>';
        if($('#site_users_email').val().length === 0)
            errorMessage += 'Введите e-mail.<br/>';

        if(errorMessage.length > 0)
            {  
            $("#message").html(errorMessage);
            return false;
        }
    });
}

$(document).ready(function(){
    InitizlizeAnimatedLogo();
    InitializeLeftCatalogue();
    InitializeCollectorsAndCatalogue();
    InitializeTables();
    //InitializeRightBasket();
    InitializeOnPageSelect();
    InitializeOrderForm();
});

