var ona = false;
var zIndex = 90;
var submarineLeft = false;
var submarineDrag = false;


function closePeri() {
	$("#contact_periview").css("display", "none");
}

function openportfolio(){   
    $("#treasure_links a").each(function() {
        $(this).bind('mouseover', function() {
            $("#treasure_links").css("display", "block");
            $("#treasureBox").removeClass("treasure").addClass("treasure_open");
            ona = true;
        });
        $(this).bind('mouseout', function() {
            ona = false;
        });
    });
    
    $("#treasure_links").bind('mouseover', function() {
        $("#treasure_links").css("display", "block");
        $("#treasureBox").removeClass("treasure").addClass("treasure_open");
        ona = true;
    });

    $("#treasure_links").bind('mouseout', function() {
        ona = false;
    });
    
    if(ona == false)
    {
    	$("#treasure_links").css("display", "block");
    	$("#treasureBox").removeClass("treasure").addClass("treasure_open");
    }
    
    // IE6 Fix
    if($.browser.msie && $.browser.version == "6.0")
    {
        if($("#treasureBox").attr("class") == "treasure_open") {
            $("#treasureBox").css("cssText", "background: url('/images/ie6/wesensit_schatkist_ie6_hover.png');");
        } else if($("#treasureBox").attr("class")=="treasure") {
            $("#treasureBox").css("cssText", "background: url('/images/wesensit_schatkist1.png');");
        }
        
        $("#treasureBox").wifixpng().wiunfixpng().wifixpng();
    }

}

function closeportfolio() {
    
    if(ona == false)
    {
    	$("#treasureBox").removeClass("treasure_open").addClass("treasure");	
    	$("#treasure_links").css("display", "none");
        
        // IE6 Fix
        if($.browser.msie && $.browser.version == "6.0")
        {
            if($("#treasure_links").css("display") == "none") {
                $("#treasureBox").css("cssText", "background: url('/images/wesensit_schatkist1.png');");
            }
            
            $("#treasureBox").wifixpng().wiunfixpng().wifixpng();
        }
    }
}

function subpapier(obj){   
	var pagename = $(obj).attr("name");
	var image_id = $(obj).attr("id");
	var working_id = 'sub_site_paper_' + Number(new Date());

    // We gaan de nodige html maken       
    var workhtml = '<div id="' + working_id + '" class="sub_site_paper" style="display: none; z-index: ' + zIndex + ';">';
        workhtml += '<div class="sub_site_paper_header"><a href="#" onclick="sluitsubpapier(\'' + working_id + '\');return false;"><img src="/images/wesensit_duikboot_vectorobject.png"  align="right"/></a></div>'; 
        workhtml += '<div class="sub_background_spacer">&nbsp;</div>';
        workhtml += '<div class="sub_site_paper_footer"></div>';
        workhtml += '<div class="sub_site_paper_text">&nbsp;</div>';
    workhtml += '</div>';
    
    // We gaan de zindex 1 ophogen
    zIndex += 1;
       
    // We gaan de container maken
    $("#site_container").after(workhtml);
    
	if(pagename != "contactperson")
	{
    	$.ajax({
    		url: "/includes/main/main_ajax.php?page_name=" + pagename,
    		type: "GET",
    		cache: false,
    		async: true,
    		success: function(data) {               
    			//$("#site_paper_footer").css("margin-top", "0");
    			data = unescape(data);
    			$("#" + working_id + " .sub_site_paper_text").html(data);
    			//newhtml = document.getElementById("sub_site_paper_text").innerHTML.replace("[script]", '<script type="text/javascript">');
    			//newhtml = newhtml.replace("[/scr]", "</scr");
    			//newhtml = newhtml.replace("[ipt]", "ipt>");
    						//alert(newhtml);
    			//document.getElementById("sub_site_paper_text").innerHTML = newhtml;
    
    			setTimeout('setsubPaper(\'' + working_id + '\')', 100);
    		}
    	});
	} else{
    	$.ajax({
    		url: "/includes/main/main_ajax.php?page_name=" + pagename + "&image_id=" + image_id,
    		type: "GET",
    		cache: false,
    		async: true,
    		success: function(data) {
    			//$("#site_paper_footer").css("margin-top", "0");
    			data = unescape(data);
    			$("#" + working_id + " .sub_site_paper_text").html(data);
    			//newhtml = document.getElementById("sub_site_paper_text").innerHTML.replace("[script]", '<script type="text/javascript">');
    			//newhtml = newhtml.replace("[/scr]", "</scr");
    			//newhtml = newhtml.replace("[ipt]", "ipt>");
    						//alert(newhtml);
    			//document.getElementById("sub_site_paper_text").innerHTML = newhtml;
    
    			setTimeout('setsubPaper(\'' + working_id + '\')', 100);
    			}
    	
    	});	
	}
}

