﻿//Javascript init
var backgoundImageTimer;
var nextImageIndex = 1;
var topmenuHover = false;
var textFieldHeight = -1;

var jsSize = 1;
switch(websiteSize)
{
    case 0:
        jsSize = 0.64;
    break;
    case 1:
        jsSize = 0.8;
    break;
    case 2:
        jsSize = 1;
    break;
}//switch

//When dom is ready.
$(document).ready(function() {
    
    InitElements();
});

function InitElements(){

    $(".top-menu-item").mouseenter(function() {

        topmenuHover = true;
        var imageSrc = $(this).attr("src");
        if (imageSrc.length > 0)
            updateMenuHoverFlash(imageSrc);
    });

    $(".top-menu-item").mouseleave(function() {

        topmenuHover = false;
        hideMenuHoverFlash();
    });
    
    $(".divPackageDealItem").mouseenter(function(){
      
        $(".imgFrontpagePackageDealMaskOn").css({"display" : "block"});
        $(".imgFrontpagePackageDealMaskOff").css({"display" : "none"});            
    });
    
    $(".divPackageDealItem").mouseleave(function(){
        
        $(".imgFrontpagePackageDealMaskOn").css({"display" : "none"});
        $(".imgFrontpagePackageDealMaskOff").css({"display" : "block"}); 
    });

    $(".divBookingLinkcontainer").mouseenter(function() {

        $(".on",this).css({ "display": "block" });
        $(".off",this).css({ "display": "none" });
    });

    $(".divBookingLinkcontainer").mouseleave(function() {

        $(".on",this).css({ "display": "none" });
        $(".off",this).css({ "display": "block" });
    });


    $(".divBookingLinkcontainer").click(function() {

        if ($(".divBookingLink", this).hasClass("show")) {

            $(".divBookingLink", this).removeClass("show");
            $(".divBookingLink", this).css({ "display": "none" });
            $(".divBookingBack", this).addClass("show");
            $(".divBookingBack", this).css({ "display": "block" });

            //Show booking
            $("#iframeBooking").attr("src", bookingLinkURL);
            $(".divBookingIframe").css({ "display": "block" });
            $(".large-sun-frontpage").css({ "display": "none" });
            $(".large-sun-article").css({ "display": "none" });
            $(".divImageCounterField").css({ "display": "none" });
            $(".divMainFlashContainer").css({ "display": "none" });
            $(".divPrevImageArrow").css({ "display": "none" });
            $(".divNextImageArrow").css({ "display": "none" });
            $(".divSmallTextFieldRoot").css({ "display": "none" });
            $(".divToggleTextFieldContainer").css({ "display": "none" });
            
            
            //Stop flash timer
            clearInterval(backgoundImageTimer);

        } //if
        else {

            $(".divBookingLink", this).addClass("show");
            $(".divBookingLink", this).css({ "display": "block" });
            $(".divBookingBack", this).removeClass("show");
            $(".divBookingBack", this).css({ "display": "none" });

            //Hide booking
            $(".divBookingIframe").css({ "display": "none" });
            $(".large-sun-frontpage").css({ "display": "block" });
            $(".large-sun-article").css({ "display": "block" });
            $(".divImageCounterField").css({ "display": "block" });
            $(".divMainFlashContainer").css({ "display": "block" });
            $("#iframeBooking").attr("src", "");

            $(".divPrevImageArrow").css({ "display": "block" });
            $(".divNextImageArrow").css({ "display": "block" });

            $(".divSmallTextFieldRoot").css({ "display": "block" });
            $(".divToggleTextFieldContainer").css({ "display": "block" });
            
            //Start timer
            if ($("#divImageLinkContainer a").length > 1)
                backgoundImageTimer = setInterval("nextImage()", 6000);
        } //else
    });

    $(".divPrevImageArrow").click(function() {

        prevImage();
    });

    $(".divNextImageArrow").click(function() {

        nextImage();
    });

    //Init textField
    if (boolHideTextField) {

        $(".divSmallTextFieldRoot").animate({ "height": 'toggle', "width": 'toggle' }, 0);

        $(".hideTextField").removeClass("show");
        $(".hideTextField").css({ "display": "none" });

        $(".showTextField").addClass("show");
        $(".showTextField").css({ "display": "block" });
    }//if


  
  
  if (pagetype != 99){
   //Init menu scroll position
            if (packageDealMenuScrollPosition > 0) {

                $(".divPackageDealMenuDocument").css({ "top": "" + -1 * (packageDealMenuScrollPosition) * calcValue(153 + 7 + 4) + "px" });
            }//if

            //Up
            $(".divPackageDealArrowUp").click(function() {

                if (packageDealMenuScrollPosition > 0) {

                    $(".divPackageDealMenuDocument").animate({ "top": "" + -1 * (packageDealMenuScrollPosition - 1) * calcValue(153 + 7 + 4) + "px" }, 500);
                    packageDealMenuScrollPosition--;

                    //Update scroll position
                    ScriptService.UpdateScrollOffset("packagedealmenu", "" + packageDealMenuScrollPosition, onVoidComplete, OnTimeOut, OnError);
                } //if
            });

            //Down
            $(".divPackageDealArrowDown").click(function() {

                if (packageDealMenuScrollPosition < $(".divPackagedealItemContainer").length - 3) {

                    $(".divPackageDealMenuDocument").animate({ "top": "" + -1 * (packageDealMenuScrollPosition + 1) * calcValue(153 + 7+ 4) + "px" }, 500);
                    packageDealMenuScrollPosition++;

                    //Update scroll position
                    ScriptService.UpdateScrollOffset("packagedealmenu", "" + packageDealMenuScrollPosition, onVoidComplete, OnTimeOut, OnError);
                } //if
            });

  
  
  
  
  }
  



           //imgToggleText
            // TODO: også på divToggleTextFieldContainer
  $('.imgToggleText').click(function() {

      //TODO: Lave en nicer divScrollbar.animate
      if ($(".hideTextField").hasClass("show")) {
          $(".divScrollbarContainer").animate({ 'height': 'toggle' }, 0);
      }


      $(".divTextContainerWrapper").animate({ 'height': 'toggle' }, 1000, '', function() {

          //Change visible text
          if ($(".hideTextField").hasClass("show")) {

              $(".hideTextField").removeClass("show");
              $(".hideTextField").css({ "display": "none" });

              $(".showTextField").addClass("show");
              $(".showTextField").css({ "display": "block" });
          } //if
          else {

              $(".divScrollbarContainer").animate({ 'height': 'toggle' }, 0);
              
              $(".hideTextField").addClass("show");
              $(".hideTextField").css({ "display": "block" });

              $(".showTextField").removeClass("show");
              $(".showTextField").css({ "display": "none" });
          } //else

      });
  });
            
             $('.divToggleTextFieldContainer').click(function() {
                //TODO: Lave en nicer divScrollbar.animate
             if ($(".hideTextField").hasClass("show")) {
                 $(".divScrollbarContainer").animate({ 'height': 'toggle' }, 0);
             }
                
                $(".divTextContainerWrapper").animate({'height':'toggle'},1000,'',function() {
                

                    //Change visible text
                    if ($(".hideTextField").hasClass("show")) {

                        $(".hideTextField").removeClass("show");
                        $(".hideTextField").css({ "display": "none" });

                        $(".showTextField").addClass("show");
                        $(".showTextField").css({ "display": "block" });
                    } //if
                    else {

                        $(".divScrollbarContainer").animate({ 'height': 'toggle' }, 0);
                        
                        $(".hideTextField").addClass("show");
                        $(".hideTextField").css({ "display": "block" });

                        $(".showTextField").removeClass("show");
                        $(".showTextField").css({ "display": "none" });
                    } //else
                });     
            });           

  
  
      
    //Check pagetype        
    switch(pagetype)
    {
        case 1:
            
 
            InitScrollBar('divTextRoot', 'divTextContainer', 'divTextDocument', 'divScrollbarContainer', false);


            //Load background flash
            initBackgroundFlash();

            //Hide arrow helptext
            setTimeout("hideArrowHelpText()", 5000);
            break;
        case 2:
            InitScrollBar('divLargeTextFieldRoot', 'divLargeTextFieldContainer', 'divLargeTextFieldDocument', 'divLargeTextFieldScrollContainer', false);
            break;
        case 3:


            InitScrollBar('divTextRoot', 'divTextContainer', 'divTextDocument', 'divScrollbarContainer', false);

            //Load background flash
            initBackgroundFlash();

            //Hide arrow helptext
            setTimeout("hideArrowHelpText()", 5000);
            break;
        case 4:
           
            InitScrollBar('divTextRoot', 'divTextContainer', 'divTextDocument', 'divScrollbarContainer', false);

            //Load background flash
            initBackgroundFlash();

            //Hide arrow helptext
            setTimeout("hideArrowHelpText()", 5000);
            break;
        case 5:
            initBackgroundFlash();
            InitScrollBar('divTextRoot', 'divTextContainer', 'divTextDocument', 'divScrollbarContainer', false);
            break;
        case 6:
            //Set booking form url
            $("#iframeBooking").attr("src", bookingLinkURL);
            $(".divBookingIframe").css({ "display": "block" });
            break;
        case 7:
            InitScrollBar('divCalenderContentRoot', 'divCalenderContentContainer', 'divCalenderContentDocument', 'divCalenderContentScrollbarContainer', false);

            $(".trCalendarWeek").mouseenter(function() {
                $(".tdCalenderDayCell", this).addClass("tdCalenderDayCellOn");
            });

            $(".trCalendarWeek").mouseleave(function() {
                $(".tdCalenderDayCell", this).removeClass("tdCalenderDayCellOn");
            });

            $(".divCalendarEventContainer").mouseenter(function() {
                $(".tdCalendarEventTopOff", this).addClass("tdCalendarEventTopOn");
                $(".tdCalendarEventTopOff", this).removeClass("tdCalendarEventTopOff");

                $(".tdCalendarEventCenterOff", this).addClass("tdCalendarEventCenterOn");
                $(".tdCalendarEventCenterOff", this).removeClass("tdCalendarEventCenterOff");

                $(".tdCalendarEventBottomOff", this).addClass("tdCalendarEventBottomOn");
                $(".tdCalendarEventBottomOff", this).removeClass("tdCalendarEventBottomOff");

                $(".divCalendarInfoOff", this).addClass("divCalendarInfoOn");
                $(".divCalendarInfoOff", this).removeClass("divCalendarInfoOff");
            });

            $(".divCalendarEventContainer").mouseleave(function() {
                $(".tdCalendarEventTopOn", this).addClass("tdCalendarEventTopOff");
                $(".tdCalendarEventTopOn", this).removeClass("tdCalendarEventTopOn");

                $(".tdCalendarEventCenterOn", this).addClass("tdCalendarEventCenterOff");
                $(".tdCalendarEventCenterOn", this).removeClass("tdCalendarEventCenterOn");

                $(".tdCalendarEventBottomOn", this).addClass("tdCalendarEventBottomOff");
                $(".tdCalendarEventBottomOn", this).removeClass("tdCalendarEventBottomOn");

                $(".divCalendarInfoOn", this).addClass("divCalendarInfoOff");
                $(".divCalendarInfoOn", this).removeClass("divCalendarInfoOn");
            });

            $(".divCalendarEventContainer").click(function() {

                //Get event id
                var eventId = this.id.split("_")[1];

                //Update hash
                window.location.hash = "" + eventId;

                //Update print link
                $(".aCalenderPrint").attr("href", "print.aspx?type=event&id=" + eventId);

                //Load event data {date, time, text and image}
                ScriptService.GetEventData(eventId, onGetEventDataComplete, OnTimeOut, OnError);
            });

            //Check for start hash
            if (window.location.hash.length > 1) {

                //Load selected event
                ScriptService.GetEventData(parseInt(window.location.hash.substring(1)), onGetEventDataComplete, OnTimeOut, OnError);
            } //if

            $(".divCalenderEventDetailsHideContainer").click(function() {

                //Hide outer container with slide effect
                $(".divCalenderEventDetailsOuterContainer").animate({ top: calcValue(658) + "px" }, 500, "", function() { $(".divCalenderEventDetailsRoot").css({ "display": "none" }); });
            });


            $(".divCalenderEventDetailsHideContainer").mouseenter(function() {
                $(".toggleBackgroundOffRight", this).addClass("toggleBackgroundOnRight");
                $(".toggleBackgroundOffRight", this).removeClass("toggleBackgroundOffRight");

                $(".toggleBackgroundOffCenter", this).addClass("toggleBackgroundOnCenter");
                $(".toggleBackgroundOffCenter", this).removeClass("toggleBackgroundOffCenter");

                $(".toggleContainer").addClass("toggleContainerOn").removeClass("toggleContainerOff");
            });

            $(".divCalenderEventDetailsHideContainer").mouseleave(function() {
                $(".toggleBackgroundOnRight", this).addClass("toggleBackgroundOffRight");
                $(".toggleBackgroundOnRight", this).removeClass("toggleBackgroundOnRight");

                $(".toggleBackgroundOnCenter", this).addClass("toggleBackgroundOffCenter");
                $(".toggleBackgroundOnCenter", this).removeClass("toggleBackgroundOnCenter");

                $(".toggleContainer").addClass("toggleContainerOff").removeClass("toggleContainerOn");
            });
            break;
        case 10:
            //Load background flash
            initBackgroundFlash();
            
            //Start package deal timer
            setInterval("changePackdealItem()",5000);
            break;
        default:
            break;
    }//switch
    
    //Init hover flash
    setTimeout("initTopMenuHoverFlash()",100);
    
    //Init addthis
    setTimeout("initAddThis()",100);
}//InitElements

