$(function() {
	$(".weatherOpen").click(function() {
		$(".header .weather .contracted").hide();
		$(".header .weather .expanded").show();
		$(".header .weather").css({ width: 389 });
		$(".header .weather").animate({ width: 516 });
	});
	$(".weatherClose").click(function() {
		$(".header .weather").animate({ width: 389 }, function() {
			$(".header .weather .expanded").hide();
			$(".header .weather").css({ width: 189 });
			$(".header .weather .contracted").show();
		});
	});
	
	svlv.buildDateInputs($(".dateInput"));
	svlv.buildAjaxOverlays();
	svlv.buildDateInputsTopNav();
	svlv.getBackgrounds(svlv.setRandomBackground);
});

var svlv = {
	settings : {
		controllerURL : "/includes/cfcs/controller/controller.cfm",
		loading : $("<div class='loading'><img src='/includes/images/shell/ajax-loader.gif'/><span>Loading...</span></div>")
	},
	getBackgrounds : function(callback) {
		$.get(svlv.settings.controllerURL, { object : "backgrounds", action : "getBackgrounds", returntype : "json" }, function(data) {
			if (data.status == "success") {
				svlv.settings.backgrounds = data.data;	
				if (typeof(callback) == "function") {
					callback();	
				}
			}
		}, "json");
	},
	setRandomBackground : function() {
		var rand = Math.floor(Math.random()*svlv.settings.backgrounds.length);
		$(".primaryBackground").empty();
		$(".primaryBackground").append("<img src='" + svlv.settings.backgrounds[rand].url + "'>");
	},
	backgroundCutter : function() {
		if ($(".backgroundCutter").length > 0) {
			var height = $(".backgroundCutter").offset().top;
			$(".primaryBackground").height(height);
			$(".secondaryBackground").css({ top : height });
		}
	},
	toggleLanguageDropdown : function() {
		$(".languageSelect .languages").toggle();
		$(".languageSelect > a").toggleClass("active");
		$(".languageSelect").toggleClass("shadow10");
	},
	buildLvDropDowns : function() {
		var self = this;
		
		$(".lvDropDown").each(function() {
			self.buildLvDropDown($(this));
		});
	},
	buildLvDropDown : function(node) {
		var self = this;
		
		if (node.attr("multiple") == true) {
			var myclass = "lvDropDownMultiple";	
		} else {
			var myclass = "lvDropDownSingle";	
		}
		
		var final = $("<div class='lvDropDownFinal " + myclass + "'></div>");
		
		if (node.attr("multiple") == true) {
			final.append("<div class='lvDropDownCurrent'><span class='lvCount'></span>" + node.find("option").eq(0).text() + "</div>");
			var lvOptions = $("<div class='lvOptions'></div>");
			node.find("option").not(":first-child").each(function() {
				if ($(this).is(":selected")) {
					var checked = "checked";	
				} else {
					var checked = "";	
				}
				
				lvOptions.append("<div class='lvOption'><input type='checkbox' name='" + node.attr('name') + "' value='" + $(this).val() + "' " + checked + "><span>" + $(this).text() + "</span></div>");
			});
			
			final.append(lvOptions);
			
			self.updateMultipleCount(lvOptions.find(".lvOption:first-child"));
			
			final.find(".lvOption input[type=checkbox]").click(function() {
				self.updateMultipleCount($(this));
			});
		} else {
			final.append("<input type='hidden' name='" + node.attr('name') + "' value='" + node.val() + "'>");
			final.append("<div class='lvDropDownCurrent'>" + node.find("option:selected").text() + "</div>");
			var lvOptions = $("<div class='lvOptions'></div>");
			node.find("option").each(function() {
				lvOptions.append("<div class='lvOption' data-value='" + $(this).val() + "'>" + $(this).text() + "</div>");
			});
			final.append(lvOptions);
			
			
			final.find(".lvOption").click(function() {
				var parent = $(this).closest(".lvDropDownFinal");
				parent.find("input").val($(this).attr("data-value"));
				parent.find(".lvDropDownCurrent").html($(this).html());
				parent.find(".lvOptions").hide();
			});
		}
		
		final.find(".lvDropDownCurrent").click(function() {
			$(this).next().toggle();
			if ($(this).next().is(":visible")) {
				$(this).next().focus();
			}
		});
		
		if (node.attr("multiple") == true) {
			var padding = 35;	
		} else {
			var padding = 17;	
		}
		
		node.after(final); 
		node.appendTo("body"); /*** This is a hack to properly get width from selects hidden at the time this function is called (used in topNav) ***/
		final.width(node.width() + padding);
		final.find(".lvOptions").width(node.width() + padding - 2); /** 2px difference for borders **/
		node.remove();
	},
	updateMultipleCount : function(node) {
		var count = node.closest(".lvOptions").find("input[type=checkbox]:checked").length;
		if (count == 0) {
			var countHTML = "";	
		} else {
			var countHTML = "(" + count + ")";	
		}
		
		node.closest(".lvDropDownFinal").find(".lvCount").html(countHTML);
	},
	buildDateInputs : function(nodes) {
		nodes.dateinput({
			format: 'mm/dd/yyyy',
			trigger : true
		});
	},
	buildDateInputsTopNav : function() {
		$(".topNavDateInput").dateinput({
			format : 'mm/dd/yyyy',
			trigger : true,
			onShow : function(event, date) {
				var node = this.getInput();
				var calendar = this.getCalendar();
				node.next().after(calendar);
				calendar.css({ left: 0, top: 20});
			},
			onHide : function(event, date) {
				this.getCalendar().appendTo("body");
			}
		});
	},
	buildAjaxOverlays : function() {
		$("body").append("<div id='svlvOverlay'></div>");
		this.buildAjaxOverlay($(".svlvOverlayLink"));
	},
	buildAjaxOverlay : function(nodes) {
		nodes.overlay({
			mask : "#444",
			onBeforeLoad : function() {
				var overlay = $("#svlvOverlay");
				if(overlay.children(".content").length > 0) {
					overlay.children(".content").remove();	
				}
				
				overlay.css("width", this.getTrigger().attr('data-width') + "px");
				
				var content = $("<div class='content'></div>");
				
				content.load(this.getTrigger().attr("href"));
				overlay.append(content);
			},
			closeOnClick : false
		});
	}
};

