$(document).ready(function(){
	// --------------------------------------------------------------------------
	// табы в карточке товара

	var tabs_num = $("div.tabs > a").length;
	var tabs_hash = document.location.hash;
	if(tabs_hash === "") {tabs_hash = "#tab_0"};
	var current_tab = parseInt(tabs_hash.slice(tabs_hash.indexOf("_") + 1));
	$("div.tabs > a[href='" + tabs_hash + "']").addClass("on");

	$("div.tabs > a").click(function() {
		$("div.tabs > a").removeClass("on");
		$(this).addClass("on");
		tabs_hash = $(this).attr("href");
		current_tab = parseInt(tabs_hash.slice(tabs_hash.indexOf("_") + 1));
		set_tabs();
	});

	function set_tabs() {
		$("div.tab").hide();
		$("#tab_cont_" + current_tab).show();
	}

	set_tabs();

	// --------------------------------------------------------------------------
	// Фотогалерея

	if($("#galery").width() !== null) {

		var gal_pos = 0;
		var gal_current = 1;
		var gal_length = $("#galery_big_scroll > div").length;

		$("#arr_top").click(function(event) {
			event.preventDefault();
			if(gal_current > 1) {
				gal_current--;
				move_pics();
				place_arrs();
			}
		});

		$("#arr_bot").click(function(event) {
			event.preventDefault();
			if(gal_current <= gal_length - 3) {
				gal_current++;
				move_pics();
				place_arrs();
			}
		});

		function move_pics() {
			$("#galery_small_scroll").animate({top:110 * -(gal_current - 1)},300);
		}
		function place_arrs() {
			if(gal_current === gal_length - 2) {
				$("#hide_1").hide();
				$("#hide_2").show();
			} else if(gal_current === 1) {
				$("#hide_1").show();
				$("#hide_2").hide();
			} else {
				$("#hide_1").hide();
				$("#hide_2").hide();
			}
		}
		place_arrs();

		$("#galery_small_scroll > div").click(function(event) {
			$("#galery_small_scroll > div").removeClass("on");
			$(this).addClass("on");
			for(var i = 0; i < gal_length; i++) {
				if($("#galery_small_scroll > div:eq(" + i + ")").hasClass("on")) {gal_pos = i};
			}
			if(gal_pos - 1 === gal_current && gal_current < gal_length - 2) {gal_current++};
			if(gal_pos + 1 === gal_current && gal_current > 1) {gal_current--};

			move_big();
			move_pics();
			place_arrs();
		});

		function move_big() {
			$("#galery_big_scroll").animate({top:375 * -(gal_pos)},400);
		}
	}

	// --------------------------------------------------------------------------
	// Попапчики

	var pop_x = 0;
	var pop_y = 0;

	$("#go_response").click(function(event) {
		event.preventDefault();
		pop_x = $(this).offset().left;
		pop_y = $(this).offset().top;
		hide_pop();
		show_pop("pop_response");
	});

	$("#go_question").click(function(event) {
		event.preventDefault();
		pop_x = $(this).offset().left;
		pop_y = $(this).offset().top;
		hide_pop();
		show_pop("pop_question");
	});
	$("#go_comments").click(function(event) {
		event.preventDefault();
		pop_x = $(this).offset().left;
		pop_y = $(this).offset().top;
		hide_pop();
		show_pop("pop_comments");
	});
	$("a.pop_close").click(function(event) {
		event.preventDefault();
		hide_pop();
	});

	function show_pop(id) {
		$("#" + id).css("left", (pop_x - 550)).css("top", pop_y - 200);
	}

	function hide_pop() {
		$("#pop_question").css("top",-9999);
		$("#pop_comments").css("top",-9999);
		$("#pop_response").css("top",-9999);
	}

	/* ----------------------------------------------------------------------------
	 * Большая всплывающая галерея
	 * 23.06.2010
	 * by IonDen
	 */

	var big_gal_length = 0;
	var big_gal_html = "";
	var big_gal_hover = false;
	var big_gal_pos = 0;
	var big_gal_current = 1;

	$("#open_big_gal").click(function(e) {
		e.preventDefault();
		big_gal_length = $("#big_gal_info > dt").length;
		big_gal_pos = gal_pos;

		big_gal_html = '<div id="big_gal"><div id="big_gal_cont">';
		big_gal_html += '<div id="bg_big"><div id="bg_big_scroll"><table>';
		for(var i = 0; i < big_gal_length; i++) {
			big_gal_html += '<tr><td><img src="' + $("#big_gal_info > dd:eq(" + i + ")").attr("rel") + '" /></td></tr>';
		}
		big_gal_html += '</table></div></div>';
		big_gal_html += '<div id="bg_small"><div id="bg_small_scroll">';
		for(var i = 0; i < big_gal_length; i++) {
			big_gal_html += '<div><img src="' + $("#big_gal_info > dt:eq(" + i + ")").attr("rel") + '" /><b></b></div>';
		}
		big_gal_html += '</div></div>';
		big_gal_html += '<a href="#" class="gal_arr" id="ba_top"></a>';
		big_gal_html += '<a href="#" class="gal_arr" id="ba_bot"></a>';
		big_gal_html += '<div class="arr_hide" id="hide_3"></div>';
		big_gal_html += '<div class="arr_hide" id="hide_4"></div>';
		big_gal_html += '<a href="#" class="pop_close" id="bg_close""><b>&times;</b></a>';
		big_gal_html += '</div></div>';

		$("body").append(big_gal_html);
		$("#bg_big_scroll").css("top", -595 * big_gal_pos);
		$("#bg_small_scroll div:eq(" + big_gal_pos + ")").addClass("on");

		$("#bg_small_scroll > div").click(function() {
			$("#bg_small_scroll > div").removeClass("on");
			$(this).addClass("on");
			for(var i = 0; i < big_gal_length; i++) {
				if($("#bg_small_scroll > div:eq(" + i + ")").hasClass("on")) {big_gal_pos = i};
			}
			if(big_gal_pos - 3 === big_gal_current && big_gal_current < big_gal_length - 4) {big_gal_current++};
			if(big_gal_pos + 1 === big_gal_current && big_gal_current > 1) {big_gal_current--};

			move_bg();
			move_bg_small();
			place_bg_arrs();
		});

		$("#ba_top").click(function(e) {
			e.preventDefault();
			if(big_gal_current > 1) {
				big_gal_current--;
				move_bg_small();
				place_bg_arrs();
			}
		});

		$("#ba_bot").click(function(e) {
			e.preventDefault();
			if(big_gal_current <= big_gal_length - 5) {
				big_gal_current++;
				move_bg_small();
				place_bg_arrs();
			}
		});

		$("#big_gal_cont").hover(
			function() {
				big_gal_hover = true;
			},
			function() {
				big_gal_hover = false;
			}
		);
		$(document.body).mousedown(function() {
			if(big_gal_hover === false) {
				$("#big_gal").remove();
			}
		});
		$("#bg_close").click(function(e){
			e.preventDefault();
			$("#big_gal").remove();
		});

		function move_bg() {
			$("#bg_big_scroll").css("top", -595 * big_gal_pos);
		}
		function move_bg_small() {
			$("#bg_small_scroll").animate({top:110 * -(big_gal_current - 1)},200);
		}
		function place_bg_arrs() {
			if(big_gal_current === big_gal_length - 4) {
				$("#hide_3").hide();
				$("#hide_4").show();
			} else if(big_gal_current === 1) {
				$("#hide_3").show();
				$("#hide_4").hide();
			} else {
				$("#hide_3").hide();
				$("#hide_4").hide();
			}
		}
		place_bg_arrs();
	});

	// flash logo
	$("#flag").flash({
		swf: "/static/img/wind_220x170.swf",
		height: 170,
		width: 220,
		hasVersion: 8,
		wmode: "transparent",
		params: {
			menu: "false"
		}
	});
	if($.flash.hasVersion(8) === true) {
		$("#flag a").hide();
	} else {
		$("#flag object").hide();
		$("#flag embed").hide();
	}

});

