addEvent(window, "load", addGroupHeaderEvents, false);
addEvent(window, "load", addSelectSeasonEvents, false);
addEvent(window, "load", addEnokViewerEvents, false);

$(document).ready(function () {
    $("#open-tema-map, #tema-small-map").click(function () {
        var ctrlId = $(".media-tabs").attr("id");
        var 
            ctrl = $find(ctrlId),
            numOfTabs = ctrl.get_tabs().length;

        if (numOfTabs > 3) { ctrl.set_activeTab(ctrl.get_tabs()[3]); }
        else if (numOfTabs > 2) { ctrl.set_activeTab(ctrl.get_tabs()[2]); }
        else { ctrl.set_activeTab(ctrl.get_tabs()[1]); }
    });
    
    $("a.open-large-google-map").click(function () {
        $("#large-google-map-container-outermost").css("height", "650px").css("margin-bottom", "15px");
        $("#large-google-map-container").fadeIn("slow");
        $("#iframe-map").html('<iframe width="864" height="561" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.com/maps/ms?ie=UTF8&amp;hl=sv&amp;msa=0&amp;msid=102000499425842356586.000490d447ead4ca57196&amp;ll=30.287532,-13.688965&amp;spn=5.320551,9.481201&amp;z=7&amp;output=embed"></iframe>');
        $("#map-small").fadeOut("slow");
    });

    $("a#close-google-iframe").click(function () {
        $("div#large-google-map-container-outermost").css("height", "0px").css("margin-bottom", "0px");
        $("div#large-google-map-container").fadeOut("slow");
        $("#map-small").fadeIn("slow");
    });
});


function setTemaTopStyle(imgName) {
    $("#media-viewer .ajax__tab_header").css("background", String.format("transparent url({0}/Images/BlueSites/Tema/Layout/{1}) no-repeat scroll center 22px", oBaseAppSettings.IncludeSiteDirectory, imgName));
}

function setTabStyle(sender, e) {
    var tabCssClass = sender.get_activeTab().get_cssClass();
    var cssImageUrl = "transparent url(" + oBaseAppSettings.IncludeSiteDirectory + "/Images/BlueSites/Tema/MediaViewer/box-655-filler-bg.png) repeat-y scroll left top";
    if (tabCssClass === "video-viewer-tab") {        
        $(".ajax__tab_body_bottom").hide();
        if (oBaseAppSettings.IsTemaSite) {
            $("#media-viewer div.ajax__tab_panel").css("background", "none");
            setTemaTopStyle("box-659x500-top-bg.png");            
        } 
    }
    else if (tabCssClass == "pictures-viewer-tab") {
        if ($('div.image-viewer-small').get(0) !== null) {
            $('div.image-viewer-small').get(0).control.start(1);
        }
        if (oBaseAppSettings.IsTemaSite) {
            $("#media-viewer div.ajax__tab_panel").css("background", cssImageUrl);
            setTemaTopStyle("box-655x500-top-bg.png");            
        }
        $(".ajax__tab_body_bottom").show();        
    }
    else {
        $(".ajax__tab_body_bottom").show();
        if (oBaseAppSettings.IsTemaSite) {
            $("#media-viewer div.ajax__tab_panel").css("background", cssImageUrl);
            setTemaTopStyle("box-655x500-top-bg.png");            
        }      
    }
}

function addOpenMoreFactsBlock(){
    $("a.more-facts-JS").click(function() {
        if ($(this).attr("id") == "more-facts-id-dest") {
            $(this).text(($(this).text() == oTUIStrings.strShowDayProgram) ? oTUIStrings.strHideDayProgram : oTUIStrings.strShowDayProgram);
        }
        else {
            $(this).text(($(this).text() == oTUIStrings.strShowMoreFact) ? oTUIStrings.strHideMoreFact : oTUIStrings.strShowMoreFact);
        }

        if ($(this).attr("class") == "more-facts-JS") {
            $(this).removeClass("more-facts-JS").addClass("more-facts-JS-hide").toggleClass("selected");
            $("dl#more-facts-dl").fadeIn("slow");
        }
        else {
            $(this).removeClass("more-facts-JS-hide").addClass("more-facts-JS").toggleClass("selected");
            $("dl#more-facts-dl").fadeOut("slow");
        }
        return false;
    });
}

function addExpandMoreHotelInfo(){
	$("a.info-link").click(function(){
		var oInfoDiv = document.getElementById("detailed-hotel-info-container");	
		var link = document.getElementById("detailed-hotel-info-link");	
		link.className = (link.className == "info-link") ? "info-link-selected" : "info-link";
		oInfoDiv.className = (oInfoDiv.className == "display-none") ? "display-block" : "display-none";			
	return;
	});
}

