﻿if (typeof App == "undefined" || !App)
	App = {};

if (typeof App.Plugs == "undefined" || !App.Plugs)
	App.Plugs = {};

if (typeof VisitProxy == "undefined" || !VisitProxy)
	VisitProxy = {};


/*VisitProxy.ValidateDate = function(dateString) {
	debugger;
	var d = new Date(dateString);
	return !isNaN(d.valueOf());
}
*/
VisitProxy.HighlightElement = function(element) {
	var blinkCount = 2;

	blinkCount = blinkCount * 2;
	do {
		element['fade' + (blinkCount % 2 == 0 ? 'Out' : 'In')]('slow');
	} while (--blinkCount);
}


VisitProxy.SearchForm = function() {


}

VisitProxy.SearchForm.prototype = {
	_components: {},


	addComponent: function(id, component) {
		this._components[id] = component;
	},

	validate: function() {
		var isValid = true;
		for (var id in this._components) {
			if (!this._components[id].validate()) {
				this._components[id].displayError();
				isValid = false;
			}
		}

		return isValid;
	},

	beforeSubmit: function() {
		for (var id in this._components) {
			this._components[id].beforeSubmit();
		}
	},

	removeEmptyComponents: function() {
		for (var id in this._components) {
			this._components[id].removeIfNotNeeded();
		}
	},

	getComponent: function(id) {
		return this._components[id];
	},

	hookOnEnter: function() {
		for (var id in this._components) {
			this._components[id]._element.keydown(function(event) {
				if (event.keyCode == 13) {
					event.preventDefault();
					jQuery('#VisitProxy_bookingbutton').click();
				}
			});
		}
	}
}

VisitProxy.SearchComponent = function(element,options) {
	this._element = element;
	if(typeof(options) != "undefined")
		for (var id in options) {
			eval("this." + id + "=" + options[id]);
		}
}

VisitProxy.SearchComponent.prototype = {
	displayError: function() {
		VisitProxy.HighlightElement(this._element);
	},
	validate: function() { return true; },
	
	beforeSubmit: function() {},
	
	removeIfNotNeeded: function() {
		if (this._element.val() == "" || this._element.val() == "0")
			this._element.remove();
	}
}

VisitProxy.sf = new VisitProxy.SearchForm();


//Add components
var el = jqueryVSP("#VisitProxy_StartDate");
if (el.length > 0)
	VisitProxy.sf.addComponent("startDate", new VisitProxy.SearchComponent(el, {
		validate: function() {
			if (typeof (firstValidBookingDate) != 'undefined' && firstValidBookingDate != null
				&& this._element.datepicker('getDate') != null && firstValidBookingDate > this._element.datepicker('getDate').valueOf()) {
					return false;
				}
			return this._element.val() == "" || !(isNaN(this._element.datepicker('getDate').valueOf()));
		}
	}));

el = jqueryVSP("#VisitProxy_EndDate");
if (el.length > 0)
	VisitProxy.sf.addComponent("endDate", new VisitProxy.SearchComponent(el, {
		validate: function() {
			return this._element.val() == "" || !(isNaN(this._element.datepicker('getDate').valueOf()));
		}
	}));

el = jqueryVSP("#VisitProxy_EndDateDd");
if (el.length > 0)
	VisitProxy.sf.addComponent("endDateDd", new VisitProxy.SearchComponent(el, {
		beforeSubmit: function() {
			try {
				var date = jqueryVSP('#VisitProxy_StartDate').datepicker('getDate');
				var add = parseInt(this._element.val());

				if (!isNaN(add)) {
					date.setDate(date.getDate() + add);
					jqueryVSP('#VisitProxy_EndDate').val(date.getFullYear() + "-"
								+ (date.getMonth() < 9 ? "0" : "") + (date.getMonth() + 1)
								+ "-" + (date.getDate() < 10 ? "0" : "") + date.getDate());
				} else
					jqueryVSP('#VisitProxy_EndDate').remove();
			} catch (error) {
				jqueryVSP('#VisitProxy_EndDate').remove()
			}

		}
	}));
	