svlv.listings = {
	settings : {
		scrollTimer : 0
	},
	compareScroll : function() {
		clearTimeout(svlv.listings.settings.scrollTimer);
		svlv.listings.settings.scrollTimer = setTimeout("svlv.listings.updateCompareVisibility()", 200);
	},
	initStickyCompare : function() {
		$(".compareBar").show();
		this.settings.compareStartTop = $(".compareBar").offset().top;
		$(".compareBar").hide();
	},
	compareCallback : function() {
		this.updateCompareCounts();
		this.updateCompareLink();
		this.updateCompareLinks();
		this.updateCompareVisibility();
		this.updateCompareEvent();
	},
	compareAdd : function(listingid) {
		var node = $(".listingLayout .listingItem[data-listingid=" + listingid + "]");
		
		var compareItem = $("<div class='compareItem' data-listingid='" + node.attr('data-listingid') + "'></div>");
		compareItem.append("<p class='compareCount'></p>");
		compareItem.append("<p class='compareTitle'>" + node.attr('data-comparetitle') + "</p>");
		compareItem.append("<img src='/includes/images/shell/compareRemove.png' class='compareRemove' onClick='svlv.listings.compareRemove(" + node.attr("data-listingid") + ")'>");
		$(".listingLayout .compareItems").append(compareItem);
		
		this.compareCallback();
	},
	compareRemove : function(listingid) {
		var node = $(".listingLayout .compareItem[data-listingid=" + listingid + "]").remove();
		this.compareCallback();
	},
	compareToggle : function(listingid) {
		if (this.compareExists(listingid)) {
			this.compareRemove(listingid);	
		} else if ($(".compareItems .compareItem").length < 4) {
			this.compareAdd(listingid);	
		} else {
			alert('You may only compare 4 properties at a time.');	
		}
	},
	compareExists : function(listingid) {
		if ($(".listingLayout .compareItem[data-listingid=" + listingid + "]").length > 0) {
			return true;	
		} else {
			return false;	
		}
	},
	updateCompareEvent : function() {
		if ($(".compareItems .compareItem").length > 0) {
			$(window).bind("scroll", svlv.listings.compareScroll);
		} else {
			$(window).unbind("scroll", svlv.listings.compareScroll);	
		}
	},
	updateCompareLink : function() {
		var data = [];
		$(".listingLayout .compareItem").each(function() {
			data.push($(this).attr("data-listingid"));
		});
		
		$(".compareBar .redArrow").attr("href", this.compareURL + "?listingids=" + data.join(','));
	},
	updateCompareCounts : function() {
		var i = 1;
		$(".listingLayout .compareItem").each(function() {
			$(this).find(".compareCount").html("Property #" + i);
			i++;
		});
	},
	updateCompareLinks : function() {
		$(".listingLayout .listingItem .lvCompareLink span").html("Compare Property");
		
		$(".listingLayout .compareItem").each(function() {
			$(".listingLayout .listingItem[data-listingid=" + $(this).attr("data-listingid") + "] .lvCompareLink span").html("Remove Compare");
		});
	},
	updateCompareVisibility : function() {
		if ($(document).scrollTop() > this.settings.compareStartTop) {
			if ($(".compareBar").css("position") != "fixed") {
				$(".compareBar").css({ position : "fixed", top : 0, zIndex : 10 });
			}
		} else if ($(document).scrollTop() <= this.settings.compareStartTop) {
			if ($(".compareBar").css("position") != "static") {
					$(".compareBar").css({ position : "static", top : "auto", zIndex : "auto" });
			}
		}
		
		if ($(".compareBar .compareItem").length > 0 && $(".compareBar:visible").length == 0) {
			$(".compareBar").show();	
		} else if ($(".compareBar .compareItem").length == 0 && $(".compareBar:visible").length > 0) {
			$(".compareBar").hide();	
		}
	},
	addJackRabbits : function(listingids, callback) {
		var mydate = new Date();
		var day = mydate.getDate();
		var month = mydate.getMonth() + 1;
		var year = mydate.getFullYear();
		$.get("/includes/cfcs/controller/controller.cfm?object=jackObj&action=getData&returnType=json", { listingids : listingids, startDate : year + "-" + month + "-" + day, endDate : year + "-" + month + "-" + day }, function(controllerData) {
			var data = controllerData.data;
			for (var i in data) {
				var listing = $(".jackrabbitItem[data-listingid=" + i + "]");
				for (var k in data[i]) {
					if (data[i][k] > 0) {
						var bigPrice = Math.floor(data[i][k]);
						var smallPrice = data[i][k] - Math.floor(data[i][k]);
						smallPrice = smallPrice.toFixed(2);
						smallPrice = smallPrice.replace("0.", "");
						listing.find(".bigPrice").html("$" + bigPrice + ".");
						listing.find(".smallPrice").html(smallPrice);
					} else {
						listing.find(".bigPrice").html("Sold");
						listing.find(".smallPrice").hide();
						listing.find(".price").append('<div class="jrSoldText">Click "Book Direct" to find rates for other nights</div>');
					}
					listing.find(".listingBook").show();
					svlv.buildAjaxOverlay(listing.find(".viewrates"));
				}
			}
			
			if (typeof(callback) == "function") {
				callback();
			}
		}, "json");
	},
	getDetailYouTube : function(userid) {
		$.get(svlv.settings.controllerURL, { object : "listingDetailObj", action : "youtubeAjax", userid : userid, returntype : "json" }, function(data) {
			if (data.status == "success") {
				$(".youtubeAjaxContainer").html(data.data);	
			}
		}, "json");
	}
}

