
// the id of the HTML element containing the normal information
var normalDisplayId = 'column2-content';

// the id of the HTML element in which the hotel, region, etc information should be displayed
var contentPaneId = 'detail-info';

var BACK_TO_RESULTS = "results";
var BACK_TO_DETAILS = "details";
var BACK_TO_OVERVIEW = "overview";
var BACK_TO_RESERVATION = "registration";
var BACK_TO_RESERVATION_SUCCESS = "registration_success";
var BACK_TO_TRAVELDIARY = "travel_log";

function back(backKey) {
	var url1 = contextURL + '?p_p_id=' + ibePortletId;
	var url2 = '&p_p_state=maximized&p_p_mode=view&' + ibeNameSpace + 'struts_action=';
	
	if (backKey == BACK_TO_RESULTS){
		var searchForm = dwr.util.byId('searchForm');
		if (doSearch(searchForm)) {
			searchForm.submit();
		}
	} else if (backKey == BACK_TO_DETAILS){
		location.replace(url1 + '&p_p_lifecycle=0' + url2 + '/NeckermannIbe/details');
	} else if (backKey == BACK_TO_OVERVIEW) {
		location.replace(url1 + '&p_p_lifecycle=0' + url2 + '/NeckermannIbe/overview');
	} else if (backKey == BACK_TO_TRAVELDIARY) {
		location.replace(contextURL + '/../../utinaplo');
	}
	
	clearCmsContent();
}

function showHotelInfo(hotelInfoUrl) {
	if (backLinkKey && backLinkKey != '' && backLinkKey != 'null') {
		hotelInfoUrl += '?backLinkKey=' + backLinkKey;
	}
	
	if (friendlyUrl && friendlyUrl != '') {
		if (hotelInfoUrl.indexOf('?backLinkKey=') > -1) {
			hotelInfoUrl += '&';
		} else {
			hotelInfoUrl += '?';
		}
		hotelInfoUrl += 'friendlyUrl=' + escape(friendlyUrl);
	}
	
	location.replace(hotelInfoUrl);
}

function showCmsContent(content) {
	if(content != "") {
		var cmsContent = document.getElementById("cmsContent");
		cmsContent.innerHTML = content;
	}
}

function clearCmsContent() {
	var cmsContent = document.getElementById("cmsContent");
	if(cmsContent != null) {
		cmsContent.innerHTML = "";
	}
}

function showRoutePlanner(selectedProduct) {
	selProduct = selectedProduct;
	prepareLoading();
	PageController.showRoutePlanner(selectedProduct, backLink,
		{ callback: showRouterPlannerCallback, errorHandler: displayError }
	);
}

function showRouterPlannerCallback(page) {
	if (page != "") {
		showInfoPage(page);
		
		jQuery('#carousel').jcarousel({
			scroll: 1,
			visible: 4
		});
		jQuery(function() {
			jQuery('#nec-hotel-info .gallery a').lightBox({txtImage:'',
				txtOf: '/', 
				imageBtnClose: imagePath + '/lightbox/lightbox-btn-close.gif',
				imageLoading: imagePath + '/lightbox/lightbox-ico-loading.gif',
				imageBtnPrev: imagePath + '/lightbox/lightbox-btn-prev.gif',
				imageBtnNext: imagePath + '/lightbox/lightbox-btn-next.gif'
			}); // Select all links in object with gallery ID
		});
	}
}

function swapRegionInfo(first, second) {
	jQuery('#' + first + '-season-button').hide();
	jQuery('#' + first + '-region-info').show();
	jQuery('#' + second + '-season-button').show();
	jQuery('#' + second + '-region-info').hide();
}

function showRegionInfoForRegion(selectedRegion, backLinkKey) {
	var url = contextURL + '?p_p_id=' + ibePortletId + '&p_p_lifecycle=0&p_p_state=maximized&p_p_mode=view&' + ibeNameSpace + 'struts_action=/NeckermannIbe/regioninfo';
	var parameters = '&regionId=' + selectedRegion + '&backLinkKey=' + backLinkKey;
	location.replace(url + parameters);
}

function showRegionInfo(selectedProduct, backLinkKey) {
	var url = contextURL + '?p_p_id=' + ibePortletId + '&p_p_lifecycle=0&p_p_state=maximized&p_p_mode=view&' + ibeNameSpace + 'struts_action=/NeckermannIbe/regioninfo';
	var parameters = '&productId=' + selectedProduct + '&backLinkKey=' + backLinkKey;
	location.replace(url + parameters);
}