el = jqueryVSP("#VisitProxy_Search");
if (el.length > 0)
	VisitProxy.sf.addComponent("freeText", new VisitProxy.SearchComponent(el));

el = jqueryVSP("#searchCategory");
if (el.length > 0)
	VisitProxy.sf.addComponent("category", new VisitProxy.SearchComponent(el), {
		removeIfNotNeeded: function() {
			if (this._element.val() == "0")
				this._element.remove();
		}
	});
el = jqueryVSP("#VisitProxy_PackageLightCategoryId");
if (el.length > 0)
	VisitProxy.sf.addComponent("packagelightcategoryid", new VisitProxy.SearchComponent(el));

el = jqueryVSP("#proxy_area");
if (el.length > 0)
	VisitProxy.sf.addComponent("area", new VisitProxy.SearchComponent(el));

	

el = jqueryVSP("#arena");
if (el.length > 0)
	VisitProxy.sf.addComponent("arena", new VisitProxy.SearchComponent(el));

el = jqueryVSP("#VisitProxy_reservation_pid");
if (el.length > 0)
	VisitProxy.sf.addComponent("roomsConfig", new VisitProxy.SearchComponent(el, {
		removeIfNotNeeded: function() {
			if (jqueryVSP("#VisitProxy_StartDate").length == 0 || jqueryVSP("#VisitProxy_StartDate").val() == "" || !VisitProxy.sf.getComponent("startDate").validate() ||
				jqueryVSP("#VisitProxy_EndDate").length == 0 || jqueryVSP("#VisitProxy_EndDate").val() == "" || !VisitProxy.sf.getComponent("endDate").validate())
				this._element.remove();
		},

		beforeSubmit: function() {
			jqueryVSP("#Proxy_Bookable_Toggle").attr('selected', true);
		}
	}));

el = jqueryVSP("#proxy_sort");
if (el.length > 0)
	VisitProxy.sf.addComponent("sort", new VisitProxy.SearchComponent(el));

el = jqueryVSP("#vp_proxy_sortfix");
if (el.length > 0)
	VisitProxy.sf.addComponent("sortfix", new VisitProxy.SearchComponent(el, {
		beforeSubmit: function() {
			try {
				if (jqueryVSP("#vp_proxy_sortfix").length > 0) {
					jqueryVSP("#proxy_sort").remove();
				}
			} catch (error) {
			}

		}
	}));

VisitProxy.sf.hookOnEnter();

