$(document).ready(function() {
    var root = $("#root").attr("href");

    $('#container').tabs();

    $("#loginBox, #loginBox2").click(function() {
        $(".diskusion-top").toggle("slow");
    });

    $("#alert .close").click(function() {
        $("#alert").fadeOut();
    });

    $("#chmi1").click(function() {
        window.open(this.href, "chmi", "width=400,height=310,left=150,top=150,location=0,menubar=0,resizable=0,scrollbars=0,status=0,titlebar=0,toolbar=0");
        return false;
    });

    $("#chmi2").click(function() {
        window.open(this.href, "chmi", "width=175,height=225,left=150,top=150,location=0,menubar=0,resizable=0,scrollbars=0,status=0,titlebar=0,toolbar=0");
        return false;
    });

    $("#chmi3").click(function() {
        window.open(this.href, "chmi");
        return false;
    });

    $(".lefttop a").click(function() {
        var toggle1 = "#toggleTop"+this.rel;
        var toggle2 = "div.toggleTop"+this.rel;

        if ( $(toggle2).length ){
            if($(toggle1).is(".active")) {
                $(toggle1).removeClass('active');
                $(toggle2).hide("fast");
                $(".leftmenu ul li ul:visible").hide("fast");
            } else {
                $(".leftmenu:visible").not(":first").hide("fast");
                $(".lefttop.active").removeClass('active');
                $(".leftmenu ul li.active").removeClass("active");
                $(".leftmenu ul li ul:visible").hide("fast");
                $(".toggleTop"+this.rel+" ul li ul").show("fast");
                if ( $(toggle1).length  ){
                    if ( $(toggle2).is(':hidden') ){
                        $(toggle1).addClass('active');
                    }else{
                        $(toggle1).removeClass('active');
                    }
                }
                $(toggle2).toggle("slow");
            }
        }
    });

    $("#search").focus(function() {
        if(this.value=='Vyhledávání') {
            this.value = '';
        }
    });

    $(".togglew").click(function() {
        if(!$(this).hasClass("active")) {
            $(".togglew").removeClass("active");
            $(this).addClass("active");
            if(this.rel == "togglew1") {
                $("#togglew2").hide("fast");
                $("#togglew1").show("slow");
            } else {
                $("#togglew1").hide("fast");
                $("#togglew2").show("slow");            
            }
        }
    });

    $("#showFlash").click(function() {
        var width = parseInt((document.body.clientWidth - 600) / 2);

        $("body").prepend('<div id="flash"></div>');
        $("#flash").css("left", width);

   		var s1 = new SWFObject($("#flashUrl").val(), "line", "605", "423", "8");
		s1.addParam("wmode", "transparent");
        s1.addVariable("lang","cz");
    	s1.addVariable("url",$("#flashPath").val());
    	s1.addParam("base", $("#flashPath").val());
   		s1.write("flash");

        $("#flash").append('<a href="javascript:;" id="closeFlash">zavřít</a>');
        $("body").append('<div id="flashOverlay"></div>');

        $("#flashOverlay, #closeFlash").bind("click", function() {
            $("#flashOverlay").remove();
            $("#flash").remove();
        });

    });

    $("#showFinancing").click(function() {
        var width = parseInt((document.body.clientWidth - 600) / 2);

        $("body").prepend('<div id="financing" class="textpage"><h2>Financování</h2></div>');
        $("#financing").css("left", width);
        $("#financing").append($("#financingData").html());
        $("#financing").append('<a href="javascript:;" id="closeFinancing">zavřít</a>');
        $("body").append('<div id="financingOverlay"></div>');

        $("#financingOverlay, #closeFinancing").bind("click", function() {
            $("#financingOverlay").remove();
            $("#financing").remove();
        });

    });

    $(".youtube").click(function() {
        var width = parseInt((document.body.clientWidth - 420) / 2);
        var height = parseInt(document.documentElement.scrollTop + 50);
        $("body").prepend('<div id="youtube"><object width="425" height="344"><param name="movie" value="'+this.href+'&color1=0x234900&color2=0x4e9e00"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="'+this.href+'&color1=0x234900&color2=0x4e9e00" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object></div>');
        $("#youtube").css("left", width);
        $("#youtube").css("top", height);
        $("#youtube").append('<a href="javascript:;" id="closeYoutube">zavřít</a>');
        $("body").append('<div id="youtubeOverlay"></div>');

        $("#youtubeOverlay, #closeYoutube").bind("click", function() {
            $("#youtubeOverlay").remove();
            $("#youtube").remove();
        });

        return false;
    });

    $(".video").click(function() {
        var width = parseInt((document.body.clientWidth - 420) / 2);
        var height = parseInt(document.documentElement.scrollTop + 50);
        $("body").prepend('<div id="youtube"></div>');
        $("#youtube").css("left", width);
        $("#youtube").css("top", height);
        $("#youtube").append('<a href="javascript:;" id="closeYoutube">zavřít</a>');
        $("body").append('<div id="youtubeOverlay"></div>');

   		var video = new SWFObject(root+"swf/prehravac/player.swf?videoUrl="+this.href, "line", "425", "344", "8");
		video.addParam("wmode", "transparent"); 
   		video.write("youtube");

        $("#youtubeOverlay, #closeYoutube").bind("click", function() {
            $("#youtubeOverlay").remove();
            $("#youtube").remove();
        });

        return false;
    });

    $("#sendfr").click(function() {
        $("#sendfriend").toggle();
    });

});

function toggle(id){ //prepinac zobrazeni leftmenu
    var toggle1 = "#toggle"+id;
    var toggle2 = "ul.toggle"+id;
    var tog = id.substr(0, 1);

    if($(toggle2).length) {
        if($(toggle1).length) {
            if($(toggle2).is(':hidden')) {
                $("#leftcolumn>div.active").removeClass("active");
                $(".leftmenu:visible").not(".toggleTop1, .toggleTop"+tog).hide("fast");
                $(".toggleTop"+tog+" ul li ul").not(".toggle"+id).hide("fast");
                $(".leftmenu ul li.active").removeClass('active');
                $(toggle1).addClass('active');
            } else {
                $(toggle1).removeClass('active');
            }
        }
        $(toggle2).toggle("slow");
    }
}