svlv.overlayCalendar = {
	init : function() {
		svlv.buildLvDropDowns();
		this.selectDate($(".viewCalendar .viewDay.hasData").eq(0).attr("data-date"));
		$(".viewWindow").scrollable({
			onSeek : function() {
				var self = this;
				if (this.getIndex() == this.getSize() - 1) {
					var items = this.getItems();
					var current = items.eq(this.getIndex());
					
					var data = current.attr('data-date');
					var dateParts = data.split("-");
					
					var newDate = new Date(dateParts[0], dateParts[1], 1);
					var dateString = newDate.getFullYear() + "-" + (newDate.getMonth() + 1) + "-" + newDate.getDate();
					$(".viewCalendars").append(svlv.settings.loading);
					$.get(svlv.settings.controllerURL, { object : "listingDetailObj", action : "calendarBottom", startdate : dateString, listingid : $(".viewRates").attr("data-listingid") }, function(data) {
						var newContent = $(data);
						$(".loading").detach();
						$(".viewCalendars").append(newContent);
						self.seekTo(self.getIndex());
						svlv.overlayCalendar.adjustHeight();
					});
				}
			},
			next : ".viewRight",
			prev : ".viewLeft"
		});
		this.adjustHeight();
	},
	adjustHeight : function() {
		var topHeight = $(".viewCalendarContainer").height();
		$(".viewCalendarContainer").each(function() {
			if ($(this).height() > topHeight) {
				topHeight = $(this).height();	
			}
		});
		$(".viewWindow").height(topHeight);
	},
	selectDate : function(dateString) {
		var start = $(".viewCalendar .viewDay.start");
		var finish = $(".viewCalendar .viewDay.finish");
		var newNode = $(".viewDay.hasData[data-date=" + dateString + "]");
		var allNodes = $(".viewDay").not(".notactive");
		
		if (start.length == 0) {
			$(".viewRatesTopRight .dateInput").eq(0).val(dateString);
			$(".viewRatesTopRight .dateInput").eq(1).val(dateString);
			newNode.addClass("selected").addClass("start");
		} else if (start.length == 1 && finish.length == 0 && newNode.get(0) != start.get(0)) {
			var startPos = $.inArray(start.get(0), allNodes);
			var endPos = $.inArray(newNode.get(0), allNodes);
			
			if (endPos < startPos) {
				start.removeClass("selected").removeClass("start");
				return this.selectDate(dateString);	
			}
			
			for(var i = startPos; i < endPos; i++) {
				if (allNodes.eq(i).hasClass("hasData")) {
					allNodes.eq(i).addClass("selected");	
				} else {
					alert('Invalid date range, day in between does not have available rooms');
					allNodes.removeClass("selected");
					start.addClass("selected");
					return false;
				}
			}
			
			$(".viewRatesTopRight .dateInput").eq(1).val(dateString);
			newNode.addClass("selected").addClass("finish");	
		} else if (newNode.get(0) == start.get(0) && finish.length == 0) {
			allNodes.removeClass("finish").removeClass("selected").removeClass("start");
			this.selectDate("");
		} else {
			allNodes.removeClass("finish").removeClass("selected").removeClass("start");
			this.selectDate(dateString);
		}
	},
	selectNode : function(node) {
		this.selectDate(node.attr("data-date"));
	}
}