function proxy_SetupDates(lang) {
	if (jqueryVSP('#VisitProxy_StartDate, #VisitProxy_EndDate').length == 0)
		return;
//	jqueryVSP.datepicker.setDefaults(jqueryVSP.extend({ showMonthAfterYear: false, showOtherMonths: true, minDate: new Date() }, jqueryVSP.datepicker.regional['']));
//	var options = typeof (jqueryVSP.datepicker.regional[lang]) != 'undefined' ? jqueryVSP.datepicker.regional[lang] : {};
	var options = {minDate: 0};
	options.onSelect = function(dateText, inst) {
		d = jqueryVSP(this).datepicker('getDate');
		if (d) {
			ed = new Date(d.setDate(d.getDate() + 1));
			if (jqueryVSP('#VisitProxy_EndDate').datepicker('getDate') <= d) {
				jqueryVSP('#VisitProxy_EndDate').datepicker('setDate', ed);
			}
			jqueryVSP('#VisitProxy_EndDate').datepicker('option', 'minDate', ed);
			setNights();
		}
	};

	var startDateIsValid = function() {
		var d = new jqueryVSP('#VisitProxy_StartDate').datepicker('getDate');
		if (d == null)
			return false;
		else {
			if (typeof (firstValidBookingDate) != 'undefined' && firstValidBookingDate != null && firstValidBookingDate > d.valueOf())
				return false;
			return !isNaN(d.valueOf());
		}
	}

	var showHideTimeperiod = function() {
		if (startDateIsValid())
			jqueryVSP('#VisitProxy_EndDateContainer').removeClass('invisible');
		else
			jqueryVSP('#VisitProxy_EndDateContainer').addClass('invisible');
	}

	var setEndDateMin = function(dateText, inst) {
		var startDate = jqueryVSP('#VisitProxy_StartDate').datepicker('getDate');

		var endDateEl = jqueryVSP('#VisitProxy_EndDate');
		if(startDateIsValid(startDate)) {
			endDateEl.datepicker('setDate', startDate.setDate(startDate.getDate() + 1));
			endDateEl.datepicker('option', 'minDate', startDate);
		}
	}
	var startDateInput = jqueryVSP('#VisitProxy_StartDate');
	var endDateInput = jqueryVSP('#VisitProxy_EndDate');
	
	var bindDatepickerToIcon = function(el) {
		el.parent().children('.vp_icon').click(function(e) { jqueryVSP(this).parent().children('input').datepicker('show'); });
	};
	bindDatepickerToIcon(startDateInput);
	bindDatepickerToIcon(endDateInput);
	

	startDateInput.datepicker({
		minDate: (typeof(firstValidBookingDate) != 'undefined' && firstValidBookingDate != null) ? firstValidBookingDate : new Date(),
		onClose: function(dateText, inst) {
			showHideTimeperiod();
			setEndDateMin(dateText, inst);
			setNights();
		} 
	}
	);

	jqueryVSP.watermark.options.className = "watermark";
	
	if(!VisitProxy.IsAccommodation)
		jqueryVSP('#VisitProxy_StartDate').watermark(VisitProxy.Lang.Search.EnterDate);

	options.minDate = 1;
	delete (options.onSelect);
	options.onSelect = function(dateText, inst) {
		setNights();
	};
	endDateInput.datepicker(options).datepicker('setDate', bookingEndDate);
	jqueryVSP('#VisitProxy_StartDate, #VisitProxy_EndDate').siblings('.side').click(function() {
		jqueryVSP(this).siblings('input').focus();
	});

	var setNights = function() {
		var nightsSpan = jqueryVSP('#proxy_nights');
		if (nightsSpan.length > 0) {
			var startDate = jqueryVSP('#VisitProxy_StartDate').datepicker('getDate');
			var endDate = jqueryVSP('#VisitProxy_EndDate').datepicker('getDate');
			if (startDateIsValid(startDate)) {
				var nights = Math.round(((endDate - startDate) / 86400000));
				var html = " (" + nights + " " + ((nights > 1) ? VisitProxy.Lang.Search.Nights : VisitProxy.Lang.Search.Night) + ")";
				nightsSpan.html(html);
			}
		}
	};
}