function getcontact(contactname){
	var working_id = 'sub_site_paper_' + Number(new Date());
    var pagename = 'contactperson';

    // We gaan de nodige html maken       
    var workhtml = '<div id="' + working_id + '" class="sub_site_paper" style="display: none; z-index: ' + zIndex + ';">';
        workhtml += '<div class="sub_site_paper_header"><a href="#" onclick="sluitsubpapier(\'' + working_id + '\');return false;"><img src="/images/wesensit_duikboot_vectorobject.png"  align="right"/></a></div>'; 
        workhtml += '<div class="sub_background_spacer">&nbsp;</div>';
        workhtml += '<div class="sub_site_paper_footer"></div>';
        workhtml += '<div class="sub_site_paper_text">&nbsp;</div>';
    workhtml += '</div>';
    
    // We gaan de zindex 1 ophogen
    zIndex += 1;
       
    // We gaan de container maken
    $("#site_container").after(workhtml);

	$.ajax({
		url: "/includes/main/main_ajax.php?page_name=" + pagename + "&image_name=" + contactname,
		type: "GET",
		cache: false,
		async: true,
		success: function(data) {
			//$("#site_paper_footer").css("margin-top", "0");
			data = unescape(data);
			$("#" + working_id + " .sub_site_paper_text").html(data);
			//newhtml = document.getElementById("sub_site_paper_text").innerHTML.replace("[script]", '<script type="text/javascript">');
			//newhtml = newhtml.replace("[/scr]", "</scr");
			//newhtml = newhtml.replace("[ipt]", "ipt>");
						//alert(newhtml);
			//document.getElementById("sub_site_paper_text").innerHTML = newhtml;

			setTimeout('setsubPaper(\'' + working_id + '\')', 100);
			}
	
	});	
}


function setsubPaper(working_id)
{
    // We gaan het papiertje zichtbaar maken
	$("#" + working_id).css("display", "block");
    
    // We gaan de gegevens ophalen
	var height = $("#" + working_id + " .sub_site_paper_text").height();
	var hdr_height = $("#" + working_id + " .sub_site_paper_header").height();
	var ftr_height = $("#" + working_id + " .sub_site_paper_footer").height();
	var spacer_fix = (hdr_height+ftr_height)/2;

    // Attributes
    var max = 0;
    var maxId = "";
    var min = 10000;
    var minId = "";
    var subPaperCounter = 0;
    
    // We gaan kijken of er al niet formuliertjes open zijn
    $(".sub_site_paper").each(function() {
        // We gaan de hoogste en laagste posities zijn
        var offsetS = $(this).offset();
        if(offsetS.top > max)
        {
            max = offsetS.top;
            maxId = $(this).attr("id");
        }
        
        if(min > offsetS.top)
        {
            min = offsetS.top;
            minId = $(this).attr("id");
        }
        
        subPaperCounter++;
    });
        
    // We gaan nu kijken waar we het beste met het nieuwe blaatjes heen kunnen
    if(subPaperCounter > 1)
    {        
        if(min > 100 && min < 200)
        {
            $("#" + working_id).css("top", (min + 25) + "px");
            $("#" + working_id).css("left", ($("#" + minId).offset().left + 25) + "px");
        } else if(min > 199 && max < 201 && (max - 25) > 99) {
            $("#" + working_id).css("top", (max - 25) + "px");
            $("#" + working_id).css("left", ($("#" + maxId).offset().left - 25) + "px");
        } else {
            $("#" + working_id).css("top", (max + 25) + "px");
            if(($("#" + minId).offset().left + (25 + $("#" + working_id).width())) > $("body").width()) {
                $("#" + working_id).css("left", -25  + "px"); //($("#" + maxId).offset().left - 25)
            } else {
                $("#" + working_id).css("left", 25 + "px");
            }
        }
    }
    
    // We gaan de spacer hoogte ophalen
	var new_height = (height-spacer_fix);
	if(new_height < 0) new_height=0;
	$("#" + working_id + " .sub_background_spacer").css("height", new_height);    
    
    // We gaan het papiertje sleepbaar maken
    $("#" + working_id).draggable(); //{ containment: 'window'}
    $("#" + working_id).bind('click', function() {
        if($("#" + working_id).css("z-index") < zIndex)
         $("#" + working_id).css("z-index", zIndex);
         zIndex++;
    });
    
    // We gaan de lightbox instellen
	//$("*[class^=gallery]").lightBox();
    initLightBox("*[class^=gallery]");
}