svlv.overlayGallery = {
	init : function() {
		$(".overlayGallery .pane").eq(0).fadeIn().addClass("active");
	},
	cycle : function(num) {
		var activeNode = $(".overlayGallery .pane.active");
		var nodes = $(".overlayGallery .pane");
		for (var i = 0; i < nodes.length; i++) {
			if (nodes.get(i) == activeNode.get(0)) {
				index = i;
			}
		}
		
		next = index + num;
		
		if (next >= nodes.length) {
			next = 0;	
		} else if (next < 0) {
			next = nodes.length - 1;
		}
		
		activeNode.removeClass("active").hide();
		nodes.eq(next).addClass("active").fadeIn();
		$(".overlayGallery .count").html(next + 1);
	}
}

svlv.dragdrop = {
	getDefaultSettings : function() {
		return {
			clone : false,
			callback : "",
			dropSelector : ".lvDroppable",
			dropProxy : $("<div class='lvDropProxy'>Insert Here</div>")
		}
	},
	makeDraggable : function(nodes, settings) {
		var mysettings = this.getDefaultSettings();
		for (var i in settings) {
			mysettings[i] = settings[i];	
		}
		
		nodes.bind('mousedown', { settings : mysettings, library : this }, this.dragFunction);
	},
	dragFunction : function(e) {
		var original = this;
		if (e.which == 1) {
			var drag = $(this).clone().hide().appendTo("body").addClass('lvDragging').css({ opacity : .6 });
			var dropProxy = e.data.settings.dropProxy;
			var startx = $(original).offset().left;
			var starty = $(original).offset().top;
			var diffx = e.pageX - startx;
			var diffy = e.pageY - starty;
			drag.css({ left : startx, top : starty, width : $(original).width() });
			var i = 0;
			$("body").mousemove(function(data) {
				var startx = data.pageX - diffx;
				var starty = data.pageY - diffy;
				$(".lvDragging").css({ left : startx, top : starty }).show();
				var inDrop = false;
				$(e.data.settings.dropSelector).each(function() {
					var dropZone = $(this);
					if (e.data.library.mouseWithin(data.pageX, data.pageY, dropZone)) {
						var draggable = $(this).find(".lvDraggable");
						draggable.each(function() {
							var offset = $(this).offset();
							var height = $(this).outerHeight(true);
							
							if (data.pageY > offset.top + height/2 && data.pageY < offset.top + height) {
								if ($(this).next().hasClass("lvDropProxy") == false) {
									$(this).after(dropProxy);
								}
								
								return false;
							} else if (data.pageY > offset.top && data.pageY < offset.top + height/2) {
								if ($(this).prev().hasClass("lvDropProxy") == false) {
									$(this).before(dropProxy);
								}
								
								return false;
							}
						});
						
						if (draggable.length == 0) {
							$(this).append(dropProxy);	
						}
						
						inDrop = true;
						return false;
					}
				});
				
				if (!inDrop) {
					dropProxy.detach();
				}
				
				return false;
			}); 
			$("body").mouseup(function(data) {
				var drop = dropProxy.closest(".lvDroppable");
				var drag = $(".lvDragging").removeClass("lvDragging");
				
				if (drop.length > 0) {
					if (e.data.settings.clone) {
						drag.removeAttr("style");
						dropProxy.after(drag);
					} else {
						dropProxy.after(original);
						drag.remove();
					}
				} else {
					drag.remove();	
				}
				
				if (e.data.settings.callback != "") {
					e.data.settings.callback($(original), drop, drag); 
				}
				
				$(this).unbind('mousemove').unbind(data);
				dropProxy.remove();
			});
			
			return false;
		}
	},
	mouseWithin : function(x,y,node) {
		var offset = node.offset();
		var height = node.outerHeight(true);
		var width = node.outerWidth(true);
		
		if (x > offset.left && x < offset.left + width && y > offset.top && y < offset.top + height) {
			return true;	
		} else {
			return false;	
		}
	}
}