function proxy_SetupSearchForm() {
	var sidebar = jqueryVSP(".VisitProxy_Sidebar");
	var listing = jqueryVSP("#proxy_productListing");
	var loading = jqueryVSP("#VisitProxy_ListProgress");
	var heading = jqueryVSP(".VisitProxy_ResultHeader");
	var container = jqueryVSP("#VisitProxy_bookingbox");
	var crosslink = jqueryVSP("#VisitProxy_Crosslinks");

	var formUpdate = function(e) {
		var roomCount = jqueryVSP(this).val();
		var guestwrapper = jqueryVSP(this).parents(".vp_inner").eq(0).find('.vp_guestwrapper');
		if (roomCount == 1) {
			guestwrapper.addClass('vp_oneRoom').removeClass('vp_manyRooms');
		} else {
			guestwrapper.removeClass('vp_oneRoom').addClass('vp_manyRooms');
		}
		for (var i = 1; i <= 9; i++) {
			var master = "#VisitProxy_room" + i;
			var container = jqueryVSP(master);
			if (i <= roomCount) {
				container.removeClass('vp_hidden');
			} else {
				container.addClass('vp_hidden');
			}
		}
	}

	var childUpdate = function(e) {
		var idstring = jqueryVSP(this).attr('id');
		var childid = idstring.substring(idstring.indexOf("children") + 8);
		for (var i = 1; i <= 9; i++) {
			var master = "#VisitProxy_child" + childid + "" + i;
			if (i <= this.value)
				jqueryVSP(master).removeClass('vp_hidden');
			else
				jqueryVSP(master).addClass('vp_hidden');
		}
	}

	var buildRoomsString = function() {
		var element = jqueryVSP('#VisitProxy_reservation_pid');
		if (element.length > 0) {
			var count = jqueryVSP('#VisitProxy_search_rooms').val();
			var str = "";
			for (var i = 1; i <= count; i++) {
				if (str.length > 0) {
					str += "r";
				}
				str += jqueryVSP('#VisitProxy_adults' + i).val();

				var children = jqueryVSP('#VisitProxy_children' + i).val();
				if (children > 0) {
					str += "a";
				}
				for (var j = 1; j <= children; j++) {
					if (j > 1) {
						str += "c";
					}
					str += jqueryVSP('#VisitProxy_childage' + i + '' + j).val();
				}
			}
			element.val(str);
		}
	}

	var updateCabinSearchBy = function() {
		if (jqueryVSP('#VisitProxy_search_byweek').attr('checked')) {
			jqueryVSP(".vp_dates").hide();
			jqueryVSP(".vp_weeks").show();
		} else {
			jqueryVSP(".vp_dates").show();
			jqueryVSP(".vp_weeks").hide();
		}
	}

	var updateWeekSearchPeriod = function() {
		var weeklengthContainer = jqueryVSP('.vp_weeklength', container);
		var periodVal = jqueryVSP(this).val();
		var foundShowWeek = false;
		if (typeof (VisitProxy.ShowWeekFor) != 'undefined' && VisitProxy.ShowWeekFor != null) {
			for (var i = 0; i < VisitProxy.ShowWeekFor.length; i++) {
				if (VisitProxy.ShowWeekFor[i] == periodVal) {
					foundShowWeek = true;
					break;
				}
			}
		}
		if (foundShowWeek) {
			weeklengthContainer.show();
		} else {
			weeklengthContainer.hide();
		}
	}

	jqueryVSP('#VisitProxy_search_bydates, #VisitProxy_search_byweek').click(updateCabinSearchBy);
	updateCabinSearchBy();
	jqueryVSP('#VisitProxy_search_period').change(updateWeekSearchPeriod).change();
	jqueryVSP("#VisitProxy_formFix select").bind('change', buildRoomsString);
	jqueryVSP('#VisitProxy_search_rooms').bind('change', formUpdate).change();
	for (var i = 1; i <= 9; i++)
		jqueryVSP('#VisitProxy_children' + i).bind('change', childUpdate).change();

	var newForm = false;
	var moveForm = function(action, method) {
		// Copy the search form outside the bloody asp webform nightmare
		if (newForm)
			return;

		sidebar.hide();
		listing.hide();
		heading.hide();
		crosslink.hide();
		loading.removeClass('hidden');
		loading.show();


		if (jqueryVSP('#VisitProxy_StartDate').val() == VisitProxy.Lang.Search.EnterDate) {
			jqueryVSP('#VisitProxy_StartDate').val('');
		}

		//remove empty elements
		VisitProxy.sf.removeEmptyComponents();

		var selectIndex = jqueryVSP('select[name="searchCategory"]').attr('selectedIndex');
		if (selectIndex > 0) {
			jqueryVSP('input[type="radio"][name="searchCategory"]').remove();
		} else {
			jqueryVSP('select[name="searchCategory"]').remove();
		}

		var stuffToMove = jqueryVSP("#VisitProxy_formFix");
		var moreStuffToMove = jqueryVSP("#VisitProxy_FilterForm");

		VisitProxy.sf.beforeSubmit();

		newForm = jqueryVSP("<form action='" + action + "' method='" + method + "' style='display:none;'></form>");
		newForm.append(stuffToMove);
		newForm.append(moreStuffToMove);


		jqueryVSP(document.body).append(newForm);

		newForm.submit();
	};
	var commit = function(e) {
		e.preventDefault();
		if (VisitProxy.sf.validate()) {
			buildRoomsString();
			var button = jqueryVSP("#VisitProxy_bookingbutton");
			moveForm(button.attr('href'), "get");
		}
	};

	VisitProxy.commit = commit;
	
	jqueryVSP("#VisitProxy_bookingbutton").bind("click", commit);
	jqueryVSP("#VisitProxy_applybutton").bind("click", commit);
	jqueryVSP("#proxy_sort").bind("change", commit);
	jqueryVSP("#proxy_area").bind("change", commit);
}



