/* Functions used on Accommodation page */
var carouselInitialized = false;
var imgCurrentIndex = 1;
var photoTotalItems = 0;
var numofphotos = 0;

// manages tabs on accommodation page
function Tab(tabName)
{
    CloseFullDescription();
    //Tabselection
    var chosen = document.getElementById("ctl00_cphBody_hChosen");
    var tab = document.getElementById("ctl00_cphBody_hTab");
    numofphotos = document.getElementById("ctl00_cphBody_hNumOfPhotos").value;
    //Check for anchor
    var anchor = "";
    var anchorPos = tabName.indexOf("#");
    if (anchorPos != -1)
    {
        anchor = tabName.substring(anchorPos+1);
        tabName = tabName.substring(0, anchorPos);
    }

    //Info items and titles
    var TabAccoInfo        = document.getElementById("TabAccoGeneral");
    var TabAccoInfoMenu    = document.getElementById("TabAccoGeneralMenu");
    var TabDestination     = document.getElementById("TabDestination");
    var TabDestinationMenu = document.getElementById("TabDestinationMenu");
    var TabPhotos          = document.getElementById("TabPhotos");
    var TabPhotosMenu      = document.getElementById("TabPhotosMenu"); 
    var TabVideos          = document.getElementById("TabVideos");
    var TabVideosMenu      = document.getElementById("TabVideosMenu"); 
    var TabMap          = document.getElementById("TabMap");
    var TabMapMenu      = document.getElementById("TabMapMenu"); 
    var TabWeather         = document.getElementById("TabWeather");
    var TabWeatherMenu     = document.getElementById("TabWeatherMenu");
    var TabOpinions        = document.getElementById("TabOpinions");
    var TabOpinionsMenu    = document.getElementById("TabOpinionsMenu");

    TabAccoInfo.className = "ovvNoShow";
    TabAccoInfoMenu.className = "";
    TabDestination.className = "ovvNoShow";
    TabDestinationMenu.className = "";
    TabPhotos.className = "ovvNoShow";
    TabPhotosMenu.className = "";
    TabVideos.className = "ovvNoShow";
    TabVideosMenu.className = "";
    TabMap.className = "ovvNoShow";
    TabMapMenu.className = "";
    TabWeather.className = "ovvNoShow";
    TabWeatherMenu.className = "";
    TabOpinions.className = "ovvNoShow";
    TabOpinionsMenu.className = "";

    var tabPos = 0;

    switch(tabName)
    {
        case "TabAccoGeneral":
            TabAccoInfo.className = "ovvShow";
            TabAccoInfoMenu.className = "active";
            tabPos = 0;
            break;
        case "TabDestination":
            TabDestination.className = "ovvShow";
            TabDestinationMenu.className = "active";
            tabPos = 1;
            break;
        case "TabPhotos":
            TabPhotos.className = "ovvShow";
            TabPhotosMenu.className = "active";
            initCarousel();
            tabPos = 2;
            break;
        case "TabVideos":
            TabVideos.className = "ovvShow";
            TabVideosMenu.className = "active";            
            initACarousel('video-carousel');
            tabPos = 3;
            break;
        case "TabMap":
            TabMap.className = "ovvShow";
            TabMapMenu.className = "active"; 
            loadMap();                       
            tabPos = 4;
            break;
        case "TabWeather":
            TabWeather.className = "ovvShow";
            TabWeatherMenu.className = "active";
            tabPos = 5;
            break;
        case "TabOpinions":
            TabOpinions.className = "ovvShow";
            TabOpinionsMenu.className = "active";
            tabPos = 6;
            break;
        default:
            return;
    }
    
    if (anchor != "")
        window.location.hash=anchor; 
	
    chosen.value = tabName;
    tab.value = tabPos;
}

