﻿//(c) 2008 Michael Manning 
jQuery.parseQuery=function(A,B){var C=(typeof A==="string"?A:window.location.search),E={f:function(F){return unescape(F).replace(/\+/g," ")}},B=(typeof A==="object"&&typeof B==="undefined")?A:B,E=jQuery.extend({},E,B),D={};jQuery.each(C.match(/^\??(.*)$/)[1].split("&"),function(F,G){G=G.split("=");G[1]=E.f(G[1]);D[G[0]]=D[G[0]]?((D[G[0]] instanceof Array)?(D[G[0]].push(G[1]),D[G[0]]):[D[G[0]],G[1]]):G[1]});return D};

var SSP_GALLERY_BASE_URL = "/images/galleries/";
var SSP_GALLERY_SWF = "ssp.swf";
var SSP_VIDEO_BASE_URL = "/videos/";



$(document).ready(function() {
	setupPageBeautyShots();
	setupVideoLinks();
	setupListingsAdvancedSearchDisplay("ListingsSearchCriteria", "ListingsSearchCriteriaToggle");
	fixAspNetFormAction("Listings.aspx");
	fixBackToListingsLink();
});

function setupPageBeautyShots() {
  var replace = $(".pageBeautyShot, .pageBeautyShotMain, .listingDetailMainImage, .pageGallery");
  var replaceOptions = replace.children(0).attr("rel");
  
  if((replace.length > 0) && (replaceOptions != undefined)){
		eval("replaceOptions = {" + replaceOptions + "}");
		replaceContentWithSSPGallery(replace.children(0), replaceOptions["g"], replaceOptions["w"], replaceOptions["h"], replaceOptions["s"]);
		}
	}

function replaceContentWithSSPGallery(element, galleryId, width, height, source){
	var sspUrl = SSP_GALLERY_BASE_URL + galleryId + '/' + SSP_GALLERY_SWF;
	var sspId = "FlashGallery_" + galleryId;
	var sspBaseUrl = SSP_GALLERY_BASE_URL + galleryId + "/";
	
	var attributes = {id:sspId, base:sspBaseUrl};
	var params = {base:sspBaseUrl, wmode:'opaque', allowFullScreen:'true'};
	var flashvars = {};
	
	if(source != null){
		params["xmlfile"] = source;
		flashvars["xmlfile"] = source;
		}
	
	if((element.id == undefined) || (element.id == null) || (element.id == "")){
		element.attr("id", galleryId + "_" + new Date().getMilliseconds());
		}
	
	swfobject.embedSWF(sspUrl, element.attr("id"), width, height, "9.0.0", false, flashvars, params, attributes);
	}
	
function setupVideoLinks(){
	$("a[href*=WatchVideo.aspx]").click(function(e){
		e.preventDefault();
		
		var videoDetails = this.href.split("?");
		videoDetails = $.parseQuery(videoDetails[1]);

		var videoUrl = SSP_VIDEO_BASE_URL + videoDetails.v + ".swf";
		var videoTitle = videoDetails['t'] || '';
		var videoWidth = videoDetails['w'] || 780;
		var videoHeight = videoDetails['h'] || 475;
		
		$.nyroModalManual({url:videoUrl, title:videoTitle, swf: {base:SSP_VIDEO_BASE_URL}, width:videoWidth, height:videoHeight});
		return false;
		});
	}
	
function setupListingsAdvancedSearchDisplay(criteriaDivId, toggleElementId){
	var urlParams = $.parseQuery();
	
	if((urlParams['type'] != null) && (urlParams['type'] != '')){
		$("#"+criteriaDivId).hide();
		}
		
	$('#'+toggleElementId).click(function(e){
		$("#"+criteriaDivId).toggle('normal');
		}).css('cursor', 'pointer');
	}

// Removes the querystring of the ASP.NET form. 	
function fixAspNetFormAction(pageFileName){
	$("form[action*="+pageFileName+"]").attr("action", pageFileName);
	}
	
function fixBackToListingsLink(){
	$("a.pageBackLink").click(function(e){
		if((document.referrer.indexOf("Listings.aspx") != -1) || (document.referrer.indexOf("Newsroom.aspx") != -1)){
			e.preventDefault();
			history.go(-1);
			}
		}); 
	}