function hideArrowHelpText() {

    $(".divArrowTextContainer").animate({"opacity" : "0"},1000);
}

function changePackdealItem(){

    var current = ($('.divPackageDealContainer div.show')?  $('.divPackageDealContainer div.show') : $('.divPackageDealContainer div:first'));

	//Get next image, if it reached the end of the slideshow, rotate it back to the first image
	var next = ((current.next().length) ? ( current.next()) : $('.divPackageDealContainer div:first'));	
	
	if(jQuery.support.opacity){
	
	    //Start fade animation
	    next.css({"display":"block"}).animate({"opacity":"1.0"}, 1000);
	    current.animate({"opacity":"1.0"}, 1000).animate({"opacity":"0.0"}, 1000, '', function(){
	        
	        $(this).css({"display":"none"});
	    });
	}//if
	else{
	    next.css({"display":"block"});
	    current.css({"display":"none"});
	}//else
	
	
	//Update next pointer
	next.addClass('show');
	current.removeClass('show');
}//changePackdealItem

function initTopMenuHoverFlash(){

}//initTopMenuHoverFlash

function updateMenuHoverFlash(n){
    
    //Update flash image
    //thisMovie("sun_01").displayImage(n);
}
function hideMenuHoverFlash(){
    
    //Hide hover flash
   // $("#divHoverEffectFlashContainer").css({ "z-index": "-1" });
}