function showWeatherInfo(selectedProduct, backLinkKey) {
	var url = contextURL + '?p_p_id=' + ibePortletId + '&p_p_lifecycle=0&p_p_state=maximized&p_p_mode=view&' + ibeNameSpace + 'struts_action=/NeckermannIbe/weatherinfo';
	var parameters = '&productId=' + selectedProduct + '&backLinkKey=' + backLinkKey;
	location.replace(url + parameters);
}

function showWeatherInfoForRegion(selectedRegion, backLinkKey) {
	var url = contextURL + '?p_p_id=' + ibePortletId + '&p_p_lifecycle=0&p_p_state=maximized&p_p_mode=view&' + ibeNameSpace + 'struts_action=/NeckermannIbe/weatherinfo';
	var parameters = '&regionId=' + selectedRegion + '&backLinkKey=' + backLinkKey;
	location.replace(url + parameters);
	point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
	marker = new GMarker(point);
	map.addOverlay(marker);
	marker.openInfoWindowHtml(place.address + '<br>' +
		'<b>Country code:</b> ' + place.AddressDetails.Country.CountryNameCode);
}

function showProductRating(selectedProduct, backLinkKey) {
	var url = contextURL + '?p_p_id=' + ibePortletId + '&p_p_lifecycle=0&p_p_state=maximized&p_p_mode=view&' + ibeNameSpace + 'struts_action=/NeckermannIbe/ratinginfo';
	var parameters = '&productId=' + selectedProduct + '&backLinkKey=' + backLinkKey;
	location.replace(url + parameters);
}

var selProduct = -1;
function onSelectInfoType(info) {
	if (info == 'HOTEL_INFO') {
		showHotelInfo(selProduct);
	} else if (info == 'REGION_INFO') {
		showRegionInfo(selProduct);
	} else if (info == 'WEATHER_INFO') {
		showWeatherInfo(selProduct);
	} else if (info == 'PRODUCT_RATING') {
		showProductRating(selProduct);
	}
}

var selRegion = -1;
function onSelectRegionInfoType(info) {
	if (info == 'REGION_INFO') {
		showRegionInfoForRegion(selRegion);
	} else if (info == 'WEATHER_INFO') {
		showWeatherInfoForRegion(selRegion);
	}
}

function showTab(selector) {
	if (jQuery(selector).is(':hidden')) {
		jQuery(selector).slideDown('slow');
	} else {
		jQuery(selector).slideUp('slow');
	}
	return false;
}

/**
 * URL encode / decode
 * http://www.webtoolkit.info/
 */
var Url = {

    // public method for url encoding
    encode : function (string) {
        return escape(this._utf8_encode(string));
    },

    // public method for url decoding
    decode : function (string) {
        return this._utf8_decode(unescape(string));
    },

    // private method for UTF-8 encoding
    _utf8_encode : function (string) {
        string = string.replace(/\r\n/g,"\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
    },

    // private method for UTF-8 decoding
    _utf8_decode : function (utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;

        while ( i < utftext.length ) {

            c = utftext.charCodeAt(i);

            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i+1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i+1);
                c3 = utftext.charCodeAt(i+2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }

        }

        return string;
    }

};


var googleMap = null;
var destinationGeocode = null;
var destinationId = null;
var directionsLayer;

function submitRoutePrint() {
	window.open('/NeckermannIbe/html/portlet/regionmap/printRoute.jsp?from='
			+ Url.encode('' + jQuery('nec_googlemap_route_form_source').value)
			+ '&to=' + Url.encode('' + destinationGeocode)
			+ '&hotelId=' + Url.encode('' + destinationId),
		'printRoute1',
		'width=1000,height=600,left=200,top=0,toolbar=yes,menubar=yes,scrollbars=1,resizable=1');
}

// TODO Ezt a stringet kell lokalizálni, ha szükség lesz rá.
var routeNotPossible = "Sajnáljuk, a keresett útvonalat a Google Maps nem tudja megtervezni!";
function submitRouteSearch() {
	var source = '' + jQuery('nec_googlemap_route_form_source').value;
	var queryStr = "from: " + source + " (startpont) to: " + destinationGeocode + " (célpont)";

	if (directionsLayer == null) {
		directionsLayer = new GDirections(googleMap, jQuery('nec_googlemap_canvas_routeplan'));
	}

	GEvent.addListener(directionsLayer, 'error', function() {
		alert('routeNotPossible');
	});
	
	directionsLayer.load(queryStr);
	return false;
}

