var Page = {
	initialize: function()
	{
		// Set up the Make this my homepage link
		var link = $('make-home');
		
		if ( link )
		{
			if ( !Prototype.Browser.IE )
			{
				link.hide();
			}
			else
			{
				link.observe('click', Page.makeHome);
			}
		}
	},
	
	makeHome: function(e)
	{
		var link = Event.findElement(e, 'a');
		
		link.style.behavior = 'url(#default#homepage)';
		link.setHomePage(location.href);
	},
	
	getForm: function(id)
	{
		new Ajax.Request("http://www.seventeen.co.za/plugins/get_form.php?id="+id, {
			onSuccess: Page.showForm
		});
	},
	
	showForm: function(transport) {
		
		var html = transport.responseText;
		
		Pox.showHTML(document.body, html);
	},
	
	viewForm: function(id) {
		var win = window.open("http://www.seventeen.co.za/tools/forms/view.php?id="+id, null, "status=yes,toolbar=no,menubar=no,location=no,resizable=yes,scrollbars=yes,width=400");
		return false;
	},
	
	pagination: function() {
		if ($('previous'))
		{
			var prev = $$("a.pager-previous");
			$('previous').href = prev;
		}
		if ($('next'))
		{
			var next = $$("a.pager-next");
			$('next').href = next;
		}
		
	}
}

// Initialize the page
document.observe('dom:loaded', Page.initialize);

var FocusImageBlock = Class.create({
	
	images: null,
	contents: null,
	current: 0,
	locked: false,
	mouse_over_timer: null,
	mouse_move_timer: null,
	
	initialize:function(element, first)
	{
		// Extend the element or get the element if the ID is passed
		element = $(element);
		
		// Determine what the first item to be shown is
		first = first || 0;
		
		this.current = first;
		
		// Get the images
		this.images = element.select("div.latest-images img");
		
		// Get the contents
		this.contents = element.select("div.latest-item-outer");
		
		// Add the mouse over listeners to the content and do initial styling
		var content;
		
		for ( var i = 0, length = this.contents.length; i < length; i++ )
		{
			content = this.contents[i];
			
			content.observe('mouseover', this.mouseOver.bind(this, i));
			
			if ( first == i )
			{
				content.addClassName('latest-item-outer-selected');
				content.removeClassName('latest-item-outer');
				
				this.images[i].show();
			}
			else
			{
				this.images[i].hide();
			}
		}
	},
	
	mouseOver: function(index, e)
	{
		if ( this.locked )
		{
			setTimeout('60');
			return;
		}
		
		if ( index == this.current )
		{
			return;
		}
		else
		{
			// Swop the class names on the indexed content
			this.locked = true;
			this.contents[index].addClassName('latest-item-outer-selected');
			this.contents[index].removeClassName('latest-item-outer');
			
			// Swop the class names on the current content
			this.contents[this.current].addClassName('latest-item-outer');
			this.contents[this.current].removeClassName('latest-item-outer-selected');
			
			// Fade the old image out and the new image in
			new Effect.Fade(this.images[this.current], {duration: 0.25});
			new Effect.Appear(this.images[index], {duration: 0.25, afterFinish: this.unlock.bind(this)});
			
			// Set the current content
			this.current = index;
		}
		
	},
	
	unlock: function()
	{
		this.locked = false;
	}
								 
});

