/* ShowIt Objects */
var ShowIt = new Object();
ShowIt.isGrouped = false;
ShowIt.library = new Object();
ShowIt.lastElementPosition = -1;
ShowIt.lastElementRel = "ShowIt";
ShowIt.diashowStarted = false;
ShowIt.diashowTimer;
ShowIt.showButtonCaption = new Array("<img src=\"/page/system/src/Play-icon.png\" width=\"10\" alt=\"Play\" border=\"0\" /> Diashow starten", "<img src=\"/page/system/src/Pause-icon.png\" width=\"10\" alt=\"Pause\" border=\"0\" /> Diashow anhalten");
ShowIt.currentContent = null;

ShowIt.init = function(){

   check4IE6();

    // check, whether popups dom-elements exists
    if (! $("#popup").length > 0){
        // create popups dom-elements
        var html = "<div id=\"popup\"><div id=\"popupHead\"><div id=\"popupTitle\">...</div><div id=\"popupClose\">x</div></div><div id=\"popupContent\">... loading ...</div></div><div id=\"popupBackground\"></div>";
        $('body').prepend(html);
    }

    // Bilder vorladen:
    var img1 = new Image();
    img1.src = "/page/system/src/Play-icon.png";
    var img2 = new Image();
    img2.src = "/page/system/src/Pause-icon.png";

    // Durchlaufe alle Links mit rel=ShowIt(.)*
    $("a[rel^=ShowIt]").each(function() {
	var rel = $(this).attr("rel");
	var title = $(this).attr("title");
	var href = $(this).attr("href");
	var target = $(this).attr("target");

	if(rel != "ShowIt"){
		// Link ist Teil von Gruppe
	        if(ShowIt.library[rel]){
	          ShowIt.library[rel].push(new Array(href, title, target));
	        }else{
	          ShowIt.library[rel] = new Array(new Array(href, title, target));
	        }
	}
     });

     // Setze click-Event auf diese Links
     $("a[rel^=ShowIt]").click(function(event){
        loadPopup();
        event.preventDefault();

        var url = $(this).attr("href");
        var title = $(this).attr("title");
        var rel = $(this).attr("rel");
        var target = $(this).attr("target");

        // Suche in Library nach aktuellem Element
        ShowIt.isGrouped = false;
        var elementId;
        var n = 0;
        try{ n = ShowIt.library[rel].length; }catch(e){}
        for(var i = 0; i < n; i++){
            if(ShowIt.library[rel][i][0] == url){
                ShowIt.lastElementPosition = i;
                ShowIt.lastElementRel = rel;
                ShowIt.isGrouped = true;
                break;
            }
        }

        ShowIt.diashowStarted = false;
        ShowIt.showElement(url, title, target);
      });
}
ShowIt.toggleShow = function(){
	if(ShowIt.diashowStarted)
		ShowIt.stopShow();
	else
		ShowIt.startShow();
}
ShowIt.startShow = function(){
	// Wenn diashow nicht schon gestartet und nicht das letzte Element gerade aktiv ist
	if(!ShowIt.diashowStarted && ShowIt.lastElementPosition != ShowIt.library[ShowIt.lastElementRel].length-1){
		// Starten
		ShowIt.diashowTimer = setTimeout("ShowIt.stepShow();", 1000);
		ShowIt.diashowStarted = true;
		$("#diashowButton").html(ShowIt.showButtonCaption[1]);
	}
}
ShowIt.stopShow = function(){
	if(ShowIt.diashowStarted){
		// Stoppen
		clearTimeout(ShowIt.diashowTimer);
		ShowIt.diashowStarted = false;
		$("#diashowButton").html(ShowIt.showButtonCaption[0]);
	}
}
ShowIt.stepShow = function(){
	// Diashow bei letztem Element stoppen
	if(ShowIt.lastElementPosition == ShowIt.library[ShowIt.lastElementRel].length-2){
		ShowIt.stopShow();
		/*}else{
		ShowIt.diashowTimer = setTimeout("ShowIt.stepShow();", 5500);*/
	}

	ShowIt.next();
}
ShowIt.prev = function(){
	var i = ShowIt.lastElementPosition - 1;
	var rel = ShowIt.lastElementRel;
	if(i < 0) return;

	var url = ShowIt.library[rel][i][0];
	var title = ShowIt.library[rel][i][1];
	var target = ShowIt.library[rel][i][2];

	ShowIt.lastElementPosition = i;
	ShowIt.showElement(url, title, target);

	if(ShowIt.diashowStarted){
		// Ist eine Diashow gestartet und Benutzer klickt weiter, timeout für nächsten wechsel resetten
		clearTimeout(ShowIt.diashowTimer);
		ShowIt.diashowTimer = setTimeout("ShowIt.stepShow();", 5500);
	}
}
ShowIt.next = function(){
	var i = ShowIt.lastElementPosition + 1;
	var rel = ShowIt.lastElementRel;
	if(i >= ShowIt.library[rel].length){
		return;
	}

	var url = ShowIt.library[rel][i][0];
	var title = ShowIt.library[rel][i][1];
	var target = ShowIt.library[rel][i][2];

	ShowIt.lastElementPosition = i;
	ShowIt.showElement(url, title, target);

	if(ShowIt.diashowStarted){
		// Ist eine Diashow gestartet und Benutzer klickt weiter, timeout für nächsten wechsel resetten
		clearTimeout(ShowIt.diashowTimer);
		ShowIt.diashowTimer = setTimeout("ShowIt.stepShow();", 5500);
	}


	// Das nächste Bild vorladen, sofern es eins gibt :-)
	var j = i + 1;
	if(j < ShowIt.library[ShowIt.lastElementRel].length-1){
		var tmp = new Image();
		tmp.src = ShowIt.library[rel][j][0];
	}

}
ShowIt.showElement = function(url, title, target){

    var currentContent = new Object();

	// Bestimme den Typ des Elements
		var fileType = url.split(".");
		fileType = fileType[ fileType.length - 1 ]; 

		fileType = fileType.toLowerCase();

		if(!fileType || fileType == "" || !(fileType == "jpg" || fileType == "gif" || fileType == "png" || fileType == "bmp" || fileType == "html" || fileType == "swf" || fileType == "mp3")){
			var tmp = title.split(".");
			fileType = tmp[ tmp.length - 1 ];

			title = "";
			for(var i = 0; i < (tmp.length - 1); i++){
				title += tmp[i];
			}
			fileType = fileType.toLowerCase();
		}


	// Bestimme anzuzeigende Größe des Elements
		var show_size = target.split(",");

	$("#popupTitle").text(title);

	var html = "";
	navigationBar = "";


	// Setze GruppierNavigation
		if(ShowIt.isGrouped){
			navigationBar = "<div align=\"center\" id=\"ShowIt2NavigationBar\">";
			navigationBar += "<span style=\"padding: 2px;\">(" + (ShowIt.lastElementPosition+1) + "/" + ShowIt.library[ShowIt.lastElementRel].length + ")</span>&nbsp;&nbsp;";
			navigationBar += "<a href=\"javascript:ShowIt.prev();\" style=\"text-decoration:none; color: white; background-color: #6F6F6F; padding: 2px;\" title=\"Bild zur&uuml;ck (Pfeil links)\">&lt; zur&uuml;ck </a>&nbsp;";
			navigationBar += "<a href=\"javascript:ShowIt.next();\" style=\"font-weight: bold; text-decoration:none; color: white; background-color: #6F6F6F; padding: 2px;\" title=\"Bild weiter (Pfeil rechts)\"> weiter &gt;</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
				var diashowButtonText = ShowIt.showButtonCaption[0];
				if(ShowIt.diashowStarted) diashowButtonText = ShowIt.showButtonCaption[1];
			navigationBar += "<a href=\"javascript:ShowIt.toggleShow();\" style=\"text-decoration:none; color: white; background-color: #6F6F6F; padding: 2px;\" id=\"diashowButton\">" + diashowButtonText + "</a>";
			navigationBar += "</div>";
		}

	// Zeige Element an
	if($("#ShowIt2Image").length > 0){
		// Wenn ein Bild geladen ist, nur das src-Attribut ändern statt popup neu zu laden

		$("#ShowIt2Image").fadeOut("slow", function(){
            $("#ShowIt2Image").attr("src", url);
            
            var img = new Image();
            $(img).load(function(){

                // Pflanze aktuelle NavigationBar ein, entferne dafür erst die alte
                $("#ShowIt2NavigationBar").remove();
                $("#popupContent").append(navigationBar);

                $("#ShowIt2Image").fadeIn("medium", function(){

                    // Größe des Bildes zurücksetzen und abspeichern
                    $("#ShowIt2Image").height("auto");
                    $("#ShowIt2Image").width("auto");
                    currentContent.width = $("#ShowIt2Image").width();
                    currentContent.height = $("#ShowIt2Image").height();

                    // Popup ausrichten
                    centerPopup();
                    
                });

            }).attr('src', url);

		});
        
    }else{
		// Binde anhand des Element-Typs das Element ein

		// Popup mit neuem HTML-Code initialisieren
		if(fileType == "jpg" || fileType == "gif" || fileType == "png" || fileType == "bmp"){
			html = "<div style=\"text-align:center\"><img src=\"" + url + "\" alt=\"" + title + "\" id=\"ShowIt2Image\" /></div><br/><div align=\"center\">" + title + "</div>";

            var img = new Image();
            $(img).load(function(){

                currentContent.width = $("#ShowIt2Image").width();
                currentContent.height = $("#ShowIt2Image").height();

                centerPopup();
            }).attr('src', url);


		}else if(fileType == "html"){

			html = "<iframe src='"+url+"' height=" + show_size[1] + " width=" + show_size[0] + " id='ShowIt_iframe' name='ShowIt_iframe' border='0' frameborder='0'></iframe>";

            currentContent.width = show_size[0];
            currentContent.height = show_size[1];

		}else if(fileType == "swf"){
			html = "<object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0\"  height=" + show_size[1] + " width=" + show_size[0] + " align=\"middle\">";
			html += "<param name=\"allowScriptAccess\" value=\"sameDomain\" />";
			html += "<param name=\"movie\" value=\"" + url + "\" />";
			html += "<param name=\"menu\" value=\"false\" />";
			html += "<param name=\"quality\" value=\"high\" />";
			html += "<param name=\"bgcolor\" value=\"#FFFFFF\" />";
			html += "<embed src=\"" + url + "\"  height=" + show_size[1] + " width=" + show_size[0] + " menu=\"false\" quality=\"high\" wmode=\"opaque\" bgcolor=\"#FFFFFF\" align=\"middle\" allowScriptAccess=\"sameDomain\" type=\"application/x-shockwave-flash\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" />";
			html += "</object>";

            currentContent.width = show_size[0];
            currentContent.height = show_size[1];

		}else if(fileType == "mp3"){
			html = "<object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0\"  height=30 width=300 align=\"middle\">";
			html += "<param name=\"allowScriptAccess\" value=\"sameDomain\" />";
			html += "<param name=\"movie\" value=\"/page/system/src/mp3player2.swf\" />";
			html += "<param name=\"menu\" value=\"false\" />";
			html += "<param name=\"quality\" value=\"high\" />";
			html += "<param name=\"bgcolor\" value=\"#FFFFFF\" />";

			html += "<param name=\"FlashVars\" value=\"mp3=" + url + "&amp;width=300&amp;height=30&amp;showvolume=1&amp;showloading=always&amp;buttonwidth=30&amp;volumeheight=8&amp;autoload=1\" />";

			html += "<embed src=\"/page/system/src/mp3player2.swf\"  height=30 width=300 menu=\"false\" quality=\"high\" wmode=\"opaque\" bgcolor=\"#FFFFFF\" align=\"middle\" allowScriptAccess=\"sameDomain\" type=\"application/x-shockwave-flash\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\"  FlashVars=\"mp3=" + url + "&amp;width=300&amp;height=30&amp;showvolume=1&amp;showloading=always&amp;buttonwidth=30&amp;volumeheight=8&amp;autoload=1\"/>";
			html += "</object>";

		}
		html += "<br/>" + navigationBar;
		$("#popupContent").html(html);

	} //$("#ShowIt2Image").length > 0

	//setTimeout("centerPopup();",90);
	setTimeout("centerPopup();",150);

    ShowIt.currentContent = currentContent;

}