function ToggleFullDescription()
{
    // get acco full text div
    // if hidden, then show; if showed, then hide
   
    var placeholder = document.getElementById("accoFullText");
    if (placeholder == null)
        return;
        
    if (placeholder.style.display == "none")
        placeholder.style.display = '';
    else
        placeholder.style.display = 'none';
}

function CloseFullDescription()
{
    // get acco full text div
    // if hidden, then show; if showed, then hide
   
    var placeholder = document.getElementById("accoFullText");
    if (placeholder == null)
        return;
           
   placeholder.style.display = 'none';
}

function AddAccoToSavedAccos(type, accoID) 
{
	var url = Master_BasePath + "AccoCookies.aspx?Action=Save&Type=" + type + "&AccoID="+accoID;
	var req = new Request();
	req.GetNoCache(url, AddAccoToSavedAccosResult);
}

function AddAccoToSavedAccosResult(result) 
{
	if (result.readyState != ReadyState.Complete) return;
	if (result.status == HttpStatus.OK && result.responseText != "") {
		if (result.responseText.indexOf("ERROR") != -1)
		{
		    alert("Error");
		}
		else
		{
		    //alert("Acco saved");
		    ResetSavedVisitedNumber()
		}
	}
}

var phoneNumber
function SelectPrice(accoID, price, date, dur, unit, board, touropID, depart, linkID, phone)
{
    SelectPriceWithAlt(accoID, price, date, dur, unit, board, touropID, depart, linkID, phone, 'no');
}
function SelectPriceWithAlt(accoID, price, date, dur, unit, board, touropID, depart, linkID, phone, alternative)
{
    document.getElementById('divAlternativesBottomMessage').style.display = 'none'; 

    phoneNumber = phone;
    
    //part for changing colors of selected link		
	var link                         = document.getElementById(linkID);
	if(link == null) return false;
	if (link.className == "niet-beschikbaar") return;
			
	var HiddenOldLinkStyleClassName  = document.getElementById("OldLinkStyleClassName");
    var HiddenOldLinkId              = document.getElementById("OldLinkId");               
    var ClickedPriceLink             = document.getElementById("ClickedPriceLink");	
    ClickedPriceLink.value           = linkID;
    
    if (HiddenOldLinkId.value != '')
    {
        var oldLink         = document.getElementById(HiddenOldLinkId.value);
        oldLink.className   = HiddenOldLinkStyleClassName.value;                
    }        
        
    HiddenOldLinkStyleClassName.value   = link.className;       
    HiddenOldLinkId.value               = linkID;         
    link.className                      = "selected";
    //end
    
    document.getElementById('ctl00_cphBody_UpdateProgressPriceMatrix').style.display = '';
    
    var url = Master_BasePath + "GetReceipt.aspx";
	url    += "?acco=" + accoID;
	url    += "&price=" + price;
	url    += "&date=" + date;
	url    += "&dur=" + dur;
	url    += "&unit=" + unit;
	url    += "&board=" + board;
	url    += "&tourop=" + touropID;
	url    += "&depart=" + depart;
	url    += "&linkid=" + linkID;
	url    += "&alternative=" + alternative;
	
	var req = new Request();

	req.GetNoCache(url, SelectPriceResult);
    //alert("accoID:" + accoID + " ,price:" + price + " date:" + date + " ,dur:" + dur + " ,unit:" + unit + " ,board:" + board + " ,touropID:" + touropID + " ,depart:" + depart + " ,linkID:" + linkID);
}

function SelectPriceResult(result) 
{
	if (result.readyState != ReadyState.Complete) return;
	if (result.status == HttpStatus.OK && result.responseText != "") 
	{
	
	    var lgh = result.responseText.indexOf('</head>');
		eval(result.responseText.substring(lgh + 7));
	
//		alert(result.responseText );
	}
	else
	{
		//alert("error\n" +  result.responseText );		
		alert("We kunnen op dit moment helaas geen verbinding krijgen met de touroperator om de beschikbaarheid van de door u gekozen reis te controleren. Onze excuses voor het ongemak. Chat of bel " + phoneNumber+ " met een van onze reisadviseurs om de beschikbaarheid te controleren.");		
		document.getElementById('ctl00_cphBody_UpdateProgressPriceMatrix').style.display = 'none';
	}
}