var LoadStyles = Class.create({
	pink: {
		logo: "http://www.seventeen.co.za/images/seventeen/logo_pink.gif",
		left_bg_pos: "0px 0px",
		right_bg_pos: "-24px 0px",
		class_name: "pink"
	},
	
	red: {
		logo: "http://www.seventeen.co.za/images/seventeen/logo_red.gif",
		left_bg_pos: "-114px 0px",
		right_bg_pos: "-138px 0px",
		class_name: "red"
	},
	
	blue: {
		logo: "http://www.seventeen.co.za/images/seventeen/logo_blue.gif",
		left_bg_pos: "-57px 0px",
		right_bg_pos: "-81px 0px",
		class_name: "blue"
	},
	
	green: {
		logo: "http://www.seventeen.co.za/images/seventeen/logo_green_03.png",
		left_bg_pos: "-171px 0px",
		right_bg_pos: "-195px 0px",
		class_name: "green"
	},
	
	current: null,
							  
	initialize: function(element, style)
	{
		// Get the element
		element = $(element);
		
		// If the style is not passed set the default
		style = style || 0;
		
		// Get the style selector images
		var selectors = element.select("img");
		
		// Add listeners to the selector images
		for ( var i = 0, length = selectors.length; i < length; i++ )
		{
			selectors[i].observe('click', this.setSelectedStyle.bind(this, i));
		}
		
		// Set the styles
		this.setSelectedStyle(style);
	},
	
	setSelectedStyle: function(style)
	{
		// Error checking
		if ( style == undefined )
		{
			return;
		}
		
		style = parseInt(style);
		
		// Make sure the value is within the correct range
		if ( style < 0 || style > 3 )
		{
			return;
		}
		
		// Decide which style to show
		switch ( style )
		{
			case 0:
				this.loadStyle(this.pink);
				break;
			case 1:
				this.loadStyle(this.red);
				break;
			case 2:
				this.loadStyle(this.blue);
				break;
			case 3:
				this.loadStyle(this.green);
				break;
		}
		
		// Store this value to reload the custom style later
		Cookie.set("seventeen_style", style, 30);
	},
	
	loadStyle: function(styles)
	{
		// Change the imagery
		$('seventeen-logo').src = styles.logo
		$('menu-left').setStyle({backgroundPosition: styles.left_bg_pos});
		$('menu-right').setStyle({backgroundPosition: styles.right_bg_pos});
		
		// Get all the menu items
		var elements = ( Element.select ) ? $('menu').select("a") : $('menu').select("a");
		
		for ( var i = 0, length = elements.length; i < length; i++ )
		{
			element = elements[i];
			
			// Check that a current is stored, this is generally for first load
			if ( this.current != null )
			{
				// Remove the old class
				element.removeClassName(this.current.class_name);
			}
			
			// Add the new class
			element.addClassName(styles.class_name);
		}
		
		// Change the container background
		var menu_container = $('menu-container');
		
		// Remove the old class
		if ( this.current != null )
		{
			menu_container.removeClassName(this.current.class_name + '-bg');
		}
		
		// Add the new class
		menu_container.addClassName(styles.class_name + '-bg');
		
		// Store the new styles
		this.current = styles;
	}
});

var Images = {
	transparent_image: "http://www.seventeen.co.za/images/mw/spacer.gif",
	
	fixAll: function()
	{
		// Check if the browser is IE 5.5 or IE 6
		if ( !Prototype.Browser.IE && !Prototype.Browser.IE5_5 && !Prototype.Browser.IE6 )
		{
			return;
		}
		
		// If there is a player on the page run again on load to fix all images after the player
		if ( $$('object').length > 0 )
		{
			Event.observe(window, 'load', Images.fixAll);
		}
		
		// Get all images
		$$('img').each(Images.fix);
	},
	
	fix: function(image)
	{
		if ( !Prototype.Browser.IE5_5 && !Prototype.Browser.IE6 )
		{
			return;
		}
		
		image = $(image);
	
		var filename = image.readAttribute('src');
		if ( filename.toLowerCase().endsWith('png') )
		{
			var opacity = image.getStyle('opacity');
			
			image.setStyle({
				filter: 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="' + filename + '", sizingMethod="none")'
			});
			image.writeAttribute({
				src: Images.transparent_image
			});
			
			if ( opacity != 1 )
			{
				image.setStyle({opacity: opacity});
			}
		}
	}
}

var Slideshow = Class.create({
	seconds: null,
	
	images: null,
	timer: null,
	current: null,
	running: false,
	locked: false,
	
	initialize: function(container, seconds)
	{
		container = $(container);
		this.seconds = seconds || 10;
		
		this.images = container.select('.image-holder');
		
		var max_height = Util.getMaxHeight(this.images);
		
		container.setStyle({
			position: 'relative'
		});
		
		this.images.each( function(element) {
			element.setStyle({
				position: 'absolute',
				display: 'none'
			});
		});
		
		this.images[0].show();
		this.current = 0;
		
		// Attach listeners
		$('slideshow-previous').observe('click', this.previous.bind(this));
		$('slideshow-play').observe('click', this.toggle.bind(this));
		$('slideshow-next').observe('click', this.next.bind(this));	
		
		this.play();	
	},
	
	play: function()
	{
		this.timer = setInterval(this.next.bind(this), this.seconds * 1000);
		$('slideshow-play').src = "http://www.genderlinks.org.za/images/gmdc_pause_button.png";
		Images.fix('slideshow-play');
		this.running = true;
	},
	
	pause: function()
	{
		clearInterval(this.timer);
		$('slideshow-play').src = "http://www.genderlinks.org.za/images/gmdc_play_button.png";
		Images.fix('slideshow-play');
		this.running = false;
	},
	
	toggle: function()
	{
		if ( !this.running )
		{
			this.play();
		}
		else
		{
			this.pause();
		}
	},
	
	previous: function()
	{
		if ( this.current == 0 )
		{
			var previous = this.images.length - 1;
		}
		else
		{
			var previous = this.current - 1;
		}
		
		this.effects(this.current, previous);
	},
	
	next: function()
	{
		if ( this.current == (this.images.length - 1) )
		{
			var next = 0;
		}
		else
		{
			var next = this.current + 1;
		}
		
		this.effects(this.current, next);
	},
	
	effects: function(before, after)
	{
		if ( this.locked )
		{
			return;
		}
		
		this.locked = true;
		
		new Effect.Fade(this.images[before], {duration: 0.75});
		new Effect.Appear(this.images[after], {duration: 0.75, afterFinish: this.unlock.bind(this)});
		
		this.current = after;
	},
	
	unlock: function()
	{
		this.locked = false;
	}
});