svlv.google = {
	isMapVisible : function() {
		return $(".mapHolder .gmapBox").is(":visible");
	},
	toggleMap : function() {
		if (this.isMapVisible()) {
			this.hideMap();	
		} else {
			this.showMap();	
		}
	},
	showMap : function() {
		$(".mapHolder .gmapBox").show();
		initWidgetMap();
	},
	hideMap : function() {
		$(".mapHolder .gmapBox").hide();
	},
	goToPlacemark : function(num) {
		if (!this.isMapVisible()) {
			this.showMap();	
		}
		
		$(".mapHolder").get(0).scrollIntoView();
		for(var i in placemarks) {
			placemark = placemarks[i][num];
		}
		
		placemark.marker.openInfoWindowHtml(placemark.marker.myInfoHTML);
	},
}

/*** Owen: This section is pretty ugly spaghetti code. If I had known I was going to use the same slider for both videos and photos I would have made it object oriented. Let me know if you encounter problems. ***/
svlv.vpt = {
	settings : {
		imagesLoaded : 0
	},
	loadVideos : function(videoid) {
		var self = this;
		
		$.get("/includes/cfcs/controller/controller.cfm?object=vptObj&action=videoPane", function(html) {
			$(".vptVideoAjax").empty().html(html);
			if (videoid != "") {
				self.selectVideo(videoid);	
			} else {
				self.selectVideo($(".vptVideoAjax .photoItem").eq(0).attr("data-videoid"));	
			}
			
			$(".vptVideoAjax .photoWindow").scrollable({
				circular : true
			});
			
			$(".vptVideoAjax .photoItem").tooltip({ effect: "slide", opacity : ".9", predelay : 100 });
			
			$(".vptVideoAjax .photoItem").click(function() {
				self.selectVideo($(this).attr("data-videoid"));
			});
			
			var api = $(".vptVideoAjax .photoWindow").data("scrollable");
			var node = $(".vptVideoAjax .photoItem[data-videoid=" + videoid + "]");
			self.positionSlide(api, node);
			svlv.buildAjaxOverlay($(".videoShare"));
		});
	},
	cyclePhoto : function(num) {
		var index = $(".vptPhotoAjax .photoContainer:visible").attr("data-index");
		var next = parseInt(index) + num;
		if (next > $(".vptPhotoAjax .photoContainer").length) {
			next = 1;	
		} else if (next <= 0) {
			next = $(".vptPhotoAjax .photoContainer").length;	
		}
		
		this.selectPhoto(next);
		var api = $(".vptPhotoAjax .photoWindow").data("scrollable");
		var node = $(".vptPhotoAjax .photoItem[data-index=" + next + "]");
		this.positionSlide(api, node);
	},
	selectVideo : function(videoid) {		
		for(var i = 0; i < svlv.vpt.settings.videos.length; i++) {
			if (svlv.vpt.settings.videos[i].videoid == videoid) {
				var video = svlv.vpt.settings.videos[i];
				var container = $(".vptVideoAjax .largeVideo");
				container.find("iframe").attr("src", "http://www.youtube.com/embed/" + video.videoid + "?wmode=transparent");
				container.find("h2").html(video.title);
				container.find(".contentString").html(video.content);
				container.find(".dateString").html(video.published);
				$(".vptVideoAjax .photoItem").removeClass("current");
				$(".vptVideoAjax .photoItem[data-videoid=" + video.videoid + "]").addClass("current");
				svlv.misc.setHashVar("videoid", video.videoid);
				
				var shareURL = "/includes/cfcs/controller/controller.cfm?object=shareContent&action=mailOverlay&subject=" + encodeURI('Visit Las Vegas: Video Tour') + "&title=" + encodeURI('Share Video with a Friend') + "&url=" + encodeURIComponent(window.location.href);
				$(".vptVideoAjax .videoShare").attr("href", shareURL);
			}
		}
	},
	loadTour : function(tourid) {
		svlv.misc.setHashVar("tourid", tourid);
		this.settings.imagesLoaded = 0;
		$(".vptPhotoAjax").empty().html("<div class='loading' style='text-align: center;'><img src='/includes/images/shell/ajax-loader.gif'></div>");
		$.get("/includes/cfcs/controller/controller.cfm?object=vptObj&action=photoTour", { tourid : tourid }, function(data) {
			$(".vptPhotoAjax").empty();
			$(".vptPhotoAjax").html(data);
			svlv.buildLvDropDown($(".vptFull select"));
			svlv.buildAjaxOverlay($(".photoShare"));
		});
	},
	selectPhoto : function(index) {
		$(".vptPhotoAjax .photoContainer").hide();
		var container = $(".vptPhotoAjax .photoPaneTop").find(".photoContainer[data-index=" + index + "]");
		container.fadeIn();
		$(".vptPhotoAjax .photoItem").removeClass("current");
		$(".vptPhotoAjax .photoItem[data-index=" + index + "]").addClass("current");
		
		var shareURL = "/includes/cfcs/controller/controller.cfm?object=shareContent&action=mailOverlay&subject=" + encodeURI('Visit Las Vegas: Photo Tour') + "&title=" + encodeURI('Share Photo with a Friend') + "&url=" + encodeURIComponent(window.location.href);
		$(".vptPhotoAjax .photoShare").attr("href", shareURL);
		
		svlv.misc.setHashVar("photoid", $(".vptPhotoAjax .photoItem.current").attr("data-photoid"));
	},
	initPhotos : function() {
		var self = this;
		
		self.sizeWindows();
		
		$(".vptPhotoAjax .photoWindow").scrollable({
			circular : true
		});
		
		$(".vptPhotoAjax .photoContainer").not(":first").hide();	
		$(".vptPhotoAjax .photoItem").click(function() {
			self.selectPhoto($(this).attr("data-index"));
		});
		
		var index = self.getInitialPhotoIndex();
		self.selectPhoto(index);
		
		var api = $(".vptPhotoAjax .photoWindow").data("scrollable");
		var node = $(".vptPhotoAjax .photoItem[data-index=" + index + "]");
			
		self.positionSlide(api, node);
	},
	initAjaxState : function() {
		var tourid = svlv.misc.getHashVar("tourid");
		var videoid = svlv.misc.getHashVar("videoid");
		
		this.loadVideos(videoid);
		
		if (tourid) {
			this.loadTour(svlv.misc.getHashVar("tourid"));
			this.settings.tourid = tourid;	
		} else {
			this.loadTour(this.settings.tourid);
		}
		
		$(".vptFull .lvDropDownFinal .lvOption[data-value=" + this.settings.tourid + "]").click();
	},
	getInitialPhotoIndex : function() {
		var photoid = svlv.misc.getHashVar("photoid");
		
		if (photoid) {
			var index = $(".photoContainer[data-photoid=" + photoid + "]").attr("data-index");
			if (!index) {
				return 1;	
			} else {
				return index;
			}
		} else {
			return 1;	
		}
	},
	getInitialIndex : function() {
		if (svlv.misc.getHashVar("tab")) {
			var name = svlv.misc.getHashVar("tab");	
		} else {
			var name = "Videos";	
		}
		
		var tabs = $(".vptTab");
		
		for (var i = 0; i < tabs.length; i++) {
			if (tabs.eq(i).attr("data-name") == name) {
				return i;
			}
		}
		
		return 1;
	},
	sizeWindows : function() {
		/*** 
		Owen: Image width cannot be got until window is loaded. In addition, hidden images have no width, thus we have to append them to a div off the screen and handle the modifications. It is certainly an imperfect solution, but I am unaware of a better way. If you find it, let me know.
		***/
		$("body").append("<div class='hiddenExchanger' style='position: absolute; left: -10000'></div>");
		$(".primaryPhoto").each(function() {
			$(this).find(".mainImage").appendTo(".hiddenExchanger");
			var width = $(".hiddenExchanger .mainImage").width();
			$(".hiddenExchanger .mainImage").prependTo($(this));
			$(this).width(width);
		});
		$(".hiddenExchanger").remove();
	},
	imageLoaded : function() {
		this.settings.imagesLoaded++;
		if (this.settings.imagesLoaded == $(".vptFull .mainImage").length) {
			this.initPhotos();	
		}
	},
	positionSlide : function(api, node) {
		/*** Owen: Ensures that the slider is currently displaying the selected node ***/
		var slides = api.getItems();
		
		for (var i = 0; i < slides.length; i++) {
			if (slides.eq(i).has(node).length > 0) {
				if (!slides.eq(i).is(":visible")) {
					slides.eq(i).closest(".vptPane").show();
					api.seekTo(i);
					slides.eq(i).closest(".vptPane").hide();
				} else {
					api.seekTo(i);
				}
			}
		}
	}
};