(function(jqueryVSP) {
	jqueryVSP.fn.menutoggle = function(options) {
		elem = jqueryVSP(this);
		var onClick = function(e) {
			e.preventDefault();
			jqueryVSP(this).blur();
			elem.children('.vp_selected').removeClass('vp_selected');
			jqueryVSP(this).parent().addClass('vp_selected');
			jqueryVSP(this).find('input[type="radio"]').attr('checked', true);
			var name = jqueryVSP(this).find('input[type="radio"]').attr('name');
			jqueryVSP('select[name="' + name + '"]').remove();
			jqueryVSP("#VisitProxy_bookingbutton").click();

		}

		elem.find('> .vp_item a').each(function() {
			jqueryVSP(this).click(onClick);
			if (jqueryVSP(this).find("input[type='radio']").attr('checked')) {
				elem.children('.vp_selected').removeClass('vp_selected');
				jqueryVSP(this).parent().addClass('vp_selected');
			}
		});

	}

	jqueryVSP.fn.proxyCalendar = function(url) {
		var myUrl = url;
		elem = jqueryVSP(this);

		var bindEvents = function(elem, onNav, onClick) {

			elem.find('div.vp_hd > a ').each(function() {
				jqueryVSP(this).click(onNav);
			});

			elem.find('a.vp_activeday').each(function() {
				jqueryVSP(this).click(onClick);
			});
		};

		var setDepartureDate = function(e) {
			e.preventDefault();
			jqueryVSP('#VisitProxy_departureCalendarDate').val(jqueryVSP(this).attr('rel'));

			var parentTbl = jqueryVSP(this).parent().parent().parent();
			var tds = parentTbl.find('td');
			tds.removeClass('vp_selected');
			tds.filter('.vp_sel_mark').removeClass('vp_sel_mark').addClass('vp_sel');

			var currentSelectedTd = jqueryVSP(this).parent();
			currentSelectedTd.addClass('vp_selected');

			var passMarkIndex = tds.index(parentTbl.find('td.vp_pass_mark'));

			if (passMarkIndex == -1) {
				// if no passmark, the passmark is probably located in another month
				var firstActiveDay = parentTbl.find('td a.vp_activeday').filter(':first').parent();
				passMarkIndex = tds.index(firstActiveDay) - 1;
			}

			var selectedMarkIndex = tds.index(currentSelectedTd);

			var tdsToMarkPass = tds.filter(':gt(' + passMarkIndex + '):lt(' + (selectedMarkIndex - passMarkIndex - 1) + ')');

			tdsToMarkPass.removeClass().addClass('vp_sel_mark');

			jqueryVSP("#VisitProxy_Button").removeClass('vp_disabled');
		};

		var onNav = function(e) {
			e.preventDefault();
			jqueryVSP('#VisitProxy_ArrivalTable').addClass('vp_hidden');
			jqueryVSP('#VisitProxy_ArrivalLoading').removeClass('vp_hidden');
			jqueryVSP.get(jqueryVSP(this).attr('rel'), function(data) {
				var arrCal = jqueryVSP('#VisitProxy_arrivalCalendar');
				jqueryVSP('#VisitProxy_ArrivalLoading').addClass('vp_hidden');
				jqueryVSP('#VisitProxy_ArrivalTable').removeClass('vp_hidden');
				arrCal.html(data);
				bindEvents(arrCal, onNav, onClick);

			});
		}

		var onDepartureNav = function(e) {
			e.preventDefault();
			jqueryVSP('#VisitProxy_DepartureTable').addClass('vp_hidden');
			jqueryVSP('#VisitProxy_DepartureLoading').removeClass('vp_hidden');
			var depCal = jqueryVSP('#VisitProxy_departureCalendar');
			jqueryVSP.get(jqueryVSP(this).attr('rel'), function(data) {
				jqueryVSP('#VisitProxy_DepartureLoading').addClass('vp_hidden');
				jqueryVSP('#VisitProxy_DepartureTable').removeClass('vp_hidden');
				depCal.html(data);
				bindEvents(depCal, onDepartureNav, setDepartureDate);
			});
		}

		var onClick = function(e) {
			e.preventDefault();
			jqueryVSP("#VisitProxy_Button").addClass('vp_disabled');
			jqueryVSP('#VisitProxy_DepartureTable').addClass('vp_hidden');
			jqueryVSP('#VisitProxy_DepartureLoading').removeClass('vp_hidden');
			var depCal = jqueryVSP('#VisitProxy_departureCalendar');
			jqueryVSP(this).parent().parent().parent().find("td").removeClass('vp_selected');
			jqueryVSP(this).parent().addClass('vp_selected');
			depCal.val('');

			jqueryVSP.get(jqueryVSP(this).attr('rel'), function(data) {
				jqueryVSP('#VisitProxy_DepartureLoading').addClass('vp_hidden');
				jqueryVSP('#VisitProxy_DepartureTable').removeClass('vp_hidden');
				depCal.html(data);

				bindEvents(depCal, onDepartureNav, setDepartureDate);
			});
		}

		bindEvents(elem, onNav, onClick);

	}

})(jqueryVSP);