/* globale Variablen */
var popupStatus = 0;  // 0 = disabled

function loadPopup(){
    //loads popup only if it is disabled
    if(popupStatus==0){
        $("#popupBackground").show();
        $("#popup").show();
        popupStatus = 1;
    }
}

function disablePopup(){
    //disables popup only if it is enabled
    if(popupStatus==1){
        $("#popupBackground").hide();
        $("#popup").hide();
        popupStatus = 0;

	$("#popupContent").html("");
	ShowIt.stopShow();
    }
}

function centerPopup(){
    // Set popup-width
    /*var contentWidth = $("#popupContent>*").width()+40;
    if(contentWidth > 40)
        $("#popup").width(contentWidth);*/


    //Get the window height and width  
    var winH = $(window).height();  
    var winW = $(window).width();  

    // Wenn Bild angezeigt wird, prüfen ob das im Browser Platz hat
    if($("#ShowIt2Image").length > 0){
        var popupH = $("#popup").height();
        var popupW = $("#popup").width();
        var newSize;

        // Schrumpfe Bild an Breite
        var deltaW = popupW - winW + 10;
        newSize = $("#ShowIt2Image").width() - deltaW;
        if(newSize <= ShowIt.currentContent.width)
            $("#ShowIt2Image").width(newSize);

        // Schrumpfe Bild an Hoehe
        var deltaH = popupH - winH + 10;
        newSize = $("#ShowIt2Image").height() - deltaH;
        if(newSize <= ShowIt.currentContent.height)
            $("#ShowIt2Image").height(newSize);
    }

    //Set the popup window to center
    //$("#popup").css("top",  winH/2-$("#popup").height()/2);
    //$("#popup").css("left", winW/2-$("#popup").width()/20);
    $("#popupBackground").css("height", winH);

    $("#popup").animate({
	top: winH/2-$("#popup").height()/2,
	left: winW/2-$("#popup").width()/2
	}, 100);

}

