function params(qp) { try{r=unescape(location.search.match(new RegExp(qp+"=+([^&]*)"))[1]);}catch(e){r='';} return r; }		
$(document).ready(function()
{
	
	
	Date.firstDayOfWeek = 0;
	Date.format = 'mm/dd/yyyy';
	try	{
		if (typeof(window.top.nyedate) != "undefined") {
			$("#chk_in").val("12/31/2011");
			$("#hotel-check-out").val("01/02/2012");	
			$("INPUT[name=src_aid]").val("newyearseve");
			$("INPUT[name=subAffiliateId]").val("newyearseve");
		}
	} catch(err)  {  }
	
	$('input.date-pick').datePicker().bind(
			'dateSelected',
			function(e, selectedDate, $td)
			{
				if (e.target.id == "chk_in") {
											
					newdate = selectedDate.addDays(2).asString('mm/dd/yyyy');
					console.log($("#hotel-check-out").val(newdate));
				}
												
			}
		);
	
	/*-------------------------------------------    
  		Featured Philly
	-------------------------------------------*/
	$('div.coda').each(function()
	{
		// scope the coda
		var $coda = $(this);
		
		// only print controls to page if they're needed
		if($('ul.featured > li').length > 5){
			$coda.append('<div class="controls"><a class="previous" href="#">Previous</a><a class="next" href="#">Next</a></div>');
			$('a.previous').addClass('off');
		}		
		
		// hide extras
		$coda.find('ul.featured > li:gt(4)').hide();
		
		// store page
		$coda.attr('page', '1');
		
		// make the previous work
		$coda.find('a.previous').click(function()
		{
			var page = parseInt($coda.attr('page'))-1;
			if (page == 0) { return false; }
			
			$coda.attr('page', page);
			$coda.find('ul.featured > li').fadeIn();
			$coda.find('ul.featured > li:gt('+((page*5)-1)+')').hide();
			
			$coda.find('ul.featured > li:eq('+((page*5)-5)+')').addClass('first');
			$coda.find('ul.featured > li:lt('+((page*5)-5)+')').hide();
			
			if (page == 1) { $('a.previous').addClass('off'); }
			$('a.next').removeClass('off');
			
			return false;
		});
		
		// make the next work
		$coda.find('a.next').click(function()
		{
			var page = parseInt($coda.attr('page'))+1;
			
			var num = $coda.find('ul.featured > li').size();
			var spillover = num%5;
			var maxPage = Math.floor(num/5)+(spillover!=0?1:0);
			
			if (page > maxPage) { return false; }
			
			$coda.attr('page', page);
			$coda.find('ul.featured > li').fadeIn();
			$coda.find('ul.featured > li:gt('+((page*5)-1)+')').hide();
			
			$coda.find('ul.featured > li:eq('+((page*5)-5)+')').addClass('first');
			$coda.find('ul.featured > li:lt('+((page*5)-5)+')').hide();
			
			if (page == maxPage) { $('a.next').addClass('off'); }
			$('a.previous').removeClass('off');
			
			return false;
		});
	});
	
	/*-------------------------------------------    
  		Grey Initial Values
	-------------------------------------------*/
	var filled = $('.filled');
		
	if(filled.length > 0){
		for(var i = 0; i < filled.length; i++){
				
			
			$(filled[i]).addClass("empty");
			filled[i].initialValue = filled[i].value;
			
			filled[i].onclick = filled[i].onfocus = function(){
				$(this).removeClass("empty");
				$(this).addClass("filled");
				if(this.value == this.initialValue){
					this.value= "";
				}
			}
			
			filled[i].onblur = function(){
				if(this.value == this.initialValue || this.value == ""){
					$(this).addClass("empty");
					$(this).removeClass("filled");
					this.value = this.initialValue;
				}else{
					$(this).addClass("empty");
					$(this).removeClass("filled");
				}
			}
		}
	}
	
	else
	{
		return; 
	}
	
	
	/*-------------------------------------------    
		Open external links in a new window
	-------------------------------------------*/
	$("a[rel='external'],a.external").click(function() 
	{ 
		window.open($(this).attr('href'));
		return false; 
	});
	
	
	/*-------------------------------------------    
		Main Nav Drop Downs
	-------------------------------------------*/
	var img1 = document.createElement('img');
	img1.src = "/m/screen/bg-main-nav-drop.png";	
	
	var img2 = document.createElement('img');
	img2.src = "/m/screen/bg-main-nav-drop-alt.png";	
	
	var img3 = document.createElement('img');
	img3.src = "/m/widgets/circle-loader.gif";
	
	$(['things-to-do', 'plan-your-trip', 'philly-now']).each(function()
	{
		var tab = this;
		
		$('.nav-'+tab).parents('li:eq(0)').hover(
			
			// each time we roll over a nav item something should happen
			function ()
			{
				// hide open tabs
				$('#main-nav > li.on').mouseout();
				
				// set the on class for the parent li
				 $(this).addClass('on');
				
				// store the anchor we clicked on and the tab (if it's already in the source)
				var $anchor = $(this).find('a.nav-'+tab);
				var $tabContainer = $('.tab-'+tab);
				
				// if the tab is already in the source (because its been clicked already) just show it
				if ($tabContainer.size())
				{
					$tabContainer.show();
				}
				
				// if it's not in the source go fetch it and add it in
				else
				{
					var $dropDownContainer = $('<div class="tab-container tab-'+tab+'"><img src="/m/widgets/circle-loader.gif" style="position:absolute; top:25%; left:46%;" /></div>');
					$anchor.after($dropDownContainer);
					$.get('/enhanced-includes/tab-'+tab+'/', {}, function(data)
					{
						$dropDownContainer.html(data);
						
						// make it work for screen readers, bad code, bad place, but it works...
						$('#main-nav > li:last-child .actions a:last').blur(function() { $('#main-nav > li.on').mouseout(); });
					});
				}
			},
			
			// similarly, something should happen on mouseout
			function ()
			{
				if ($.browser.msie) {
					$('.tab-'+tab).hide();
				}
				else
				{
					$('.tab-'+tab).fadeOut(200);
				}
				$(this).removeClass('on');
			}
		);
		
		// make it work for screen readers
		$('.nav-'+tab).focus(function () { $(this).parents('li:eq(0)').mouseover(); });
		$('#county-nav li:last-child a').focus(function() { $('#main-nav > li.on').mouseout(); });
		
		// Don't allow href follow 
		$('.nav-'+tab).click(function()
		{
			return false;
		});

	});
	
	/*-------------------------------------------    
		Carousel/Slideshow Helpers
	-------------------------------------------*/	
	
	function getPrevious($data)
	{
		if ($data.find('li.on').prev().size())
		{
			return $data.find('li.on').prev().find('a');
		}
		
		if ($data.find('li:last-child').hasClass('direction'))
		{
			return $data.find('li:last-child').prev().find('a');
		}
		
		return $data.find('li:last-child').find('a');
	}
	
	function getNext($data)
	{
		if ($data.find('li.on').next().size() && !$data.find('li.on').next().hasClass('direction'))
		{
			return $data.find('li.on').next().find('a');
		}

		return $data.find('li:first-child').find('a');
	}
	
	function setView($data, numInView)
	{
		if (numInView == undefined)
		{
			numInView = 10;
		}
		
		var on = $data.find('.on').prevAll('li').size();
		var min = (Math.floor(on/numInView)*numInView);
		var max = (Math.floor(on/numInView)*numInView)+(numInView-1);

		$data.find('li').hide();
		$data.find('li:eq('+min+')').css('display', '');
		$data.find('li:gt('+min+')').css('display', '');
		$data.find('li:gt('+max+')').css('display', 'none');
		$data.find('.direction').css('display', '');
	}
	
	
	/*-------------------------------------------    
		Homepage Tabset
	-------------------------------------------*/
	$('div#main').wrapInner('<div id="main-js-wrap"></div>')
	$('.roll li:eq(0)').addClass('on');
	$('.roll li a').each(function(i)
	{
		var $anchor = $(this);
		$anchor.click(function ()
		{
			$anchor.parents('ul').find('.on').removeClass('on');
			$anchor.parents('li').addClass('on');
			
			var $main = $('#main');
			var $main_js_wrap = $('div#main-js-wrap');
			
			$('div#main').css({height: $main_js_wrap.height(), position:'relative'});
			$('div#main').append('<img id="ajax-loader" src="/m/widgets/circle-loader.gif" style="position:absolute; top:190px; left:49%;" />');
			$('div#main-js-wrap').fadeOut();
			$('div#main-js-wrap').load('/homepage-tabs/'+i, {}, function()
			{
				$('img#ajax-loader').remove();
				$('div#main').css({height: 'auto'});
				$('div#main-js-wrap').fadeIn();
			});
			
			return false;
		});
	})
	
	/*-------------------------------------------    
		Social Tabset
	-------------------------------------------*/
	var $social = $('.social');
	var $tabs = $('<ul class="tabs"></ul>');
	$social.prepend($tabs);
	$social.find('h3').each(function(i)
	{
		// hide the h3, since it's being duplicated into a list before the tabs anyway
		var $h3 = $(this);
		
		// create the tab link (hardcode a return false to save a jQuery selector and since this
		// will only be seen with JS on anyway)
		var $tab = $('<li><a href="#" onclick="return false;" class="'+$h3.attr('class')+'">'+$h3.html()+'</a></li>');
		$tabs.append($tab);
		
		// now that we have a link, hide the h3
		$h3.addClass('move');
		
		// add a class to locate our tab-content later
		var $tabContent = $h3.next();
		$tabContent.addClass('social-tab-container');
		
		// on click hide all the tabs and open only the one we clicked on
		$tab.click(function()
		{
			$('.tabs li').removeClass('on');
			$social.find('div.social-tab-container').hide();
			$tab.toggleClass('on');
			$tabContent.toggle();
		});
		
		// leave only the first tab selected and open
		if (i == 0) $tab.addClass('on').addClass('first');
		if (i > 0) $tabContent.toggle();
	});
	
	/*-------------------------------------------    
		Accordian
	-------------------------------------------*/
	$('ul.accordian h3').wrapInner('<a href="#"></a>');
	$('ul.accordian h3 a').click(function()
	{
		var $anchor = $(this);
		var $li = $anchor.parents('li');
		
		$li.siblings().addClass('expand');
		$li.removeClass('expand');
		
		return false;
	});
	
	/*-------------------------------------------    
		Roster
	-------------------------------------------*/
	$('ul.roster:not(.accordian) h3').wrapInner('<a href="#"></a>');
	$('ul.roster:not(.accordian) h3 a').click(function()
	{
		var $anchor = $(this);
		var $li = $anchor.parents('li:eq(0)');
		
		if (!$li.hasClass('expand'))
		{
			$li.addClass('expand');
			$li.removeClass('collapse');
			$li.find('ul:eq(0)').hide();
		}
		
		else
		{
			$li.removeClass('expand');
			$li.addClass('collapse');
			$li.find('ul:eq(0)').show();
		}
		
		return false;
	});
	$('ul.roster:not(.accordian) li.expand > ul').hide();
	
	$('ul.roster:not(.accordian) > li:first-child').removeClass('expand');
	$('ul.roster:not(.accordian) > li:first-child').addClass('collapse');
	$('ul.roster:not(.accordian) > li:first-child ul:eq(0)').show();
	
	/*-------------------------------------------    
		Slideshows
	-------------------------------------------*/
	$('.slideshow, .slideshow-alt').each(function()
	{
		var $slideshow = $(this);
		
		$slideshow.find('.slideshow-main').each(function()
		{
			$slideshowImage = $(this);
						
			var template = $slideshow.attr('data-type')=='landing'?'widget-slideshow-controls':'widget-slideshow-controls-unit';
			var viewSize = $slideshow.attr('data-type')=='landing'?6:4;
			var imgSize = $slideshow.attr('data-type')=='landing'?530:587;

			
			$.get('/enhanced-includes/'+template+'/'+$slideshow.attr('data-entry')+'/', {}, function(data)
			{
				if (!data.replace(/\s/g, '')) { return false; }
				
				var $data = $(data);
				$data.find('li a').click(function()
				{
					var $anchor = $(this);
					
					var currentHeight = $('.slideshow-main').height();					
					
					$('.slideshow-main').fadeOut('', function() {				
						
						var slideHeight = $('.slideshow-main').height();
					
						if ($anchor.attr('data-video'))
						{
							$slideshow.find('.slideshow-main').replaceWith('<div class="slideshow-main" style="display:none;">'+$anchor.attr('data-video')+'</div>');
											
							if(currentHeight == slideHeight){
								$('.slideshow-main').fadeIn();
							}					
							else
							{		
								$('div.slide-wrap').animate({
									height: slideHeight
								}, 600, function() {
									$('.slideshow-main').fadeIn();
								});
							}
						}
						
						else
						{
							$slideshow.find('.slideshow-main').replaceWith('<img src="'+$anchor.attr('data-image')+'" alt="'+$anchor.attr('data-alt')+'" width="'+imgSize+'" class="slideshow-main" style="display:none;" />');
											
							var imgInterval = setInterval(function(){
								if($('.slideshow-main')[ 0 ].complete){ 
									clearInterval(imgInterval);
									slideHeight = $('.slideshow-main').height();
									
									if(currentHeight == slideHeight){
										$('.slideshow-main').fadeIn();
									}					
									else
									{		
										$('div.slide-wrap').animate({
											height: slideHeight
										}, 600, function() {
											$('.slideshow-main').fadeIn();
										});
									}
								}
							}, 1000);
						}		
											
					});
					
					$slideshow.find('h3.zeta').html('<a href="'+$anchor.attr('href')+'">'+$anchor.attr('data-title')+'</a>');
					$slideshow.find('p.caption').html($anchor.attr('data-description')+' '+($anchor.attr('data-credit')?'<strong>Credit: '+$anchor.attr('data-credit')+'</strong>':''));
					
					$slideshow.find('.on').removeClass('on');
					$anchor.parents('li').addClass('on');
					
					setView($data, viewSize);
					
					return false;
				});
				
				setView($data, viewSize);
				
				$data.find('.previous').click(function()
				{
					getPrevious($data).click();
					return false;
				});
				
				$data.find('.next').click(function()
				{
					getNext($data).click();
					return false;
				});
				
				$slideshowImage.after($data);
				
				$slideshowImage.wrap('<div class="slide-wrap" />');
			});
		});
	});
	
	/*-------------------------------------------    
		Search Filters
	-------------------------------------------*/
	// hide everything after the nth result (0-based)
	var minToShow = 3;
	
	// grab each ul
	$('ul.summary-list').each(function()
	{
		var $ul = $(this);
		
		// grab each li
		var $lis = $ul.find('li');
		
		// if needing hiding hide 'em
		if ($lis.size() > minToShow+1)
		{
			// generate the more link
			var $more = $('<li class="more"><a href="#">Show More</a></li>');
			
			// hid the elements
			$ul.find('li:gt('+minToShow+')').hide();
			
			// now that things are hidden, append the more link
			$ul.append($more);
			
			// add a click ot the more to hide/show elements
			$ul.find('.more').click(function()
			{
				if ($ul.find('li:gt('+minToShow+'):eq(0)').css('display') == 'none')
				{
					$ul.find('li:gt('+minToShow+')').show();
					$more.html('<a href="#">Show Less</a>');
				}
				
				else
				{
					$ul.find('li:gt('+minToShow+'):not(.more)').hide();
					$more.html('<a href="#">Show More</a>');
				}
				return false;
			});
		}
	});
	
	/*-------------------------------------------    
		Travelocity
	-------------------------------------------*/
	$('.book-online').each(function()
	{
		// store widget in scope
		var $widget = $(this);
		
		
		// find h3's to convert to tabs
		var $h3s = $widget.find('h3');
		if ($h3s.length == 0) return;
		
		// hide h3s
		$h3s.hide();
		
		// generate a ul of tabs
		var $ul = $('<ul class="travelocity-nav"></ul>');
		
		// loop through each h3 and add it as a tab
		$h3s.each(function(i)
		{
			var $h3 = $(this);
			var $li = $('<li class="'+$h3.attr('class')+'"><a href="#">'+$h3.text()+'</a></li>');
			$ul.append($li);
			
			// update the form class name
			$h3.next().attr('class', $h3.attr('class')+'-form');
			
			// add the click to the new tab
			$li.find('a').click(function()
			{
				var $anchor = $(this);
				
				$widget.find('.on').removeClass('on');
				$anchor.addClass('on');
				$widget.find('form').hide();
				$widget.find('.'+$h3.attr('class')+'-form').show();
				return false;
			});
			
			// mark the first one as open
			if (i == 0)
			{
				$li.find('a').addClass('on');
			}
		});
		
		// add the tabs
		$($h3s.get(0)).before($ul);
	});
	
	
	/*-------------------------------------------    
		Share Popup
	-------------------------------------------*/
	$('.share a').click(function()
	{
		var $anchor = $(this);
		
		if ($anchor.next().hasClass('share-div'))
		{
			if ($anchor.next().css('display') == 'none')
			{
				$anchor.next().show();
			}
			
			else
			{
				$anchor.next().hide();
			}
		}
		
		else
		{
			$.get($anchor.attr('href'), {}, function(data)
			{
				var $data = $($(data).find('.share-div').get(0));
				var $socialblock = $data.find('.share-method.socially');
				var $emailblock = $data.find('.share-method.email');

				$emailblock.hide();
				$data.find('.share-email, .share-back').click(function()
				{
					if ($emailblock.css('display') == 'none')
					{
						$socialblock.hide();
						$emailblock.show();
					}

					else
					{
						$socialblock.show();
						$emailblock.hide();
					}
					
					return false;
				});

				$anchor.after($data);
			});
		}
		
		return false;
	});
	
	$('.ir-uwishunu-mini').parent().prevAll('h3.epsilon').remove();
});

