$(document).ready(function() {

	$('#explore_travel_rollover').addClass('selected');

	/* This bit takes the last pipe off the featured destination links. This makes it somewhat "error proof" if the user forgets to remove the pipe */
	var last_li = $("#featured_destination_information ul#featured_destination_links li:last");
	var first_a = new Array();
	first_a['text'] = last_li.children('a').text();
	first_a['title'] = last_li.children('a').attr("title");
	first_a['href'] = last_li.children('a').attr("href");
	$(last_li).replaceWith('<li><a title=\"' + first_a['title'] + '\" href=\"' + first_a['href'] + '\" >' + first_a['text'] + '</a></li>');

	addPagerToDiv('hotelListPager');
	addPagerToDiv('restaurantListPager');
	addPagerToDiv('thingsToDoListPager');
	addPagerToDiv('eventListPager');
	addPagerToDiv('nightlifeListPager');
	addPagerToDiv('shoppingListPager');

	$('.top_snippets .featured_destination_links li').not($('.top_snippets .featured_destination_links li:last')).append("|");
	$('.top_snippets .featured_destination_links li a:first').css('padding', '0 5px 0 0');
	$('#bottom_snippet .featured_destination_links li').not($('#bottom_snippet .featured_destination_links li:last')).append("|");
	$('#bottom_snippet .featured_destination_links li a:first').css('padding', '0 5px 0 0');

	var li_count = 0;
	function display_list_of_favorites(id) {
		$(id).each(function() {
			li_count++;
			if (li_count >= 10 && jQuery.browser.msie && jQuery.browser.version == 6) {
				$(this).wrapInner('<div class="position_li_over_nine"></div>');
				$(this).prepend('<div class="number">' + li_count + '</div>');
			} else {
				$(this).wrapInner('<div class="position_li"></div>');
				$(this).prepend('<div class="number">' + li_count + '</div>');
			}
		});
	}
	display_list_of_favorites($('#list_of_favorites ol li'));
	display_list_of_favorites($('#favorite_theme_list ol li'));

	putListInColumns('area_locations', 'southCities', 3);
	putListInColumns('area_locations', 'southeastCities', 3);
	putListInColumns('area_locations', 'southwestCities', 3);
	putListInColumns('area_locations', 'northeastCities', 3);
	putListInColumns('area_locations', 'northwestCities', 3);
	putListInColumns('area_locations', 'midwestCities', 2);
	putListInColumns('area_locations', 'exploreTravelList', 3);

	//Init each widget in this array
	jQuery.each(pluckWidgets, function() {
		this.init();
	});
	setupSearchBox();
});

function addPagerToDiv(divName) {
	$('#' + divName).pager('div', {
		navId: 'nav3',
		linkWrap: '<span style="margin:0 4px 0 0;"></span>'
	});
}

function setupSearchBox() {
	var defVal = 'Search Travel Guide';
	$('#q').val(defVal);
	$('#q').focus(function() {
		$(this).css('color', 'black');
		if(this.value == defVal){
			this.value = '';
		}
		if(this.value != defVal){
			this.select();
		}
	});
	$('#q').blur(function() {
		if($.trim(this.value) == '' || $.trim(this.value) == defVal){
			$(this).css('color', '#b7b7b7');
			this.value = (defVal ? defVal : '');
		}
	});
}

$(window).load(function() {

	if ($('.deal-summary-image').css('background')) {
		apply_polaroid_frame($('.top_snippets .deal-summary-image img'), 'square');
	} else if ($('.deal-summary-image-destinations.css').css('background')) {
		apply_polaroid_frame($('.top_snippets .deal-summary-image-destinations img'), 'square');
	} else {
		apply_polaroid_frame($('.top_snippets img'), 'square');
	}
	//	apply_polaroid_frame($('div.image_summary_wrapper div#image img'), 'square');
	apply_polaroid_frame($('#explore_activities .top_snippets img'), 'square');
	apply_polaroid_frame($('div#theme_details div#image img'), 'square');
	apply_polaroid_frame($('div#activity_details div#image img'), 'square');


});