function sluitsubpapier(working_id)
{
	$("#" + working_id).remove();
}

function papier(obj, name)
{
    obj = obj || false;
    name = name || "";
    
    if(obj)
    {
	   var pagename = $(obj).attr("name");
    } else {
        var pagename = name;
    }
    
    if(pagename=="submarine") {
        showSubmarine();
        return false;
    }
    
	$.ajax({
		url: "/includes/main/main_ajax.php?page_name=" + pagename,
		type: "GET",
		cache: false,
		async: true,
		success: function(data) {
			//$("#site_paper_footer").css("margin-top", "0");
			       //     alert(data);
			data = unescape(data);
			data = data.replace("[space]", "%20");
			//$("#site_paper_text").html(data);
            
			document.getElementById("site_paper_text").innerHTML = data;
            
            // We gaan de lightbox actieveren
            $(".nieuwspicture").each(function() {
                $(this).find("a").each(function() {
                    if($(this).attr("class").substr(0, 13) == "lightboxnews_")
                    {
                        // We gaan de lightbox actieveren
                        $("." + $(this).attr("class")).lightBox();
                    }
                });
            });
            
            // Google pagina oplossing
            if($("#google_perifilm"))
            {
                $("#google_perifilm").html('<object height="166" width="166" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0"><param value="/images/flash/google_periscope_view.swf" name="movie"/> <param value="high" name="quality"/> <param value="true" name="loop"/> <param value="noborder" name="scale"/> <param value="transparent" name="wmode"/> <param value="always" name="AllowScriptAccess"/> <embed height="166" width="166" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" allowscriptaccess="always" wmode="transparent" scale="noborder" loop="true" quality="high" src="/images/flash/google_periscope_view.swf"/></object>');
            }

			setTimeout('setPaper()', 100);
	}
	})
	
}

function setPaper()
{
	$("#site_paper").css("display", "block");
	var height = $("#site_paper_text").height();
	var hdr_height = $("#site_paper_header").height();
	var ftr_height = $("#site_paper_footer").height();
	var spacer_fix = (hdr_height+ftr_height)/2;
	var sitebodyheight = height+80+"px";

	var new_height = (height-spacer_fix);
	if(new_height < 0) new_height=0;
	$("#background_spacer").css("height", new_height);
	$("#site_body").css("height", sitebodyheight)
	//debug();
	//$("*[class^=gallery]").lightBox();
    initLightBox("*[class^=gallery]");
    //$("#site_paper").draggable({ containment: 'body'});
    $("#contact_periview").draggable();
    
    if($.browser.version=='6.0' && $.browser.msie)
    {
        $("#site_paper_header,#site_paper_footer,#site_paper_text,#google_periscoop,.picturesearch a").wifixpng().wiunfixpng().wifixpng();
    }
}

Array.prototype.contains = function (element) {
    
    for (var i = 0; i < this.length; i++) {
        if (this[i] == element) {
            return true;
        }
    }
    
    return false;
}

// Functie om JQuery lightbox verschillende collecties
// te laten aanmaken afhankelijk van klassenaam.
function initLightBox(matchClasses) {
    
    // Wat arrays om informatie in op te slaan
    var uniqueClasses = new Array();
    var matchedItems  = $(matchClasses);
    
    // Loop door alle matches
    for(var i=0;i < matchedItems.length;i++) {
        
        // Kijk of de huidige klassenaam al in de array zit
        if(!uniqueClasses.contains(matchedItems[i].className)) {
            // Klasse zit nog niet in de array, dus we zetten hem er nu in
            uniqueClasses[uniqueClasses.length] = matchedItems[i].className;
            
            // Maak lightbox object aan voor deze klasse.
            $("." + matchedItems[i].className).lightBox();
        } 
        
    }
    
}

