/* popup text block
 * widget by IonDen
 * v 1.2
 * 15.06.2010
 * depends on jQuery 1.4
 */

$(document).ready(function(){
    var master_height = $("body > div").height();
    var master_text = "";
    var master_pop = "";
    var slave_height = 0;

    $("a.popup_text").click(function(event){
        event.preventDefault();
        master_text = $(this).attr("rel");

        master_pop = '<div id="big_grey"></div><div id="big_center"><div id="big_text"><div id="big_text_cont">';
        master_pop += master_text;
        master_pop += '</div><a href="#" id="big_close"></a></div></div>';

        $("body").append(master_pop);
        $("#big_grey").height(master_height);
        slave_height = $("#big_text").innerHeight();
		if(navigator.userAgent.indexOf("MSIE 6.0") == -1) {
	        $("#big_text").css("top",-(Math.round(slave_height / 2)));
		} else {
			$("#big_text").css("top",-(Math.round(slave_height / 2)) + $(window).scrollTop());
			
			$(window).scroll(function(event){
				$("#big_text").css("top",-(Math.round(slave_height / 2)) + $(window).scrollTop());
			});
		}

        $("select").css("visibility","hidden");
        $("object").hide();

        $("#big_close").click(function(event){
            event.preventDefault();
            master_pop = "";
            master_text = "";
            $("#big_grey").remove();
            $("#big_center").remove();

            $("select").css("visibility","visible");
            $("object").show();
        });
    });

	var master_height = $("body > div").innerHeight();
	var master_video = "";
	var mv_pop = "";
	var scr_height = 0;
	var scr_top = 0;
	var tv_width = 0;
	var tv_height = 0;

	$("a.show_video").click(function(event) {
		event.preventDefault();
		master_video = $(this).attr("rel");
		tv_width = master_video.slice(master_video.indexOf('width'),master_video.indexOf('height'));
		tv_height = master_video.slice(master_video.indexOf('height'),master_video.indexOf('>'));

		tv_width = parseInt(tv_width.slice(7));
		tv_height = parseInt(tv_height.slice(8));
		mv_pop = '<div id="big_grey"></div>';
		mv_pop += '<div id="big_video"><span><a href="#" id="bv_close">закрыть</a></span><div>';
		mv_pop += master_video;
		mv_pop += '</div></div>';
	
		$("body").append(mv_pop);
		$("#big_video > span").width(tv_width - 18).css("left", -(tv_width/2)).css("top", -(tv_height/2) - 64);
		$("#big_video > div").width(tv_width).height(tv_height).css("left", -(tv_width/2)).css("top", -(tv_height/2) - 40);

		if(navigator.userAgent.indexOf("MSIE 6.0") !== -1) {
			scr_height = parseInt($(window).height() / 2);
			scr_top = parseInt($(window).scrollTop());
			var z = scr_top + scr_height;
			$("#big_video").css("top", z);
		}
		
		if(navigator.userAgent.indexOf("MSIE") !== -1) {
			$("select").css("visibility","hidden");
		}
		
		$("#big_grey").height(master_height);
		$("#bv_close").click(function(event) {
			event.preventDefault();
			$("#big_grey").remove();
			$("#big_video").remove();
			master_video = "";
			if(navigator.userAgent.indexOf("MSIE") !== -1) {
				$("select").css("visibility","visible");
			}
		});
	});

    if($("#main_pic").width() !== null) {
        var mp_num = $("#mp_big > a").length;
        var mp_current = 0;

        $("#mp_big > a").removeAttr("id");
        $("#mp_big > a:eq(" + mp_current + ")").attr("id", "sh");

        $("#mp_small > a").mouseenter(function() {
            $("#mp_small > a").removeAttr("id");
            $(this).attr("id", "mp_on");

            for(var i = 0; i < mp_num; i++) {
                if($("#mp_small > a:eq(" + i + ")").attr("id") == "mp_on") {
                    mp_current = i;
                }
            }

            $("#mp_big > a").removeAttr("id");
            $("#mp_big > a:eq(" + mp_current + ")").attr("id", "sh");
        });
    }
});