function initAddThis(){

    $(".divAddThis").html('<!-- AddThis Button BEGIN --><a class="addthis_button" href="http://www.addthis.com/bookmark.php?v=250&amp;username=xa-4b9ddf823ae6a32c"><img src="http://s7.addthis.com/static/btn/v2/lg-share-en.gif" width="125" height="16" alt="Bookmark and Share" style="border:0"/></a><script type="text/javascript" src="http://s7.addthis.com/js/250/addthis_widget.js#username=xa-4b9ddf823ae6a32c"></script><!-- AddThis Button END -->');
}//initAddThis

function initBackgroundFlash(){

    //Init flash div
    setTimeout("LoadLargeBackgroundFlash()",100);
    
    //Start timer
    if (animatedBackground && $("#divImageLinkContainer a").length > 1)
        backgoundImageTimer = setInterval("nextImage()", 6000);
}//initBackgroundFlash

function nextImage(){

    //Stop timer
    clearInterval(backgoundImageTimer);

    //if no IMGs have the show class, grab the first image
	var current = ($('#divImageLinkContainer a.show')?  $('#divImageLinkContainer a.show') : $('#divImageLinkContainer a:first'));

    //Update image index
    if(current.next().length > 0)
        nextImageIndex++;
    else
        nextImageIndex = 1;

	//Get next image, if it reached the end of the slideshow, rotate it back to the first image
	var next = ((current.next().length) ? ( current.next()) : $('#divImageLinkContainer a:first'));	
	
	var nextImageSrc = next.find('span').attr('rel');
	
	//Update background flash image
	thisMovie("slide_js").displayImage(nextImageSrc);
	
	//Update next pointer
	next.addClass('show');
	current.removeClass('show');

	//Start timer
	if(animatedBackground)
        backgoundImageTimer = setInterval("nextImage()", 6000);
}//nextImage