var NewsLetter = Class.create({
	EMAIL_TEXT: "enter email",
							  
	initialize: function(form)
	{
		// Extend the form
		form = $(form);
		
		// Add a submit listenter for form validation
		form.observe('submit', this.formSubmit.bind(this));
		
		// Get the email address input
		var email = form.getInputs('text', 'email')[0];
		
		// Add a click listener to clear the default text
		email.observe('click', this.emailClick.bind(this));
		
		// Add a blur listener to restore the default text
		email.observe('blur', this.emailBlur.bind(this));
	},
	
	emailClick: function(e)
	{
		// Stop the event propagation
		Event.stop(e);
		
		// Get the email input from the event
		var email = Event.findElement(e, 'input');
		
		// Check if the text is the default text
		if ( $F(email) == this.EMAIL_TEXT )
		{
			email.value = "";
		}
	},
	
	emailBlur: function(e)
	{
		// Stop the event propagation
		Event.stop(e);
		
		// Get the email input from the event
		var email = Event.findElement(e, 'input');
		
		// Get the current field value
		var value = $F(email);
		
		// Restore the default text if the field is empty
		if ( value == null || value.length == 0 )
		{
			email.value = this.EMAIL_TEXT;
		}
	},
	
	formSubmit: function(e)
	{
		// Stop the event propagation
		Event.stop(e);
		
		// Get the form where the event was triggered
		var form = Event.findElement(e, 'form');
		
		// Get the submit button
		var button = form.down('input[type=submit]');
		
		// Stop the button's onclick action
		if ( button.onclick )
		{
			button.onclick(null);
		}
		
		// Get the email address input
		var email = form.getInputs('text', 'email')[0];
		
		// Validate the email address
		if ( !this._validEmail($F(email)) )
		{
			alert('The email address that you entered is not valid, please check the email address.');
			return false;
		}
		
		form.submit();
	},
	
	_validEmail: function(value)
	{
		return ( value != null ) && ( value.length != 0 ) && /\w{1,}[@][\w\-]{1,}([.]([\w\-]{1,})){1,3}$/.test(value);
	}
});	
function popUpForm(id) {
	var w = document.viewport.getWidth();
	width = (w/2)-200;
//window.open('http://www.seventeen.co.za/tools/forms/view.php?id=' + id,"_blank", 'form', 'scrollbars=yes,location=no,statusbar=yes,width=440,height=900,left = '+ width + ',top = 34');
var win = window.open("http://www.seventeen.co.za/tools/forms/view.php?id="+id, null, "status=yes,toolbar=no,menubar=no,location=no,resizable=yes,scrollbars=yes,width=440,height=850,left = "+ width + ",top = 34");
		//return false;

}
function popUpMagazine(url) {
	var w = document.viewport.getWidth();
	width = (w/2)-350;
	
	if ( url == undefined || url == '' )
	{
	    url = 'https://www.multi-mags.com/securesubs/suborderform.aspx?pr=SEVE&pe=236&t=12&ec=B63';
	}
	
	window.open(url,"_blank", 'form', 'scrollbars=1,location=0,statusbar=1,width=700,height=700,left = '+ width + ',top = 34');
}
function popUpPrint(id) {
	var w = document.viewport.getWidth();
	width = (w/2)-200;
window.open('http://www.genderlinks.org.za/print_version.php?a_id=' + id,"_blank", 'Print Version', 'scrollbars=1,location=0,statusbar=1,width=700,height=700,left = '+ width + ',top = 34');
}
function addUniversity(id)
{
	new Ajax.Updater($('varsity' + id), '/plugins/s.update.row.php?varsity=1&id=' + id, {insertion: Insertion.After});
	newid = id + 1;
	$('varsity-button').href = "javascript: addUniversity(" + newid + ");";
}
function addSubject(id)
{
	$('subject'+id).insert ({
         'after'  :
          $('subject'+id).innerHTML    
     });
}
function addIntern(id)
{
	new Ajax.Updater($('intern' + id), '/plugins/s.update.row.php?intern=1&id=' + id, {insertion: Insertion.After});
	newid = id + 1;
	$('intern-button').href = "javascript: addIntern(" + newid + ");";
}
function addLanguage(id)
{
	new Ajax.Updater($('language' + id), '/plugins/s.update.row.php?language=1&id=' + id, {insertion: Insertion.After});
	newid = id + 1;
	$('language-button').href = "javascript: addLanguage(" + newid + ");";
}
function clearText(el) 
{
	if (el.defaultValue == el.value)
	{
		el.value = "";
	}
}