function Book(to, pc, tr, dt, du, dp, ut, bo)
{
	var url = Master_BasePath + "App_Frontend/Boeken/Start.aspx";
	url    += "?Action=book&fill=true";
	url    += "&to=" + to;
	url    += "&pc=" + pc;
	url    += "&tr=" + tr;
	url    += "&dt=" + dt;
	url    += "&du=" + du;
	url    += "&dp=" + dp;
	url    += "&ut=" + ut;
	url    += "&bo=" + bo;
	
	location.href = url;
}

function DisablePrice()
{
	var ClickedLinkHiddenField = document.getElementById("ClickedPriceLink");
	if (ClickedLinkHiddenField != null && ClickedLinkHiddenField.value != '')
	{
		var link = document.getElementById(ClickedLinkHiddenField.value);
		if(link != null)
		{		        
			link.className = "niet-beschikbaar";
			link.onClick = "return false";
            
			var HiddenOldLinkId = document.getElementById("OldLinkId");
			if(HiddenOldLinkId != null)
			{		        
				HiddenOldLinkId.value = "";
			} 
            
			return false;
		}           
    }       	
}

function CheckMailFields()
	{
		var isvalid = true;
		
		if( document.getElementById('accoMail_emailAdres').value == '' || document.getElementById('accoMail_emailAdres').value == 'E-mail adres' )
		{
			document.getElementById('val_accoMail_emailAdres').style.display = '';
			isvalid = false;
		}
		else
		{
			document.getElementById('val_accoMail_emailAdres').style.display = 'none';
		}
		
		if( !checkEmail('accoMail_emailAdres') )
		{
			document.getElementById('reg_accoMail_emailAdres').style.display = '';
			isvalid = false;
		}
		else
		{
			document.getElementById('reg_accoMail_emailAdres').style.display = 'none';
		}

		if( document.getElementById('accoMail_userName').value == '' || document.getElementById('accoMail_userName').value == 'Uw naam' )
		{
			document.getElementById('val_accoMail_userName').style.display = '';
			isvalid = false;
		}
		else
		{
			document.getElementById('val_accoMail_userName').style.display = 'none';
		}
		
		if( document.getElementById('accoMail_userEmailAdres').value == '' || document.getElementById('accoMail_userEmailAdres').value == 'Uw e-mail adres' )
		{
			document.getElementById('val_accoMail_userEmailAdres').style.display = '';
			isvalid = false;
		}
		else
		{
			document.getElementById('val_accoMail_userEmailAdres').style.display = 'none';
		}

		if( !checkEmail('accoMail_userEmailAdres') )
		{
			document.getElementById('reg_accoMail_userEmailAdres').style.display = '';
			isvalid = false;
		}
		else
		{
			document.getElementById('reg_accoMail_userEmailAdres').style.display = 'none';
		}

		return isvalid;
		//return false;
	}
	
	function checkEmail(field) 
	{
		var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		if (!filter.test(document.getElementById(field).value)) 
		{
			return false;
		}
		return true;
	}
	
	function prevPhoto(carousel)
{
    showPhoto((parseInt(imgCurrentIndex) - 1), carousel);    
}

function nextPhoto(carousel)
{
    showPhoto((parseInt(imgCurrentIndex) + 1), carousel);    
}

function showPhoto(index, carousel) 
{        
    if(index >= 1 && index <= photoTotalItems)
    {
        if(index+1 >= (photoTotalItems-3))
        {            
            carousel.next();
        } 
        else if(index+1 <= imgCurrentIndex && index+1 == (photoTotalItems-4))
        {            
            carousel.prev();
        } 
        else 
        {            
            carousel.scroll(index);
        }

        imgCurrentIndex = index;
        showBigimg();
    }
    //setNextPrevButtonState();
}