$(document).ready(function(){

    ShowIt.init();

    centerPopup();

    $("#popup").draggable({ scroll: false, handle: '#popupHead'});

    //Click the x event!
    $("#popupClose").click(function(){
        disablePopup();
    });
    // Click on Background
    $("#popupBackground").click(function(){
        disablePopup();
    });
    //Press Escape event!
    $(document).keypress(function(e){
        if(e.keyCode==27 && popupStatus==1){
            disablePopup();
        }
    });

    //Press left button
    $(document).keypress(function(e){
        if(e.keyCode==37 && popupStatus==1){
            ShowIt.prev();
        }
    });
    //Press right button
    $(document).keypress(function(e){
        if(e.keyCode==39 && popupStatus==1){
            ShowIt.next();
        }
    });

    // Behandle window resize -event und setze popup wieder in die mitte
    $(window).resize(function() {
        if(popupStatus==1){
            centerPopup();
        }
    });


});

function check4IE6(){
	if ($.browser.msie && $.browser.version=="6.0") {
		if (!get_cookie("CheckedBrowser")){
			alert("ACHTUNG! Sie verwenden einen unsicheren und veralteten Browser (Internet Explorer Version 6).\nEs kann auf den folgenden Seiten zu Anzeigefehlern kommen.\n\nBITTE AKTUALISIEREN SIE IM EIGENEN INTERESSE IHREN BROWSER, bestenfalls auf die aktuellste Version des kostenfreien Mozilla Firefox (http://www.mozilla.com/de/).");

			var current_date = new Date;
			var cookie_year = current_date.getFullYear ( );
			var cookie_month = current_date.getMonth ( );
			var cookie_day = current_date.getDate ( ) + 1;
			set_cookie ( "CheckedBrowser", "true", cookie_year, cookie_month, cookie_day );
		}
	}
}

function get_cookie ( cookie_name )
{
  var results = document.cookie.match ( '(^|;) ?' + cookie_name + '=([^;]*)(;|$)' );

  if ( results )
    return ( unescape ( results[2] ) );
  else
    return null;
}

function set_cookie ( name, value, exp_y, exp_m, exp_d, path, domain, secure )
{
  var cookie_string = name + "=" + escape ( value );

  if ( exp_y )
  {
    var expires = new Date ( exp_y, exp_m, exp_d );
    cookie_string += "; expires=" + expires.toGMTString();
  }

  if ( path )
        cookie_string += "; path=" + escape ( path );

  if ( domain )
        cookie_string += "; domain=" + escape ( domain );
  
  if ( secure )
        cookie_string += "; secure";
  
  document.cookie = cookie_string;
}