var arrAddOnsGroupHeaderLinks;
function addGroupHeaderEvents(){
	var oGroupHeaderContainer = document.getElementById("hotel-info");
	if (!oGroupHeaderContainer) {
	    oGroupHeaderContainer = document.getElementById("hotel-comparison");
	}
	
	if(oGroupHeaderContainer){
		arrAddOnsGroupHeaderLinks = getElementsByClassName(oGroupHeaderContainer, "a", "group-header-link");
		for(var i=0; i<arrAddOnsGroupHeaderLinks.length; i++){
			arrAddOnsGroupHeaderLinks[i].onclick = function (oEvent){
				var oEvent = (typeof oEvent != "undefined")? oEvent : event;
				oEvent.returnValue = false;
				if(oEvent.preventDefault){
					oEvent.preventDefault();
				}
				expandCollapseRows(this);				
			}
		}
		var oShowAllRows = document.getElementById("show-all-rows");
		if(oShowAllRows){
			oShowAllRows.onclick = function(){
				expandCollapseAllRows(true);
			};			
		}
		var oHideAllRows = document.getElementById("hide-all-rows");
		if(oHideAllRows){
			oHideAllRows.onclick = function(){
				expandCollapseAllRows(false);
			};
		}
	}	
}
function expandCollapseAllRows(bExpand){
	if(typeof arrAddOnsGroupHeaderLinks != "undefined"){
		for(var i=0; i<arrAddOnsGroupHeaderLinks.length; i++){
			expandCollapseRows(arrAddOnsGroupHeaderLinks[i], bExpand);
		}
	}
}
function expandCollapseRows(oElm, bExpand){
	
	var oTableRow = oElm.parentNode.parentNode;
	var bExpand = (typeof bExpand != "undefined")? bExpand : (oTableRow.className.search(/expanded-group/) == -1)? true : false;	
	var strRowClassName = "display-table-row";
	if(bExpand){
		oTableRow.className = oTableRow.className.replace(/\sexpanded-group/i, "") + " expanded-group";
	}
	else{
		oTableRow.className = oTableRow.className.replace(/\sexpanded-group/i, "");
		strRowClassName = "display-none";
	}	
	var strNewClassName;
	while(oTableRow.nextSibling){		
		oTableRow = oTableRow.nextSibling;
		if(oTableRow.nodeName.search(/tr/i) != -1 && oTableRow.className.search(/group-header/) == -1){
			strNewClassName = oTableRow.className.replace(/\s?display-(table-row|none)/gi, "");
			oTableRow.className = strNewClassName + " " + strRowClassName;
		}
		else if(oTableRow.nodeType == 3){
			continue;
		}
		else{
			break;
		}
	}
}
function redirectResortSeason(value){
	window.location = value;
}