function setNextPrevButtonState() 
{
    if(imgCurrentIndex > 1) 
    {
        $('.jcarousel-prev-horizontal').removeClass('jcarousel-prev-disabled');
        $('.jcarousel-prev-horizontal').removeClass('jcarousel-prev-disabled-horizontal');
    } 
    else 
    {
        $('.jcarousel-prev-horizontal').addClass('jcarousel-prev-disabled');
        $('.jcarousel-prev-horizontal').addClass('jcarousel-prev-disabled-horizontal');
    }
    if(imgCurrentIndex >= photoTotalItems) 
    {
        $('.jcarousel-next-horizontal').addClass('jcarousel-next-disabled');
        $('.jcarousel-next-horizontal').addClass('jcarousel-next-disabled-horizontal');
    } 
    else 
    {
        $('.jcarousel-next-horizontal').removeClass('jcarousel-next-disabled');
        $('.jcarousel-next-horizontal').removeClass('jcarousel-next-disabled-horizontal');
    }
}

function printpage(){
    var placeholder = document.getElementById("accoFullText");

    placeholder.style.display = '';

    document.getElementById('imgHeaderPrint').style.display = 'block';

    var html='<div id="additionalDestinationTab">';
    html += window.frames['idTopFrame'].document.body.innerHTML;
    html += '</div>';
    document.getElementById('TabDestination').innerHTML += html;
    window.print();
    var olddiv = document.getElementById("additionalDestinationTab");
    document.getElementById('TabDestination').removeChild(olddiv);
    
    document.getElementById('imgHeaderPrint').style.display = 'none';

}
function printIFrames(){
var html='<html><body>';
for (var i=0;i<window.frames.length;i++){
if (window.frames[i].name!="printFrame") html += window.frames[i].document.body.innerHTML;
}
html += '</body></html>';
var printFrame = window.frames["printFrame"];
printFrame.document.write(html);
printFrame.document.close();
printFrame.focus();
printFrame.print();
}

var currentCarousel = 'none';
function initCarousel()
{   
    initACarousel('mycarousel');    
}
function initACarousel(aCarousel)
{
    var isDifferentCarousel = (currentCarousel != aCarousel);

    var oldCarousel = currentCarousel;
    currentCarousel = aCarousel;

    if (carouselInitialized == false || isDifferentCarousel == true)
    {
        if(carouselInitialized == true)
        {
           if( theCarousel = $('#'+oldCarousel).data('jcarousel') ) {                
                theCarousel.reset();
            }            
        }
    
        jQuery('#' + currentCarousel).jcarousel({
            scroll: 1,
            initCallback: mycarousel_initCallback
            });
        carouselInitialized = true;    
    }
}

function mycarousel_initCallback(carousel, state) 
{

    photoTotalItems = $('#'+currentCarousel+' li').length;
    showBigimg();
    
    
    jQuery('#'+currentCarousel+'-next').bind('click', function() {
        nextPhoto(carousel);
        return false;
    });

    jQuery('#'+currentCarousel+'-prev').bind('click', function() {
        prevPhoto(carousel);
        return false;
    });
    
    jQuery('.jcarousel-next-horizontal').bind('click', function() {
        nextPhoto(carousel);
        return false;
    });

    jQuery('.jcarousel-prev-horizontal').bind('click', function() {
        prevPhoto(carousel);
        return false;
    });

    jQuery('.jcarousel-item-horizontal').bind('click', function() {
        var imgIdx = jQuery(this).attr("jcarouselindex");
        showPhoto(parseInt(imgIdx), carousel);
        return false;
    });
    if(numofphotos < 5){
        jQuery('.jcarousel-next-horizontal').hide();
        jQuery('.jcarousel-prev-horizontal').hide();
    }
    if(numofphotos < 2){
        jQuery('#'+currentCarousel+'-next').hide();
        jQuery('#'+currentCarousel+'-prev').hide();
    }
}