function sluitpapier()
{
	$("#site_paper").css("display", "none");
}

function debug() {
	
	var site_paper = $("#site_paper").height();
	var site_paper_header = $("#site_paper_header").height();
	var site_paper_footer = $("#site_paper_footer").height();
	var site_paper_text = $("#site_paper_text").height();
	var background_spacer = $("#background_spacer").height();
	
	var data  = "site_paper: "+site_paper+"<br />";
	data += "site_paper_header: "+site_paper_header+"<br />";
	data += "background_spacer: "+background_spacer+"<br />";
	data += "site_paper_footer: "+site_paper_footer+"<br />";
	data += "site_paper height: "+$("#site_paper").height()+"<br />";
	data += "site_paper_text: "+site_paper_text+"<br />";

	$("#debugblok").html(data);
}

function info(obj){
	
	var image_id = $(obj).attr("id");

	$.ajax({
		url: "/includes/main/main_ajax.php?image_id=" + image_id,
		type: "GET",
		cache: false,
		async: true,
		success: function(data) {
  			$("#contact_midden").html(unescape(data));
  			$("a.foto").removeClass("selected");
  			$("a.foto[id='"+image_id+"']").addClass("selected");
		}
	});
}

 $(document).ready(function() {
    var lastOffset = 0;
    
    document.title = "weSENSit | Een duikboot in goor";
    
   $("#sub_site_paper").css("display", "none")
   $("#site_body").css("height", "685px")
          
   //$("#treasure_links").css("left", $("#treasureBox").offset().left + "px");
   if($.browser.msie && ($.browser.version == "7.0" || $.browser.version == "6.0")) {
    $("#treasure_links").css("cssText", "margin-left: -186px !important;");
    $("#submarine").css("top", "0px");
    
    // We gaan kijken of het IE 6 is
    if($.browser.version == "6.0") {
        $("#treasure_container").css("cssText", "margin: -503px 0 0 370px;");
        $("img[src$=.png], #treasureBox, #submarine, #site_header, #site_footer, #site_navigation_bottom, .picturesearch a,#site_paper_header,#site_paper_footer,#site_paper_text,#google_periscoop").wifixpng().wiunfixpng().wifixpng();
    }
   } else {
    $("#submarine").css("top", "180px");
   }
   
   // Duikboot juist plaatsen
   $("#submarine").css("left", ($("#background_container").width() - 500) + "px");
   
   $("#treasureBox").bind('mouseover', function() {
    openportfolio();
   });
   $("#treasure_container").bind('mouseout', function() {
    closeportfolio();
   });
   
   $("#submarine").draggable({
    drag: function(event, ui) {
        var currentOffset = $("#submarine").offset();
        
        if((lastOffset + 3) < currentOffset.left)
        {            
            $("#submarine").css("background-image", "url('/images/wesensit_duikboot_right.png')");            
            $("#submarine_bulleyes_left").attr("id", "submarine_bulleyes_right");
            $("#submarine_baloon_left").attr("id", "submarine_baloon_right");
            
            submarineLeft = false;
        } else if((lastOffset - 3) > currentOffset.left) {            
            $("#submarine").css("background-image", "url('/images/wesensit_duikboot_left.png')");            
            $("#submarine_bulleyes_right").attr("id", "submarine_bulleyes_left");
            $("#submarine_baloon_right").attr("id", "submarine_baloon_left");
            
            submarineLeft = true;
        }
        
        // IE6 Fix
        if($.browser.msie && $.browser.version == "6.0")
        {
            $("#submarine").wifixpng().wiunfixpng().wifixpng();
        }
        
        lastOffset = currentOffset.left;
    },
    containment: 'body'
   });
   
   // Mouse actions plaatsen
   $("#submarine").bind('mouseover', function() {
    // We gaan de x / y ophalen van de background
    var offsetBackground = $("#background_container").offset();
    var offsetSubmarine = $("#submarine").offset();
    
    // We gaan de x / y uitrekenen en doorgeven
    if(!submarineDrag) tooltip('show', 'submarine', (offsetSubmarine.left - offsetBackground.left), (offsetSubmarine.top - offsetBackground.top), $(this).width(), $(this).height(), '<img src="/images/tooltip_duikboot.gif" alt="" border="0" />');
   });
   $("#submarine").bind('mouseout', function() {
        // We gaan de boel weer verbergen
        tooltip('hide', 'submarine');
   });
   $("#submarine").bind('dragstart', function() {
        // We gaan de boel weer verbergen
        tooltip('hide', 'submarine');
        submarineDrag = true;
   });
   $("#submarine").bind('dragstop', function() {
        // We gaan de x / y ophalen van de background
        var offsetBackground = $("#background_container").offset();
        var offsetSubmarine = $("#submarine").offset();
    
        // We gaan de boel weer op false zetten
        tooltip('show', 'submarine', (offsetSubmarine.left - offsetBackground.left), (offsetSubmarine.top - offsetBackground.top), $(this).width(), $(this).height(), '<img src="/images/tooltip_duikboot.gif" alt="" border="0" />');
        submarineDrag = false;
   });
   
   if($.browser.msie && $.browser.version == '6.0')
   {
       $("#submarine").bind('click', function() {
            showSubmarine();
       });
   }
   
   // Scrollbar voorkomen
   /*if($(window).height() < $("#background_container").height())
   {*/
        // We gaan kijken hoever we de achtergrond moeten bijstellen
        /*var changeMent = ($("#site_container").height() - 80);
    
        // We gaan de achtergrond bijstellen
        if(($("#background_container").height() - changeMent) > 790)
        {
            $("#background_container").css("top", "-" + changeMent + "px");
        } else {
            $("#background_container").css("top", "-" + ($("#site_container").height() - 80) + "px");
        }*/
   /*}*/
   
   // We gaan de gegevens van de navigatie ophalen
   var navigationOffset = $("#site_navigation").offset();
   var navigationHeight = $("#site_navigation").height();
   
   if($(window).height() > $("body").height()) {
    $("#background_container").css("top", ($(window).height() - 160) + "px");
   } else {
    $("#background_container").css("top", (navigationOffset.top + navigationHeight) + "px");
   }
   
   // We gaan de gegevens van de background-container ophalen
   var backgroundOffset = $("#background_container").offset();
   var backgroundHeight = $("#background_container").height();
   var footerHeight = ($("#site_footer").height() + 4);
   
   // We gaan de nieuwe top positie bereken
   var newTop = (backgroundOffset.top + (footerHeight + 60));   
   $("#site_footer").css("top", newTop + "px");
});