function splitDate(input, output)
{
	$('#'+input).each(function()
	{
		var date = $(this).val();
		var pieces = date.split('/');
		var outputs = ['mm', 'dd', 'yy'];
		
		for (var i=0; len=pieces.length,i<len; i++)
		{
			$('#'+output+'_'+outputs[i]).val(pieces[i]);
		}
	});
}

// Console Logging with Firebug
if(typeof(console) != "object") {var console = new Object(); console.log = function() {} };
var msie = navigator.userAgent.toLowerCase().indexOf('msie') > 0;
$D = function (str) { 
	if (!msie) console.log(str + '\n');
}
	

// Widget Tracking
function trackWidget(src,type) {	// SRC is the widget SRC, and Type is 'hotel','car','activities'
	var googleTrack = '/widget/' + src + "/" + type;
	$D("TRACK: " + googleTrack);
	trackitem(googleTrack);
}

/* Begin JQuery link tracking */
// allows for link tracking to include a defined prefix for any given page
if (typeof(trackingPrefix) != "string") {trackingPrefix = "";}	else {	trackingPrefix = "/" + trackingPrefix;	}	

// find all A and AREA links to external sites, and enable onclick tacking
//var allLinks = $("A[@href^=http://]:not([@href^=http://www.visitphilly.com]),AREA[@href^=http://]:not([@href^=http://www.visitphilly.com])");
var allLinks = $("A[href^='http://']:not([href*='.visitphilly.com']):not([href$='.pdf']):not([href$='.mp3']),AREA[href^='http://']:not([href*='.visitphilly.com']):not([href$='.pdf']):not([href$='.mp3'])");
allLinks.each(function(i){
		var thisHref = $(this).attr("href");
		thisHref = thisHref.replace("http://", "");			// remove http
		if (thisHref.charAt(thisHref.length - 1) == "/")	// trim ending slash
			thisHref = thisHref.substr(0,thisHref.length - 1);
//		console.log(thisHref);
		$(this).click(function() { 
			trackitem('/outbound/' + thisHref)							   
		})
});