function prevImage(){

    //Stop timer
    clearInterval(backgoundImageTimer);
    
    //if no IMGs have the show class, grab the first image
	var current = ($('#divImageLinkContainer a.show')?  $('#divImageLinkContainer a.show') : $('#divImageLinkContainer a:first'));

    //Update image index
    if(current.prev().length > 0)
        nextImageIndex--;
    else
        nextImageIndex = $('#divImageLinkContainer').children().length;

	//Get next image, if it reached the end of the slideshow, rotate it back to the first image
	var previous = ((current.prev().length) ? ( current.prev()) : $('#divImageLinkContainer a:last'));	
	
	//Get next image caption
	var nextImageSrc = previous.find('span').attr('rel');
	
	//Update background flash image
	thisMovie("slide_js").displayImage(nextImageSrc);
	
	//Update next pointer
	previous.addClass('show');
	current.removeClass('show');

	//Start timer
	if (animatedBackground)
        backgoundImageTimer = setInterval("nextImage()", 6000);
}//prevImage

function LoadLargeBackgroundFlash(){
 
     document.getElementById("divMainFlashContainer").innerHTML = AC_FL_RunContent(
			    'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,24,0',
			    'width', calcValue(1033) + 'px',
			    'height', calcValue(634) +'px',
			    'src', 'slide_js',
			    'quality', 'high',
			    'pluginspage', 'http://www.macromedia.com/go/getflashplayer',
			    'align', 'top',
			    'play', 'true',
			    'loop', 'true',
			    'scale', 'noscale',
			    'wmode', 'transparent',
			    'devicefont', 'false',
			    'id', 'slide_js',
			    'bgcolor', '#cccccc',
			    'name', 'slide_js',
			    'menu', 'true',
			    'allowScriptAccess','sameDomain',
			    'allowFullScreen','false',
			    'movie', 'slide_js',
			    'FlashVars', "test=false&intern=false&id=largeFlash&lib="+backgroundImageFolder+"/&imgs="+ firstBackgroundImage + "&display_sec=4&fade_sec=2&stage_width="+calcValue(1033)+"px&stage_height="+calcValue(634)+"px",
			    'salign', 'lt'
			    ); //end AC code  
		    
			             
}//LoadLargeBackgroundFlash