function proxy_setupImageScroller() {
	var wrapper = jqueryVSP("#productimagewrapper");
	var productImage = wrapper.find("img").load(function(event) {
		wrapper.removeClass('vp_loading');
	});

	
	//productImage.fancybox(fancyboxOptions);
	var scrollerContainer = jqueryVSP('#productimages');
	if (jqueryVSP(".vp_active", scrollerContainer).length == 0) {
		scrollerContainer.find(".vp_item").eq(0).addClass('vp_active');
	}
	var scroller = scrollerContainer.scrollable({ api: true, size: 3, items: '.vp_items', prevPage: 'vp_prev', nextPage: 'vp_next', activeClass: 'vp_active', disabledClass: 'vp_disabled' });

	var loadImage = function(image) {
		wrapper.addClass('vp_loading');
		var active = jqueryVSP(image);
		scroller.getItems().removeClass('vp_active');
		active.addClass('vp_active');
		var img = active.find('img');
		var src = img.attr('src');
		var endIndex = Math.min(src.indexOf("width"), src.indexOf("height"));
		var width = productImage.width();

		var height = productImage.height();
		var newsrc = src.substring(0, endIndex - 1) + "&width=" + width + "&height=" + height + "&crop=1";
		productImage.attr('src', newsrc);
		productImage.click(triggerLightboxClick);
		//productImage.fancybox(fancyboxOptions);
		updateButtons();
		updateNumbers();
	}
	var updateButtons = function() {
		var items = scroller.getItems();
		var totalcount = items.size();
		items.each(function(index) {
			if (jqueryVSP(this).hasClass('vp_active')) {
				if (index == 0) {
					prevImage.addClass('vp_disabled');
				} else {
					prevImage.removeClass('vp_disabled');
				}
				if (index == (totalcount - 1)) {
					nextImage.addClass('vp_disabled');
				} else {
					nextImage.removeClass('vp_disabled');
				}
			}
		});
	}

	scroller.getItems().click(function() {
		loadImage(this);
	});
	var nextImage = scrollerContainer.siblings('.vp_next');
	var prevImage = scrollerContainer.siblings('.vp_prev');
	var navigation = scrollerContainer.siblings('.vp_navigation').find(".vp_inner");
	nextImage.click(function(e) {
		e.preventDefault();
		scroller.getItems().filter('.vp_active').next().click();
	});

	var triggerLightboxClick = function(e) {
		e.preventDefault();
		var fullimglist = jqueryVSP("<div />");

		var currentImg = false;
		var itemcount = 0;
		var images = jqueryVSP("#productimages .vp_items img");
		var totalcount = images.length;
		var activeimg = jqueryVSP("#productimages .vp_items .vp_active img");
		var startindex = 0;
		if (activeimg.length > 0) {
			activeimg = activeimg.eq(0);
			startindex = images.index(activeimg);
		} else {
			activeimg = images.eq(0);
		}
		
		for (var i = 0; i < totalcount; i++) {
			if (startindex >= totalcount)
				startindex = 0;
			
			var currentImg = images.eq(startindex);
			var img = currentImg.attr('src');
			var endIndex = Math.min(img.indexOf("width"), img.indexOf("height"));
			var addImg = img.substring(0, endIndex - 1);
			var width = Math.round(jqueryVSP(window).width() / 0.8);
			var height = Math.round(jqueryVSP(window).height() / 0.8);
			addImg = addImg + "&width=" + width + "&height=" + height + "&fitaspect=1";

			var item = jqueryVSP("<a href='" + addImg + "' title='" + currentImg.attr('alt') + "'></a>");
			item.appendTo(fullimglist);
			
			startindex++;
		}

		fullimglist.hide().attr('id', 'vsp_fullimgbox').appendTo(document.body);

		jqueryVSP("a", fullimglist).colorbox({ open: true, photo: true, rel: 'vsp_product_images', maxWidth: '90%', maxHeight: '90%', current: "", onOpen: function() { jqueryVSP("object").hide(); }, onClosed: function() { jqueryVSP("object").show(); jqueryVSP("#vsp_fullimgbox").remove(); } });
	}

	productImage.click(triggerLightboxClick);

	prevImage.click(function(e) {
		e.preventDefault();
		var prev = scroller.getItems().filter('.vp_active').prev().click();
	});

	var seekImage = function() {
		scroller.getItems().each(function(index) {
			if (jqueryVSP(this).hasClass('vp_active')) {
				scroller.seekTo(index);
			}
		});
	}

	var updateNumbers = function() {
		var items = scroller.getItems();
		var total = items.size();
		var sel = 0;
		items.each(function(index) {
			if (jqueryVSP(this).hasClass('vp_active'))
				sel = index;
		});
		navigation.html((sel + 1) + "/" + total);
		var thisWidth = navigation.width();

		var wrapperWidth = navigation.parent().parent().width();
		var left = Math.round((wrapperWidth - thisWidth) / 2);
		navigation.parent().css('left', left + "px");
		navigation.parent().show();
	}
	updateButtons();
	updateNumbers();
	prevImage.show();
	nextImage.show();
}