function putListInColumns(classname, divname, numberOfColumns) {
	$('#' + divname + ' ul.' + classname).each(function() {
		var setOfLis = $(this).children();
		var numberOfLis = setOfLis.length;
		var divisor = (Math.floor(numberOfLis / numberOfColumns));
		if (numberOfLis % numberOfColumns != 0) {
			divisor += 1;
		}
		$(this).empty();
		for (var i = 0; i < numberOfColumns; i++) {
			var newElement = jQuery("<div class='multicolumn-" + numberOfColumns + "'></div>");
			for (var k = 0; k < divisor; k++) {
				if (i * divisor + k >= numberOfLis) {
					break;
				}
				newElement.append(setOfLis[i * divisor + k]);
			}
			$(this).append(newElement);
		}
	});
}
var pluckWidgets = new Array();
function addWidgetToInitList(widget) {
	pluckWidgets[pluckWidgets.length] = widget;
}


function apply_polaroid_frame(image_reference, polaroid_type) {
	if (polaroid_type == 'square') {
		$(image_reference).each(function() {
			var image_height = $(this).attr('height');
			if ($(this).attr('src') == "") {
				$(this).parent().remove();
				$(this).parent().parent().css('height', 'auto');
			}
			else if (image_height <= 30) {
				$(this).attr('src', "");
				$(this).parent().remove();
				$(this).parent().parent().css('height', 'auto');
			}
			else {
				if ($.browser.msie && $.browser.version == 6) {
					$(this).after('<img class="mask" id="image" width="261px" src="/images/polaroid_square.gif" alt="Polaroid Frame" />');
					$(this).css({ float: "left", padding: "10px 0 0 10px" });
					$(this).parent().css({ width: "255px", height: "200px", paddingTop: "5px" });
					$('.mask').css({ height:"210px", width:"261px", float:"left", marginTop: "-188px", marginLeft:"0px", marginBottom:"10px", position: "relative", bottom: "8px" });
					$('.mask img').attr('src', '/images/polaroid_square.gif');
					$('.mask').parent().css({width:'260px', float:"left"});
				} else
					if ($.browser.msie && $.browser.version > 6) {
						$(this).after('<img class="mask" id="image" width="261px" src="/images/polaroid_square.png" alt="Polaroid Frame" />');
						$(this).css({ float: "left", padding: "10px 0 0 10px" });
						$(this).parent().css({ width: "255px", height: "200px" });
						$('.mask').css({ height:"210px", width:"261px", float:"left", marginTop: "-188px", marginLeft:"0px", marginBottom:"10px", position: "relative", bottom: "8px" });
						$('.mask').parent().css({width:'260px', float:"left"});
					} else {
						$(this).after('<img class="mask" id="image" width="261px" src="/images/polaroid_square.png" alt="Polaroid Frame" />');
						$(this).css({ float:"left", padding:"8px 0 0 0" });
						$('.mask').css({ float: "left", marginTop: "-" + (image_height + 10) + "px", marginLeft: "-3px"});
						$('.mask').parent().css({width:'260px', float:"left"});
					}
			}

		});
	}
	else if (polaroid_type == 'large') {
		$(image_reference).each(function() {
			var image_height = $(this).attr('height');
			if ($(this).attr('src') == "") {
				$(this).parent().remove();
				$(this).parent().parent().css('height', 'auto');
			}
			else if (image_height <= 30) {
				$(this).attr('src', "");
				apply_polaroid_frame(image_reference, 'large');
			}
			else {
				if ($.browser.msie && $.browser.version == 6) {
					$(image_reference).after('<img class="mask" src="/images/polaroid_large.gif" alt="Polaroid Frame" />');
					$(image_reference).css({ float: "left", padding: "10px 0 0 10px" });
					$('.mask').css({ height:"166px", width:"542px", float:"left", marginTop:"-128px", marginLeft:"-1px", marginBottom:"10px" });
				} else
					if ($.browser.msie && $.browser.version > 6) {
						$(image_reference).after('<img class="mask" src="/images/polaroid_large.png" alt="Polaroid Frame" />');
						$(image_reference).css({ float: "left", padding: "10px 0 0 10px" });
						$('.mask').css({ height:"166px", width:"540px", float:"left", marginTop:"-128px", marginLeft:"-1px", marginBottom:"10px" });
					}
					else {
						$(image_reference).after('<img class="mask" src="/images/polaroid_large.png" alt="Polaroid Frame" />');
						$(image_reference).css({ float:"left", padding:"2px 0 0 5px" });
						$('.mask').css({ float: "left", marginTop: "-" + (image_height + 2) + "px", marginLeft: "-3px" });
					}
			}
		});
	}
}
