// flash map tracking for site catalyst and Google
var mapLocation,mapType = new String();
var pageTracker;

//track map type and name
function trackMapType(location, name)
{
	mapLocation = name;
	mapType = location;
	// only do this when the window has fully loaded
	$(window).load(function()
	{
		// Google page tracking call
		pageTracker._trackEvent(mapLocation + ' Map','Page Open',mapType+ ' Map Type',1);

	});
}

//track map start, helperSprite
function trackMapStart()
{
	// Google page tracking call
	pageTracker._trackEvent(mapLocation + ' Map','Start',mapType+ ' Map Type',1);


}

//track clicks or map navigator on map , ChildMinder && ConcertinaItem
function trackMapClicks(linkLocation , linkName , url)
{
	url = url || false;
	//link location =  right hand side, details dropdown , map ,
	//map type = country , region , lodge
	//location = grumeti , south africa , cape town
	//link name = [lodge name]_(regions_map|explore)

	// Google page tracking call
	pageTracker._trackEvent(mapLocation + ' Map',linkLocation,linkName+' Map click',1);



	if(url != false)
	{
		//redirect to url after tracking
		location.href = url;
	}
}

//track clicks or map navigator on map, ZoomSlider, MiniMap
function trackMapUsage(featureName, value)
{
	//map type = country , region , lodge
	//location = grumeti , south africa , cape town
	//navigation feature value = yes/no
	//navigation feature =  scroller , map nav

	// Google page tracking call
	pageTracker._trackEvent(mapLocation + ' Map','Map Usage',featureName,1);

}


var sendToFriendWindow,galleryWindow, subscribeWindow;

// reset the captcha number
function resetSecurityNumber(fieldId,imageId) 
{
	 var date = new Date();
	 if (fieldId != null && fieldId != '') $('#' + fieldId).val('');
	 if (imageId != null && imageId != '') $('#' + imageId).attr('src','/includes/captcha/CaptchaSecurityImages.php?width=120&height=40&noCache=' + date.getSeconds());
	 $('#' + fieldId).focus();
	 return false;
}

// show a dd/li dropdown
function showDropdown(field)
{
	$(field).slideDown();
}
// hide a dd/li dropdown
function hideDropdown(field)
{
	$(field).slideUp();
}
// load send to friend lightbox
function sendToFriend(url,title)
{
	url = url || location.href;
	title = title || '';
	sendToFriendWindow.load('/lightbox/lightbox_send_to_friend.php',{url:url , title: title});
	return false;
}
// load send to friend lightbox
function showGallery(params)
{
	params = params || null;
	galleryWindow.load('/lightbox/lightbox_gallery.php',params);
	return false;
}
function loadVideoGallery(params)
{
	params = params || null;
	//clear interval when changing gallery
	galleryWindow.load('/lightbox/lightbox_vidgallery.php',params);
	return false;
}

function _alert(error)
{
	var alertwindow = $.alertWindow({timeout: 2000});

	if(typeof(error) != 'object'){
		alertwindow.alert(error);
	}else{
	 if(error.retryAttempt-1!= error.retries)
	 {
			alertwindow.alert(error.message+'<p>Attempting retry '+error.retryAttempt+'/'+error.retries+' in ('+error.timeout+' / 1000 ) +  seconds </p>');
		}else{
			alertwindow.alert(error.message);
		}
	}
}

