﻿/*
 * LETZTE AENDERUNGEN VON EVENTIM:
 *		2009-08-13 18:30	In der Funktion popupwindow() settings um den Parameter "name" erweitert, um individuelle
 *							Namen per Profile vergeben zu koennen (hans-peter.beck@eventim.de).
 *
 * Copyright 2008-2009 by CTS Eventim AG.
 */

/*
Popupwindow plugin for jQuery.
by: Tony Petruzzi
homepage: http://rip747.wordpress.com
plugin download: http://rip747.wordpress.com/2007/03/02/the-return-of-popupwindow-jquery-plugin/

************************************************************************************

NOTE: the index.html file included in this package gives example on how
to us the plugin and the different options available.

************************************************************************************
  
Takes a link and will create a popupwindow based on the href of the link. You can
over ride the default setting by passing your own settings or profile name in the
REL attribute of the link.
   
To use just include the plugin in the HEAD section of the page AFTER calling jQuery.
After that, use jQuery to find the links you want and pass any parameters you want

04/04/2007:

1) added profiles so you don't have to pass the settings for each link anymore.
2) remove resize as a setting and add the correct setting resizable
3) removed example text from this file and made an index.htm files to house example.
4) add example of using profiles to the new examples page.
5) example pulls the latest jquery library from jquery.com.

05/14/2007

1) removed trailing comma in settings that was causing IE to bottom out with an error.

01/21/2008

1) added new setting "createnew" which when set to false will make all popups open in the same window
2) fixed a major bug where "settings" wasn't vared.

02/13/2008

1) added location and menubar settings as suggested by Matthew

02/20/2008

1) fixed bug: commas were missing in front of menubar and height attribute
*/

jQuery.fn.popupwindow = function(p) {

	var profiles = p || {};

	return this.each(function(index){
		
		var settings, parameters, mysettings, b, a;
		
		// for overrideing the default settings
		mysettings = (jQuery(this).attr("rel") || "").split(",");

		
		settings = {
			height:600, // sets the height in pixels of the window.
			width:568, // 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:0, // whether the window can be resized {1 (YES) or 0 (NO)}. Can also be overloaded using resizable.
			left:0, // left position when the window appears.
			top:0, // 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)}.
			name:false // custom name for new window, default false.
		};

		// 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=" + 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;
		
		jQuery(this).bind("click", function(){
			
			/**
			 * PATCH START
			 *
			 * DO NOT DELETE THE LINES BELOW, ASK HAPE BEFORE!
			 * Giving the name of the parent window, if name not exist, create ...
			 */
			 
			/** Get the href of the URL. "this.getAttribute('ref')" is used if "this.href" not work */
			var tmpHref = (this.href || this.getAttribute('ref')) || ''; 
			
			/**
			 * Wenn ein Popup aus einem Popup heraus aufgerufen wird, dann sollte
			 * der urspruengliche Parent der das erste Popup aufrief weitergegeben
			 * werden, damit das neue Popup dorthin referenziert.
			 * 
			 * Und folgend des Quellpopup des neuen Popups geschlossen werden, da
			 * zuviele Popups ja auch nicht gut sind!
			 */
			if ((window.isPopup || false) == false) {
				
				/** Get secure that the current window has a name ... */
				if((window.name || null) == null) window.name='PopUpWindowParent';
				var parentWindowname = window.name;
				
			} else {
				
				var parentWindowname = window.parentSource;
			}
			
			/** Adding the Window-name to the link as "popupSource". The Popups now know their parent ... */
			tmpHref += ((tmpHref.indexOf('?') > 0) ? '&' : '?') + 'popupSource=' + parentWindowname;
		
			/** Create name-Attribute for popup-window */
			var name = ((settings.name) ? settings.name : ((settings.createnew) ? "PopUpWindow" + index : "PopUpWindow") );
			
			/** Create Popup-Window */
			// Im IE gibt es seit Juli 2009 Probleme mit dem Zugriff auf Fenster und deren Werte,
			// daher wurde hier nun die URL erst spaeter gesetzt, statt gleich im open-Befehl
			// angegeben.
			var newWindow = window.open('', name, parameters);
			newWindow.location.href = tmpHref;
			
			/** Set some Information in the popup-window, for later use */
			newWindow.isPopup = true;
			newWindow.parentSource = parentWindowname;
			
			document.cookie = "parentSource_"+name+"="+parentWindowname;
			
			/**
			 * Wenn die CSS-Klasse closewindow angegeben wurde oder die 
			 * Quelle des neuen Popups ein Popup war und das neue Popup
			 * ungleich dem alten ist, dann schliesse das alte, um
			 * Popup-Chaos zu vermeiden!
			 */
			if ((jQuery(this).hasClass('closewindow') || (window.isPopup || false) == true) && name != window.name) window.close();
			
			/** Focus the new window */
			newWindow.focus();
			
			/** PATCH END */
			
			return false;
		});
	});
};

var profiles =
{

	tellAFriend:
	{
		height:645,
		width:509
	},
	
	forgotPassword:
	{
		height:400,
		width:400
	},

	window800:
	{
		height:800,
		width:800,
		status:1
	},

	helpContactWindow:
	{
		height:600,
		width:750,
		status:1
	},
	
	promotion:
	{
		height:170,
		width:400,
		status:1
	},

	window600:
	{
		height:600,
		width:600,
		status:1
	},

	window500:
	{
		height:500,
		width:500,
		status:1
	},
	
	window400:
	{
		height:400,
		width:400,
		status:1
	},

	window200:
	{
		height:200,
		width:200,
		status:1,
		resizable:0
	},

	windowCenter:
	{
		height:300,
		width:400,
		center:1
	},
	
	windowNotNew:
	{
		height:300,
		width:400,
		center:1,
		createnew:0
	},
	
	fanReportConditions:
	{
		height:600,
		width:525,
		scrollbars:1
	},
	
	fanReportExample:
	{
		height:550,
		width:750,
		scrollbars:1
	},
	
	lotteryConditions:
	{
		height:450,
		width:500,
		scrollbars:1
	},
	
	tellAFriend :
	{
		
		height:660,
		width:600,
		center:1,
		scrollbars:1
	},
	
	onlineFeedbackStep1 :
	{
		
		height:240,
		width:600,
		center:1,
		scrollbars:1
	},
	
	window1024x600 :
	{
		
		height:600,
		width:1024,
		center:1
	},
	
	windowNoStatus : 
	{
		
		height:600,
		width:600,
		center:1,
		status:0,
		scrollbars:1
	},
	
	seatingChart : 
	{
		
		height:642,
		width:817,
		center:1,
		status:0,
		scrollbars:1,
		resizable:0,
		location:0,
		menubar:0,
		name:'Saalplan'
	},
	
	googleMaps : 
	{
		
		height:500,
		width:800,
		center:1,
		status:0,
		scrollbars:0,
		resizable:1,
		location:0,
		menubar:0,
		name:'googleMaps'
	}

};


jQuery(function() {
				
	jQuery(".popupwindow").popupwindow(profiles);
});
