
jQuery(window).error(function(msg, url, line)
{
	// jQuery.post("js_error_log.php", { msg: msg, url: url, line: line });
	var msg_str = msg;
	if (typeof(msg) == "object")
	{
		msg_str = "";
		for (var x in msg)
		{
			if ((typeof(msg[x]) != "object") && (typeof(msg[x]) != "function"))
				msg_str += x + " = " + msg[x] + "\n";
		}
	}
});

var RDefaultPage = QWebPage.extend('RDefaultPage', {

	mapCM : null,
	selectedPanel : null,
	languagePrefix : "en",

	load : function()
	{
		/*
		var t_bh = jQuery.trim(this.base_href);
		var t_hf = jQuery.trim(document.location.href);
		if (t_hf.indexOf("#") >= 0)
			t_hf = t_hf.substr(0, t_hf.indexOf("#"));
		
		if (t_hf.length > t_bh.length)
		{
			var t_diff = jQuery.trim(t_hf.substr(t_bh.length));
			if ((t_diff != "en") && (t_diff != "/en") && (t_diff != "en/") && (t_diff != "/en/"))
			{
				// alert(t_diff);
				var new_href = t_bh;
				if ((t_diff.length > 1) && (t_diff.substr(0, 2) == "en") && 
					((t_diff.length == 2) || (t_diff.substr(2, 1) == "/"))) 
				{
					new_href += "en";
					t_diff = t_diff.substr(2);
					if (t_diff.substr(0, 1) == "/")
						t_diff = t_diff.substr(1);
				}
				new_href += "#" + t_diff;
				document.location.href = new_href;
			}
		}
		*/
		
		this.mapCM = this.getQWebControlById('mapCM');
		
		var ref_this = this;
		this.mapCM.bind("ev_load", function (e) {ref_this.onMapLoaded(e);})

		this.mapCM.showLargeMapControl = true;
		this.mapCM.showScaleControl = true;
		this.mapCM.showOverviewMapControl = true;
		
		this.mapCM._texts.expand = this.getLang("expand");
		this.mapCM._texts.close = this.getLang("close");

		var cm_icon = new CM.Icon(CM.DEFAULT_ICON);

		cm_icon.iconSize = new CM.Size(20, 30);
		cm_icon.image = this.base_href + "code/res/i/marker2.png";
		cm_icon.shadow = this.base_href + "code/res/i/marker-shadow2.png";

		CM.DEFAULT_ICON = cm_icon;
		// try { this.mapCM.highlightSearchArea("country:Romania", true); } catch{}
		
		this.setSelectedPanel(this.getJsParam("panel"));
		
		this.setBookmarkClass();

		this.jQuery(".__ax_submit").live("click",
			function ()
			{
				var params = ref_this.getFormParameters( this.form );
				this.form.reset();
				
				jQuery.ajax({
					type: "POST",
					url: "_contact_submit.php?lang=" + QLanguages.language,
					data: params,
					success: function(msg)
					{
						alert( msg );
					}});
				
				return false;
			});
	},
	
	onHashChanged : function (hash)
	{
		if (window["pageTracker"])
		{
			// alert("pageTracker: " + hash);
			pageTracker._trackPageview(hash);
		}
	},
	
	onJsParamsUpdated : function ()
	{
		this.setSelectedPanel(this.getJsParam("panel"));
	},
	
	setSelectedPanel : function(panel)
	{
		if (this.selectedPanel != panel)
		{
			jQuery(".menu a").removeClass("selected");
			
			if (panel == "RVisitContent")
				jQuery(".menu #menu_visit").addClass("selected");
			else if (panel == "RStayContent")
				jQuery(".menu #menu_stay").addClass("selected");
			else if (panel == "REatContent")
				jQuery(".menu #menu_eat").addClass("selected");
			else if (panel == "RFunContent")
				jQuery(".menu #menu_fun").addClass("selected");
			else if (panel == "RPremadeTripsContent")
				jQuery(".menu #menu_premade_trips").addClass("selected");
			else if (panel == "RUsefulContent")
				jQuery(".menu #menu_useful").addClass("selected");
		}
	},
	
	getDefaultMapBounds : function ()
	{
		return new CM.LatLngBounds( new CM.LatLng(43.62244, 20.26102), new CM.LatLng(48.26389, 29.6725) );
	},
	
	getDefaultMapCenter : function ()
	{
		return new CM.LatLng(45.943165, 24.96676);
	},

	onMapLoaded : function (e)
	{
		// alert("onMapLoaded");
		this.mapCM.mapZoomToBounds(this.getDefaultMapBounds());
		this.mapCM.mapSetSavedBounds(this.getDefaultMapBounds());
	},

	/*
	loadHomeContent : function()
	{
		this.getQWebControlById('centerPanel').triggerEvent("changeSelectedControl", 
			{"control_name" : "centerSubPanel", "control_class" : "RHomePanel"});
	},

	loadVisitContent : function()
	{
		this.getQWebControlById('centerPanel').triggerEvent("changeSelectedControl", 
			{"control_name" : "centerSubPanel", "control_class" : "RVisitContent"});
	},

	loadStayContent : function()
	{
		this.getQWebControlById('centerPanel').triggerEvent("changeSelectedControl", 
			{"control_name" : "centerSubPanel", "control_class" : "RStayContent"});
	},

	loadEatContent : function()
	{
		this.getQWebControlById('centerPanel').triggerEvent("changeSelectedControl", 
			{"control_name" : "centerSubPanel", "control_class" : "REatContent"});
	},

	loadFunContent : function()
	{
		this.getQWebControlById('centerPanel').triggerEvent("changeSelectedControl", 
			{"control_name" : "centerSubPanel", "control_class" : "RFunContent"});
	},

	loadPremadeTripsContent : function()
	{
		this.getQWebControlById('centerPanel').triggerEvent("changeSelectedControl", 
			{"control_name" : "centerSubPanel", "control_class" : "RPremadeTripsContent"});
	},

	loadUsefulContent : function()
	{
		this.getQWebControlById('centerPanel').triggerEvent("changeSelectedControl", 
			{"control_name" : "centerSubPanel", "control_class" : "RUsefulContent"});
	},

	*/

	setBookmarkClass : function ()
	{
		// add a "rel" attrib if Opera 7+
		if(window.opera) {
			if (jQuery("a.jqbookmark").attr("rel") != ""){ // don't overwrite the rel attrib if already set
				jQuery("a.jqbookmark").attr("rel","sidebar");
			}
		}
		
		jQuery("a.jqbookmark").click(
			
			function(event)
			{
				event.preventDefault(); // prevent the anchor tag from sending the user off to the link
				var url = this.href;
				var title = this.title;
			
				if (window.sidebar) // Mozilla Firefox Bookmark
					window.sidebar.addPanel(title, url,"");
				else if( window.external ) // IE Favorite
					window.external.AddFavorite( url, title);
				else if(window.opera)  // Opera 7+
					return false; // do nothing - the rel="sidebar" should do the trick
			});
	},
	
	bookNow : function (acc_id, params)
	{
		// alert();
		var inps = jQuery(".book_room_select");
		// var inps = tab.find("tr td"); // jQuery("select", tab);
		
		// alert(acc_id + " x=x " + inps.length);
		
		// var inps = tab.find("select");
		
		// alert(acc_id + " = " + inps.length);

		var acc_id = params["acc_id"];
		var check_in_date = params["check_in_date"];
		var check_in_days = params["check_in_days"];

		var booking_list = [];

		for (var i = 0; i < inps.length; i++)
		{
			var e = jQuery(inps[i]);
			var v = e.val();
			try
			{
				var nr = parseInt(v);
				if (nr > 0)
				{
					var params = {};
					 
					booking_list[booking_list.length] = {"room_id" : e.attr("room_id"), "rooms_nr" : nr};
				}
			}
			catch (ex)
			{
				// 
			}
		}
		
		if (booking_list.length > 0)
		{
			var p = {"acc_id" : acc_id, "check_in_date" : check_in_date, 
						"check_in_days" : check_in_days, "list" : booking_list};
			
			// this.getQWebControlById("centerPanel").triggerEvents("changeSelectedControl", {"control_name" : "centerSubPanel", "control_class" : "RHomePanel"});
			this.triggerEvents([
				{"id" : "centerPanel", "type" : "changeSelectedControl", params: {"control_name" : "centerSubPanel", "control_class" : "RBookNow"}},
				{"id" : "centerPanel-centerSubPanel", "type" : "bookNow", 
						params: p}
				]);
		}
		else
		{
			jQuery.fancybox(QLanguages.Get("select_at_least_one_room"));
		}
	},

	ShowGalleriffic : function(id)
	{
	    var gallery = jQuery('#' + id).galleriffic({
	        delay:                     3000, // in milliseconds
	        numThumbs:                 20, // The number of thumbnails to show page
	        preloadAhead:              40, // Set to -1 to preload all images
	        enableTopPager:            false,
	        enableBottomPager:         true,
	        maxPagesToShow:            7,  // The maximum number of pages to display in either the top or bottom pager

	        imageContainerSel:         '#slideshow', // The CSS selector for the element within which the main slideshow image should be rendered
	        controlsContainerSel:      '#controls', // The CSS selector for the element within which the slideshow controls should be rendered
	        loadingContainerSel:       '#loading', // The CSS selector for the element within which should be shown when an image is loading

	        renderSSControls:          true, // Specifies whether the slideshow's Play and Pause links should be rendered
	        renderNavControls:         true, // Specifies whether the slideshow's Next and Previous links should be rendered
	        playLinkText:              'Play',
	        pauseLinkText:             'Pause',
	        prevLinkText:              'Previous',
	        nextLinkText:              'Next',
	        nextPageLinkText:          'Next &rsaquo;',
	        prevPageLinkText:          '&lsaquo; Prev',
	        enableHistory:             false, // Specifies whether the url's hash and the browser's history cache should update when the current slideshow image changes
	        enableKeyboardNavigation:  true, // Specifies whether keyboard navigation is enabled
	        autoStart:                 false, // Specifies whether the slideshow should be playing or paused when the page first loads
	        syncTransitions:           false, // Specifies whether the out and in transitions occur simultaneously or distinctly
	        defaultTransitionDuration: 1000, // If using the default transitions, specifies the duration of the transitions
	        onSlideChange:             undefined, // accepts a delegate like such: function(prevIndex, nextIndex) { ... }
	        onTransitionOut:           undefined, // accepts a delegate like such: function(slide, caption, isSync, callback) { ... }
	        onTransitionIn:            undefined, // accepts a delegate like such: function(slide, caption, isSync) { ... }
	        onPageTransitionOut:       undefined, // accepts a delegate like such: function(callback) { ... }
	        onPageTransitionIn:        undefined, // accepts a delegate like such: function() { ... }
	        onImageAdded:              undefined, // accepts a delegate like such: function(imageData, $li) { ... }
	        onImageRemoved:            undefined  // accepts a delegate like such: function(imageData, $li) { ... }
	    });
	}
});