(function($){
    $.fn.nice_slider = function(options) {
        var defaults = {
            delay: 5
        };
        var options = $.extend(defaults, options);

        var nice_id = "#" + $(this).attr("id");
        var nice_num = $(nice_id + " > a.nice_pic").length;
        var current_num = 0;
        var old_num = 0;
        var first_start = true;
        var timer = 0;
        if(nice_num > 1) {
            var exec_timer = setInterval(slide,1000);

            $("#view_pic").bind("mouseenter",function() {
                $("#vp_arrs").fadeIn(300);
            });
            $("#view_pic").bind("mouseleave",function() {
                $("#vp_arrs").fadeOut(300);
            });
        } else {
            $("a.nice_pic:eq(0)").css("top",0).fadeIn(1000);
            set_link();
        }

        function slide() {
            if(timer < options.delay) {
                timer++;
                if(timer === 1) {
                    clear_once();
                }
            } else {
                timer = 0;
                if(current_num < nice_num - 1) {
                    old_num = current_num;
                    current_num++;

                    set_link();
                    $("a.nice_pic:eq("+old_num+")").fadeOut(1000);
                    $("a.nice_pic:eq("+current_num+")").css("top",0).fadeIn(1000);
                } else {
                    old_num = current_num;
                    current_num = 0;

                    set_link();
                    $("a.nice_pic:eq("+old_num+")").fadeOut(1000);
                    $("a.nice_pic:eq("+current_num+")").css("top",0).fadeIn(1000);
                }
            }
        }

        function slide_act(num) {
            timer = 0;
            old_num = current_num;
            current_num = current_num + num;
            if(current_num < 0) {current_num = nice_num - 1};
            if(current_num >= nice_num) {current_num = 0};

            set_link();
            $("a.nice_pic:eq("+old_num+")").fadeOut(1000);
            $("a.nice_pic:eq("+current_num+")").css("top",0).fadeIn(1000);
        }

        function clear_once() {
            if(first_start === true) {
                $(nice_id).css("background","#FAF9F2");
                first_start = false;
                $("a.nice_pic:eq(0)").css("top",0).fadeIn(500);
                set_link();
            }
        }

        function set_link() {
            var url = $("a.nice_pic:eq("+current_num+")").attr("href");
            $("#mask_link").attr("href",url);
        }

        $("#nice_back").click(function(e) {
            e.preventDefault();
            slide_act(-1)
        });
        $("#nice_fwd").click(function(e) {
            e.preventDefault();
            slide_act(1)
        });
    }
})(jQuery);