function flashRespons(n,id){

    if (n == "loaded") {

        if (topmenuHover) {

            //Show menu hover flash
            $("#divHoverEffectFlashContainer").css({ "z-index": "10" });
        }// if
    } //if
    else {

        if ($("#divImageLinkContainer a").length == 0)
            nextImageIndex = 0;
        
        $(".divImageCounterField").attr("innerHTML", nextImageIndex + " / " + $("#divImageLinkContainer a").length);
    }//else
}//flashRespons

function thisMovie(movieName) {
    return document[movieName];
}//thisMovie

function calcValue(intValue){
    return parseInt(intValue * jsSize);
}//calcValue

function resValue(int1, int2, int3){
    
    switch(websiteSize)
    {
        case 0:
            return int1;
        break;
        case 1:
            return int2;
        break;
        case 2:
            return int3;
        break;
    }//switch
}//resValue

/*
function onGetNewsDataComplete(args){
     
    $(".divRightTextDocument").html("<h1>"+ args[0] +"</h1><span class=\"spanNewsDate\">"+ args[2] +"</span><br />"+ args[1]);

    //Reset document top
    $(".divRightTextDocument").css({"top":"0px"});

    //Init scrollbar (news field)
	InitScrollBar('rightTextFieldCenter','divRightTextContainer', 'divRightTextDocument','divRightScrollbarContainer', false);
                    
    //show right text field
    $(".contentRightTextFieldRoot").animate({width: calcValue(507) + "px"},1000);
}

function onGetEventDataComplete(args){

    //Hide outer container
    $(".divCalenderEventDetailsOuterContainer").css({"top": calcValue(658) + "px"});

    //Update content
    $(".divCalenderEventDetailsDocument").html(args[2] + "<h1>" + args[0] + "</h1>" + args[1]);
    $(".divCalenderEventDetailsImage").attr("src", args[3]);
    $(".divCalenderEventDetailsDocument").css({"top": 0 + "px"});

    //Show root
    $(".divCalenderEventDetailsRoot").css({"display":"block"});
    
    //Init scroll bars
    InitScrollBar('divCalenderEventDetailsOuterContainer','divCalenderEventDetailsContainer', 'divCalenderEventDetailsDocument','divCalenderEventDetailsScrollbarContainer', false);
    
    //Show outer container with slide effect
    $(".divCalenderEventDetailsOuterContainer").animate({top: "0px"},500);
}
*/
function onGetNextPackageDealDataComplete(args){

    //Update show package deal
    //$(".divPackageDealContainer").attr("id", "item_" + args[4]);
    //$(".imgFrontpagePackageDeal").attr("src", args[1]);
    

    //alert(args[0]);
    
    
    /*
    <div id="item_<%=packageDealDS.Tables[0].Rows[0]["pdID_INT"]%>" class="">
    <img class="imgFrontpagePackageDeal" src="<%=dnmDatabaseInterface.GetImageSrc(packageDealDS.Tables[0].Rows[0]["imgMenuImage_INT"].ToString(), "img_packagedeal", Convert.ToInt32(Session["size"]))%>" alt=""/>
    <img class="imgFrontpagePackageDealMask" src="gfx/frontpage_packagedeal_on_<%=Session["size"]%>.png" alt="" />
    <img class="imgFrontpagePackageDealMask" src="gfx/frontpage_packagedeal_off_<%=Session["size"]%>.png" alt="" />
    <table class="tblFrontpagePackageDealText" cellpadding="0" cellspacing="0">
        <tr>
            <td colspan="2" class="tdPackagedealTitle"><%=packageDealDS.Tables[0].Rows[0]["title_" + Session["lang"]]%></td>
        </tr>
        <tr>
            <td colspan="2" class="tdPackagedealText"><%=packageDealDS.Tables[0].Rows[0]["subtitle_" + Session["lang"]]%></td>
        </tr>
        <tr>
            <td class="tdPackagedealPrice"><%=((Dictionary<string, object>)Session["labels"])["lblPrice"]%></td>
            <td class="tdPackagedealPrice"><%=packageDealDS.Tables[0].Rows[0]["price_" + Session["lang"]]%></td>
    */
}

