/*********************************************
* Popup JS for popups and new window opening *
**********************************************/

/*PROFILES*/
var profiles =
{
	/*Small*/
	windowSm:
	{
		height:500,
		width:380,
		createnew:0,
		status:1
	},
	/*Medium*/
	windowMed:
	{
		height:500,
		width:489,
		createnew:0,
		status:1
	},
	/*Large*/
	windowLrg:
	{
		height:500,
		width:600,
		createnew:0,
		status:1
	},
	/*Extra Large*/
	windowXlrg:
	{
		height:768,
		width:768,
		createnew:0,
		status:1,
		scrollbars:0
	},
	/*Alt View*/
	windowAltView:
	{
		height:640,
		width:460,
		createnew:0,
		status:1,
		scrollbars:0
	},
	/*All Styles*/
	windowAllStyles:
	{
		height:520,
		width:490,
		createnew:0,
		status:1
	},
	/*Centered New Window*/
	windowCenter:
	{
		height:300,
		width:400,
		createnew:0,
		center:1
	},
	/*Centered Not New Window*/
	windowNotNew:
	{
		height:300,
		width:400,
		center:1,
		createnew:0
	},
	/*SAS Clearance Popup*/
	windowSASCls: 
	{
		height:542,
		width:500,
		createNew:0,
		scrollbars:0
 	},
	/*Facebook Popup*/
	facebook: {
		height:542,
		width:500,
		createNew:0,
		scrollbars:0
	},
	/*Twitter Popup*/
	twitter: {
		height:542,
		width:800,
		createNew:0,
		scrollbars:0
	}

};

/*Count for number of popups on page*/
var index=1;

function vsPopUp(url, profile, jqsettings)
{
	var settings, parameters, mysettings, b, a;
		// for overrideing the default settings
		mysettings = profile.split(",");
		settings = {
			height:500, // sets the height in pixels of the window.
			width:380, // sets the width in pixels of the window.
			toolbar:0, // determines whether a toolbar (includes the forward and back buttons) is displayed {1 (YES) or 0 (NO)}.
			scrollbars:1, // determines whether scrollbars appear on the window {1 (YES) or 0 (NO)}.
			status:0, // whether a status line appears at the bottom of the window {1 (YES) or 0 (NO)}.
			resizable:1, // whether the window can be resized {1 (YES) or 0 (NO)}. Can also be overloaded using resizable.
			left:20, // left position when the window appears.
			top:20, // top position when the window appears.
			center:0, // should we center the window? {1 (YES) or 0 (NO)}. overrides top and left
			createnew:1, // should we create a new window for each occurance {1 (YES) or 0 (NO)}.
			location:0, // determines whether the address bar is displayed {1 (YES) or 0 (NO)}.
			menubar:0, // determines whether the menu bar is displayed {1 (YES) or 0 (NO)}.
			emailTitle:"", 
			emailContent:"",
			twitterContent:""
		};

		settings = jQuery.extend(settings, jqsettings);
		
		//set up the url
		if (profile == 'facebook') {
			settings.url = 'http://www.facebook.com/sharer.php?u=' + url;
		} else if (profile == 'twitter') {
			settings.url ="http://twitter.com/home?status=" + settings.twitterContent;
		} else if (profile == 'email') {
			window.location = "mailto:?subject=" + settings.emailTitle + "&body=" + settings.emailContent;
			return true;
		} else {
			settings.url = url;
		}
		
		// if mysettings length is 1 and not a value pair then assume it is a profile declaration
		// and see if the profile settings exists
		
		if(mysettings.length == 1 && mysettings[0].split(":").length == 1)
		{
			a = mysettings[0];
			
			// see if a profile has been defined
			if(typeof (profiles[a]) != "undefined")
			{
				settings = jQuery.extend(settings, profiles[a]);
			}
		}
		else
		{
			// overrides the settings with parameter passed in using the rel tag.
			for(var i=0; i < mysettings.length; i++)
			{
				b = mysettings[i].split(":");
				
				if((typeof (settings[b[0]]) != 'undefined') && b.length == 2)
				{
					settings[b[0]] = b[1];
				}
			}
		}

		// center the window
		if (settings.center == 1)
		{
			settings.top = (screen.height-(settings.height + 110))/2;
			settings.left = (screen.width-settings.width)/2;
		}
		 	
		/*Please do not change the height and width to hardcoded values here. This is used globally for all popups and will open other defects. 
		  If need be, please use specific window sizes such as windowSm, windowLrg etc. and pass them as arguments to this function.*/		
		parameters = "location=" + settings.location + ",menubar=" + settings.menubar + ",height=" + settings.height + ",width=" + settings.width + ",toolbar=" + settings.toolbar + ",scrollbars=" + settings.scrollbars  + ",status=" + settings.status + ",resizable=" + settings.resizable + ",left=" + settings.left  + ",screenX=" + settings.left + ",top=" + settings.top  + ",screenY=" + settings.top;

		var name = settings.createnew ? "PopUpWindow" + index : "PopUpWindow";
		//window.open(url, name, parameters).focus(); 
		
		var opened = window.open(settings.url, name, parameters);
		opened.resizeTo(settings.width + 8, settings.height + 80);
		opened.focus();
		
		index = index + 1;
}