function proxy_setupAccordion(selector) {
	jqueryVSP(selector).accordion({ header: 'div.vp_header', clearStyle: true, autoHeight: false });
	jqueryVSP(selector).find('div.vp_header').click(function() { jqueryVSP(this).blur(); });
}

if (typeof (VisitProxy) == "undefined")
	VisitProxy = {};

VisitProxy.postMe = function(path, params, method) {
	method = method || "post"; // Set method to post by default, if not specified.

	// The rest of this code assumes you are not using a library.
	// It can be made less wordy if you use one.
	var form = document.createElement("form");
	form.setAttribute("method", method);
	form.setAttribute("action", path);

	if (typeof (params) != 'undefined') {
		for (var key in params) {
			var hiddenField = document.createElement("input");
			hiddenField.setAttribute("type", "hidden");
			hiddenField.setAttribute("name", key);
			hiddenField.setAttribute("value", params[key]);

			form.appendChild(hiddenField);
		}
	}

	document.body.appendChild(form);    // Not entirely sure if this is necessary
	form.submit();
}

VisitProxy.Facebook = {};

VisitProxy.Facebook.LikeButtonInit = function() {
    var el = document.getElementById('vp_fblike');
    if (typeof (el) != 'undefined') {
        el.innerHTML = el.innerHTML.replace('LIKEHREF', window.location.host);
        FB.XFBML.parse(el);
    }
}

VisitProxy.recordOutboundLink = function(link, category, action) {
  try {
    if(typeof(VisitProxy.CustomerTracker) != 'undefined') {
        var pageTracker=_gat._getTracker(VisitProxy.CustomerTracker);
        pageTracker._trackEvent(category, action);
    }
    setTimeout('document.location = "' + link.href + '"', 100)
  }catch(err){}
}