function onVoidComplete(args){

}

function OnTimeOut(args) {
    
    //Reload page    
    alert("Service call timed out." + args);
}

function OnError(args) {
    alert("Error calling service method.");
}

function getViewportWidth() {
    
    var width = self.innerWidth;  // Safari
    var mode = document.compatMode;
    if (mode || isIE) { // IE, Gecko, Opera
        width = (mode == 'CSS1Compat') ?
        document.documentElement.clientWidth : // Standards
        document.body.clientWidth; // Quirks
    }
    return width;
}

function onGetEventDataComplete(args) {

    //Hide outer container
    $(".divCalenderEventDetailsOuterContainer").css({ "top": calcValue(658) + "px" });

    //Update content
    $(".divCalenderEventDetailsDocument").html(args[2] + "<h1>" + args[0] + "</h1>" + args[1]);
    $(".divCalenderEventDetailsImage").attr("src", args[3]);
    $(".divCalenderEventDetailsDocument").css({ "top": 0 + "px" });

    //Show root
    $(".divCalenderEventDetailsRoot").css({ "display": "block" });

    //Init scroll bars
    InitScrollBar('divCalenderEventDetailsOuterContainer', 'divCalenderEventDetailsContainer', 'divCalenderEventDetailsDocument', 'divCalenderEventDetailsScrollbarContainer', false);

    //Show outer container with slide effect
    $(".divCalenderEventDetailsOuterContainer").animate({ top: "0px" }, 500);
}