$(document).ready(function() {
    var $totalImagesCount = 0;
    loadCarousel = function() {
        var 
                $totalImagesCount = $("div.carousel div.images ul:first li").length,
                $visibleImagesCount = 5;

        if ($totalImagesCount < 6) {
            $("div.scroll").hide();
            $visibleImagesCount = $totalImagesCount;
        }

        $("div.carousel .images").jCarouselLite({
            btnNext: "div.carousel .next",
            btnPrev: "div.carousel .prev",
            visible: $visibleImagesCount,
            vertical: true,
            mouseWheel: true,
            speed: 500,
            circular: false,
            afterEnd: function(elements) {
                var $filePath = $(elements).filter('li:eq(0)').children().attr('src').replace('thumbs/', '');
                rewriteApplet($filePath);
            }
        });

        $("div.carousel img").click(function() {
            var $filePath = $(this).attr('src').replace('thumbs/', '');
            rewriteApplet($filePath);
        });

        $("span.panorama-viewer-tab").die("click", loadCarousel);
    }

    $("span.panorama-viewer-tab").live("click", loadCarousel);

    rewriteApplet = function(file) {

        var $barColor = "CCE6FF";
        if (oBaseAppSettings.IsTemaSite) {
            $barColor = "881f36";
        }

        var $appletCode = '<applet archive="/TuiPlugins/Media/qtptviewer.jar" code="ptviewer.class" width="565" height="343">' +
                '	<param name="file" value="' + file + '" />' +
                '	<param name="panmin" value="-180" />' +
                '	<param name="panmax" value="180" />' +
                '	<param name="pan" value="0" />' +
                '	<param name="tiltmin" value="-60" />' +
                '	<param name="tiltmax" value="80" />' +
                '	<param name="tilt" value="0" />' +
                '	<param name="fovmin" value="42" />' +
                '	<param name="fovmax" value="119" />' +
                '	<param name="fov" value="95" />' +
                '	<param name="wait" value="/TuiPlugins/Media/' + oBaseAppSettings.BrandID + '/loading.gif" />' +
                '	<param name="bar_x" value="162" />' +
                '	<param name="bar_y" value="228" />' +
                '	<param name="bar_width" value="240" />' +
                '	<param name="bar_height" value="16" />' +
                '	<param name="barcolor" value="' + $barColor + '" />' +
                '	<param name="view_x" value="0" />' +
                '	<param name="view_y" value="0" />' +
                '	<param name="view_width" value="565" />' +
                '	<param name="view_height" value="343" />' +
                '	<param name="quality" value="1" />' +
                '	<param name="frame" value="' + oBaseAppSettings.IncludeSiteDirectory + '/Images/BlueSites/' + addPrefixIfTema('Tema/') + 'MediaViewer/PanoramaViewer/control-bar.gif" />' +
                '	<param name="bgcolor" value="FFFFFF" />' +
                '	<param name="auto" value="0.3" />' +
                '   <param name="shotspot0" value=" x382 y313 a405 b340 u\'ptviewer:startAutoPan(1.0,0,1.0)\' " />' +
                '   <param name="shotspot1" value=" x348 y313 a375 b340 u\'ptviewer:startAutoPan(-1.0,0,1.0)\' " />' +
                '   <param name="shotspot2" value=" x287 y313 a317 b340 u\'ptviewer:startAutoPan(0,-1.0,1.0)\' " />' +
                '   <param name="shotspot3" value=" x257 y313 a282 b340 u\'ptviewer:startAutoPan(0,1.0,1.0)\' " />' +
                '   <param name="shotspot4" value=" x163 y313 a190 b340 u\'ptviewer:startAutoPan(0,0,0.99)\' " />' +
                '   <param name="shotspot5" value=" x195 y313 a225 b340 u\'ptviewer:startAutoPan(0,0,1.01)\' " />' +
                '	<param name="shotspot6" value=" x0 y0 a0 b0 u\'#\' t\'\' " />' +
                '</applet>';
        document.getElementById('panorama-container').innerHTML = $appletCode;
    };

    addPrefixIfTema = function(retVal) {
        return (oBaseAppSettings.IsTemaSite) ? retVal : '';
    }
});

function addOpenCityIntroText(){
	$("a#texts-text-link").click(function(){
		if($(this).attr("class") == "selected"){
		    $(this).removeClass("selected").text(oTUIStrings.strReadMoreCity);						
			$("div#texts-text-container").fadeOut("slow");			
			$("p#texts-text-ingress").removeClass("display-none").addClass("display-block");
		} else {
    $(this).addClass("selected").text(oTUIStrings.strReadLessCity);
			$("p#texts-text-ingress").removeClass("display-block").addClass("display-none");
			$("div#texts-text-container").fadeIn("slow");			
		}		
		return false;
	 });	
}
function addSelectSeasonEvents() {
	var oSelectSeason = document.getElementById('select-season');
	if (oSelectSeason) {
		var arrAllInputRadios = oSelectSeason.getElementsByTagName('input');
		for (var i=0; i < arrAllInputRadios.length; i++) {
		    arrAllInputRadios[i].onmousedown = function(oEvent) {
		        var oSelect = oSelectSeason.getElementsByTagName('select')[0];
		        if (oSelect) {
		            oSelect.selectedIndex = 0;
		        }
		        return true;
		    };		
		}
	}
}
function addEnokViewerEvents() {
    $("div.enok-hotel-level div.enok-booking-container dd.editable-false input:radio").attr("disabled", "true");
    $("input.city-level-book").click(
		function() {
		    _hotelid = $(this).attr("name").replace(/btnBookHotel-/gi, '');
		    $("ul.links li input[name='selected-booking-type-" + _hotelid + "']").each(
				function() {
				    if (this.checked) {
				        $("input#selected-booking-type").val($(this).val());
				        return false;
				    }
				}
			);
		    return true;
		}
	);
}
function showHotelInfoMap() {
	if ($("div.symbol-container").css("display") == "none") {
		$("div.symbol-container").fadeIn("slow");
		$("div#map-container").fadeIn("slow");
	} else {
		$("div.symbol-container").fadeOut("slow");
		$("div#map-container").fadeOut("slow");
	}
}