// Functie voor het weergeven van een tooltip
function tooltip(act, type, x, y, width, height, text)
{
    var x = x || 0;
    var y = y || 0;
    var text = text || "";
    var working_id = "tooltip_" + type;
    
    // We gaan kijken welke actie we moeten doen.
    if(act=='show')
    {
        // We gaan de position ophalen van de background_container
        var offsetB = $("#background_container").offset();
        
        // We gaan een nieuwe container maken
        var workhtml = '<div class="WSTooltip" id="' + working_id + '">';
        workhtml += '<div class="WSTooltiphead">' + text + '</div>';
        workhtml += '</div>';
        
        // We gaan de html plaatsen
        $("#site_container").after(workhtml);
        
        // We gaan kijken welk type het is
        switch(type)
        {
            case "fish":
                // We gaan kijken hoeveel de tooltip naar links / rechts moet
                chnLeft = 24;
            break;
            case "submarine":
                // We gaan kijken hoeveel de tooltip naar links / rechts moet
                if(submarineLeft)
                {
                    chnLeft = 100;    
                } else {
                    chnLeft = 0;
                }                            
            break;
        }
        
        // We gaan de box juist plaatsen
        $("#" + working_id).css("left", ((offsetB.left + x + ((width / 2 - ($("#" + working_id + " img").width() / 2)) - ($("#" + working_id).width() / 2))) + chnLeft) + "px");
        $("#" + working_id).css("top", ((offsetB.top + y) - ($("#" + working_id).height() - 10)) + "px");
    } else if(act=='hide') {
        // We gaan de container verwijderen
        $("#" + working_id).remove();
    }
}

// Flash functies
// Functie voor het weergeven van de tooltip
function showTooltip(type, x, y, width, height) {
    tooltip('show', 'fish', parseInt(x), parseInt(y), width, height, '<img src="/images/tooltip_fish.gif" alt="" border="0" />');
}

// Functie voor het verbergen van de tooltip
function hideTooltip(type) {
    tooltip('hide', 'fish');
}