function vsPopUp_setup(url, profile)
{
	var settings, parameters, mysettings, b, a;
		
		// for overrideing the default settings
		mysettings = profile.split(",");
		settings = {
			height:500, // sets the height in pixels of the window.
			width:380, // sets the width in pixels of the window.
			toolbar:0, // determines whether a toolbar (includes the forward and back buttons) is displayed {1 (YES) or 0 (NO)}.
			scrollbars:0, // determines whether scrollbars appear on the window {1 (YES) or 0 (NO)}.
			status:0, // whether a status line appears at the bottom of the window {1 (YES) or 0 (NO)}.
			resizable:0, // whether the window can be resized {1 (YES) or 0 (NO)}. Can also be overloaded using resizable.
			left:20, // left position when the window appears.
			top:20, // top position when the window appears.
			center:1, // should we center the window? {1 (YES) or 0 (NO)}. overrides top and left
			createnew:1, // should we create a new window for each occurance {1 (YES) or 0 (NO)}.
			location:0, // determines whether the address bar is displayed {1 (YES) or 0 (NO)}.
			menubar:0 // determines whether the menu bar is displayed {1 (YES) or 0 (NO)}.
		};

		// if mysettings length is 1 and not a value pair then assume it is a profile declaration
		// and see if the profile settings exists
		if(mysettings.length == 1 && mysettings[0].split(":").length == 1)
		{
			a = mysettings[0];
			// see if a profile has been defined
			if(typeof (profiles[a]) != "undefined")
			{
				settings = jQuery.extend(settings, profiles[a]);
			}
		}
		else
		{
			// overrides the settings with parameter passed in using the rel tag.
			for(var i=0; i < mysettings.length; i++)
			{
				b = mysettings[i].split(":");
				
				if((typeof (settings[b[0]]) != 'undefined') && b.length == 2)
				{
					settings[b[0]] = b[1];
				}
			}
		}

		// center the window
		if (settings.center == 1)
		{
			settings.top = (screen.height-(settings.height + 110))/2;
			settings.left = (screen.width-settings.width)/2;
		}
		 	
		parameters = "location=" + settings.location + ",menubar=" + settings.menubar + ",height=542,width=500,toolbar=" + settings.toolbar + ",scrollbars=" + settings.scrollbars  + ",status=" + settings.status + ",resizable=" + settings.resizable + ",left=" + settings.left  + ",screenX=" + settings.left + ",top=" + settings.top  + ",screenY=" + settings.top;

		var name = settings.createnew ? "PopUpWindow" + index : "PopUpWindow";
		window.open(url, name, parameters).focus(); 
		index = index + 1;
}