function convertHeaders(scope)
{	
	scope = scope || document;
	

	$(".freetxt h2:visible",scope).each(function()
								  {
									 
									var header = $(this);	
									header.addClass('hide');				
									var headerText = header.html();	
									
									if(! (header.next().hasClass('img_txt') && header.next().find('img').length != 0) && !header.next().is('img') )
									{
									
										headerText = headerText.replace(/<.*?>/ig,''); // remove any tags 
										var getHeader = $(this).loader({insertType: 'after'});	
										getHeader.load('/includes/get_header.php', 
													   {header: headerText,tagname: this.tagName.toLowerCase()},
													   function()
													   {										  
															header.html(headerText);
														});
									}
								  });	
									
									
}
function disableContextmenu()
{
	
		$('img').bind('contextmenu',function(e){
										
										if(e.stopPropagation)
										{
											e.stopPropagation();
										}
										e.preventDefault();
																			
									});	
}
function pngfix()
{
	if($.browser.msie && $.browser.version < '7.0')
	{		
		$('img[@src$=.png],.pricetag .inner, .png_frame').css('behavior','url(/images/iepngfix.php)');
	}
}
var lodgeDropdown;
$(document).ready(function()
				{										
					convertHeaders();
					$('.freetxt a').each(function()
								{		
									var httpReg =  new RegExp('http://'+location.hostname);	
									if( this.href.search(httpReg) == -1 )
									{
										$(this).attr('target','_blank');
										$(this).addClass('external');
									}								
								});
					disableContextmenu();
					//create send to a friend lightbox
					sendToFriendWindow = $('#sendToFriendWindow').window({
																	width: 600,
																	height: 400,
																	zindex:4,
																	titlebar: false,
																	softshadow: false,
																	popup: true,
																	autoload: false,
																	align:'center',
																	valign:'center',
																	scroll:false
																});
					//create a gallery lightbox
					galleryWindow = $('#galleryWindow').window({
																width: 835,
																height: 561,
																zindex:5,
																titlebar: false,
																softshadow: false,
																popup: true,
																autoload: false,
																align:'center',
																valign:'center',
																scroll:false
															});
					
					//setup newsletter window
					subscribeWindow = $('#subscribeWindow').window({
																	width: 600,
																	height: 500,
																	zindex:6,
																	titlebar: false,
																	softshadow: false,
																	popup: true,
																	autoload: false,
																	align:'center',
																	valign:'center'
																});


					//setup lodge dropdown
					$('.lodge_dropdown a').click(function(){
														if($('.lodge_dropdown_list').is(':visible'))
														{
															hideDropdown('.lodge_dropdown_list');
														}else
														{
															showDropdown('.lodge_dropdown_list');		
														}
														return false;
													});
					
					$('.lodge_dropdown a, .lodge_dropdown_list li').mouseout(function()
											{
												
												lodgeDropdown = setTimeout("hideDropdown('.lodge_dropdown_list')",1000);
											});
					$('.lodge_dropdown a, .lodge_dropdown_list li').mouseover(function()
											  {
												 clearTimeout(lodgeDropdown); 								
												 lodgeDropdown = null;								
											  });
					$('#lodge-list').fadeIn('slow');
					
					//setup share link dropdown
					$('#shareLink').toggle(function()
											{
												var offset = $(this).offset();
												offset.left -= $('#shareIt').width() - $(this).outerWidth()+5;
												offset.top += $(this).outerHeight() +5;
												$('#shareIt').css(offset);	
												showDropdown('#shareIt');
												
											},function()
											{
											
												hideDropdown('#shareIt');
												
											});
					
					//fires close of dropdown when a link is clicked on
					$('.share-it a').mousedown(function()
											{
												$('#shareLink').trigger('click');
											});
					
					//text size accessibility
					$('#smallerText').click(function(event)
								{
									if(document.getElementById('container').style.zoom != undefined)
									{						
										var zoomLevel = (document.getElementById('container').style.zoom !='')? parseInt(document.getElementById('container').style.zoom) : 100;
										
										zoomLevel-= 10;
										if(zoomLevel > 0)
										{
											document.getElementById('container').style.zoom = zoomLevel+'%';
										}
									}else
									{
										var fontSize = parseInt($('p').css('font-size'));
										$('p').css('font-size',fontSize-1);	
									}
									return false;
								});
					$('#biggerText').click(function(event)
								{
									if(document.getElementById('container').style.zoom != undefined)
									{						
										var zoomLevel = (document.getElementById('container').style.zoom !='')? parseInt(document.getElementById('container').style.zoom) : 100;
										
										zoomLevel+= 10;
										document.getElementById('container').style.zoom = zoomLevel+'%';						
									}else
									{
										var fontSize = parseInt($('p').css('font-size'));
										$('p').css('font-size',fontSize+1);
									}					
									return false;
								});						
					pngfix();
});


// load subscribe lightbox

function subscribe()
{
	subscribeWindow.load('/lightbox/lightbox_subscribe.php');
	return false;
}