var allPdf = $("a[href$='.pdf'],a[href$='.mp3']");
allPdf.each(											 
	function(i){
		var thisHref = $(this).attr("href");
		thisHref = thisHref.replace("http://c0526532.cdn.cloudfiles.rackspacecloud.com/", "");	
		thisHref = thisHref.replace("http://www.visitphilly.com/", "");			
//		console.log(thisHref);
		$(this).click(function() { 			
			trackitem('/downloads/' + thisHref)
		})

});	

function trackitem(wtrack) {
	$D("OUTGOING: " + wtrack);	
	_gaq.push(['_trackPageview',wtrack]);	
	_gaq.push(['_trackEvent', 'outgoing', 'links' , wtrack]);  
}

/* End JQuery link tracking */	


/* begin popup survey */
function showsurvey() {
	return;	// disabled survey
	if(typeof(Shadowbox) == "object") {
		var options = {
			skipSetup: true,
			displayNav:         false,
			handleUnsupported:  'remove',
			keysClose:          ['c', 27], // c or esc
			autoplayMovies:     false
		};
		
		Shadowbox.onReady = function() {
			setTimeout( function() { opensurvey() },1000);
		}
		Shadowbox.init(options);
	}						 	
}
function opensurvey() {
	return;	// disabled survey
	console.log("open");
	Shadowbox.open({
		player:     'iframe',
		content:    '/survey/',
		height:     235,
		width:      560
		});
}

function closesurvey() {
	Shadowbox.close();
}
/* end popup survey */

$(document).ready(function()
{
	// fix homepage offsets
	$('#content  .book-online  .date-pick').dpSetOffset(0, -175);
	
	// aaaForm Fixes
	$('#aaaForm #zip').focus(function() { $(this).val('');  });  
		  
	if (typeof(initsurvey)  !=  'undefined') { if (initsurvey) showsurvey();	}	
});