svlv.misc = {
	getUrlParams : function(url) {
		url = typeof(url) == "undefined" ? window.location.href : url;
		var paramString = url.match(/\?(.*)/ig);
		if (!paramString) {
			return {}; 	
		}

		var data = paramString[0];
		data = data.replace("?", "").split("&");
		var params = {};
		for(var i = 0; i < data.length; i++) {
			var internal = data[i].split("=");
			if (typeof(params[internal[0]]) != "undefined") {
				params[internal[0]] += "," + internal[1];	
			} else {
				params[internal[0]] = internal[1];
			}
		}
		
		return params;
	},
	getHashVar : function(name) {
		var myReg = new RegExp(name + "=([\\w-]+)", "i");
		var myurl = window.location.href;
		var data = myurl.match(myReg);
		
		if (data == null) {
			return 	"";
		} else {
			return data[1];	
		}
	},
	setHashVar : function(name, value) {
		if (this.getHashVar(name)) {
			var myReg = new RegExp(name + "=([\\w-]+)", "i");
			window.location.hash = window.location.hash.replace(myReg, name + "=" + value);
		} else {
			if (window.location.hash != "") {
				var add = "&";
			} else {
				var add = "";	
			}
			window.location.hash = window.location.hash + add + name + "=" + value;	
		}
	},
	setSameHeight : function(nodes) {
		var tallest = nodes.eq(1).height();
		for (var i = 0; i < nodes.length; i++) {
			if (nodes.eq(i).height() > tallest) {
				tallest = nodes.eq(i).height();	
			}
		}
		nodes.height(tallest);	
	},
	youtubeAddWmode : function(nodes) {
		nodes.each(function() {
			var src = $(this).attr("src");
			if ($(this).attr("src").match("http://www.youtube.com").length > 0) {
				if ($(this).attr("src").match("\\?") > 0) {
					var add = "&";	
				} else {
					var add = "?"	
				}
					
				$(this).attr("src", $(this).attr("src") + add + "wmode=transparent");
			}
		});
	}
};