function showBigimg()
{
    
    $('.jcarousel-container.jcarousel-container-horizontal li').removeClass('selected');
    $('li.jcarousel-item.jcarousel-item-horizontal:eq('+(imgCurrentIndex-1)+')').addClass("selected");
        
    var newSrc = $("img", 'li.jcarousel-item.jcarousel-item-horizontal:eq('+(imgCurrentIndex-1)+')').attr("bigSrc");
    var newAlt = $("img", 'li.jcarousel-item.jcarousel-item-horizontal:eq('+(imgCurrentIndex-1)+')').attr("alt");
    
    $('#'+currentCarousel+'-imgbig').attr("src", newSrc);
    $('#'+currentCarousel+'-imgbig').attr("alt", newAlt);
    $('#'+currentCarousel+'-imgIndex').text(imgCurrentIndex);
    
    //var subText = newAlt.substring(0,newAlt.indexOf(altAccoName));
    //$('#imgBigAlt').text(subText);
}

function initAccosCarousel()
{
    jQuery('#smallAccos-carousel').jcarousel({
        vertical: true,
        scroll: 3,
        initCallback: smallAccos_initCallback
    });
}
    
function smallAccos_initCallback(carousel, state)
{
    var numofitems = document.getElementById("ctl00_cphBody_AccoListShort_hlnumofitems").value;
    jQuery('.jcarousel-item-vertical').bind('mouseover', function() {
        jQuery(this).addClass("accoItemSelected");
        return false;
    });
    
    jQuery('.jcarousel-item-vertical').bind('mouseout', function() {
        jQuery(this).removeClass("accoItemSelected");
        return false;
    });
    
    jQuery('.jcarousel-item-vertical').bind('click', function() {
        window.location.href = jQuery(this).find("img").attr("href");
    });
     if(numofitems < 5){
        jQuery('.jcarousel-next-vertical').hide();
        jQuery('.jcarousel-prev-vertical').hide();
     }else{
        jQuery('.jcarousel-next-vertical').show();
        jQuery('.jcarousel-prev-vertical').show();
     }
}

function changePhotoImage(ind){
    imgCurrentIndex = ind;
    
    Tab('TabPhotos');
    showBigimg();
}

function changeVideo(ind){
    imgCurrentIndex = ind;
    
    Tab('TabVideos');
    showBigimg();
}

var didLoadMap = false;
function loadMap()
{
    if(!didLoadMap)
    {      
    
        var myLatlng = new google.maps.LatLng(accoMapLatitude,accoMapLongitude);
        var myOptions =
                   {
                       zoom: 6,
                       // Zoom level for initial map. Valild vaues range form 0 to 20

                       center: myLatlng,
                       // Centers the map on the coordinates contained within the varible myLatlng

            mapTypeControl: true,     
            mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU},  
            zoomControl: true,     
            zoomControlOptions: {style: google.maps.ZoomControlStyle.LARGE }, 

                       mapTypeId: google.maps.MapTypeId.HYBRID,
                       // sets the type of map to be displayed
                       // HYBRID - displays a transparent layer of major streets on satellite images
                       // ROADMAP - displays a normal street map 
                       // SATELLITE - displays satellite images
                       // TERRAIN - displays maps with physical features such as terrain

                       streetViewControl: true
                       // True displays the pegman to enable street views, 
                       // false or no streetViewControl hides the pegman from view
                   }

        map = new google.maps.Map(document.getElementById("acco_map_canvas"), myOptions);
    

        var marker = CreateMarker(myLatlng);        
                     
        didLoadMap = true;
    }
}

function CreateMarker(point) {
    var marker = new google.maps.Marker({ position: point, map: map });    

    return marker
}
