/*
 * Smoothbox v20070814 by Boris Popoff (http://gueschla.com)
 *
 * Based on Cody Lindley's Thickbox, MIT License
 *
 * Licensed under the MIT License:
 *   http://www.opensource.org/licenses/mit-license.php
 */

// on page load call TB_init
window.addEvent('domready', TB_init);

// prevent javascript error before the content has loaded
TB_WIDTH = 380;
TB_HEIGHT = 240;
var TB_doneOnce = 0 ;
var TB_frm = false;
var TB_confirmed = false;
// %email% will be replaced with the email address provided in the form
/*var TB_displayText = '<div>Just to be safe, please confirm that this is your email address:' +
	'<p style="text-align: center;"><input type="text" name="confirm_email" value="%email%" id="confirm_email" class="email"/></p>'+
	'<table border="0" width="100%"><tr><td colspan="2"><p><label for="captcha">Please type this security code into the box to verify your subscription</p></td></tr>'+
	'<tr><td width="50%" align="right" style="height: 35px;"><img src="'+ ngemail_base +'captcha.php" alt="CAPTCHA" title="Security Code" style="margin: 0;" /></td>'+
	'<td align="left"><input type="text" id="captcha" value="" style="width: 80px;" class="email" /></td></tr></table><br />'+
	'<input type="image" title="subscribe" id="go" alt="Sign up" src="http://www.trendy.in/wp-content/themes/trendylicious/images/button2.png" name="subscribe" class="button" onclick="TB_submit();" />';
	//'<a href="javascript: TB_submit();">That is Correct &raquo;</a></div>';*/

var TB_displayText = '<div>Just to be safe, please confirm that this is your email address:' +
	'<p style="text-align: center;"><input type="text" name="confirm_email" value="%email%" id="confirm_email" class="email"/></p>'+
	'<div>Please retype your email address below:'+
	'<p style="text-align: center;"><input type="text" name="confirm_email_2" value="" id="confirm_email_2" class="email"/></p>'+
	'<input type="image" title="subscribe" id="go" alt="Sign up" src="http://www.trendy.in/wp-content/themes/trendylicious/images/button2.png" name="subscribe" class="button" onclick="TB_submit();" />';

// add smoothbox to href elements that have a class of .smoothbox
function TB_init(){
	$$("a.smoothbox").each(function(el){el.onclick=TB_bind});
	
	if ($('subform')) {
		TB_frm = $('subform');
	} else if (document.icpsignup) {
		TB_frm = document.icpsignup;
	}
}

function TB_bind(event) {
	var event = new Event(event);
	// stop default behaviour
	event.preventDefault();
	// remove click border
	this.blur();
	// get caption: either title or name attribute
	var caption = this.title || this.name || "";
	// get rel attribute for image groups
	var group = this.rel || false;
	// display the box for the elements href
	TB_show(caption, this.href, group);
	this.onclick=TB_bind;
	return false;
}


// called when the user clicks on a smoothbox link
function TB_show(caption) {
	// create iframe, overlay and box if non-existent

	if ( !$("TB_overlay") )
	{
		new Element('div').setProperty('id', 'TB_overlay').injectInside(document.body);
		$('TB_overlay').setOpacity(0);
		TB_overlaySize();
		new Fx.Style('TB_overlay', 'opacity',{duration: 400, transition: Fx.Transitions.sineInOut}).start(0,0.6);
	}
	
	if ( !$("TB_window") )
	{
		new Element('div').setProperty('id', 'TB_window').injectInside(document.body);
		$('TB_window').setOpacity(0);
	}
	
	$("TB_overlay").onclick=TB_remove;
	window.onscroll=TB_positionEffect;

	TB_WIDTH += 30;
	TB_HEIGHT += 40;

	var ajaxContentW = TB_WIDTH - 30, ajaxContentH = TB_HEIGHT - 45;
	var txt = TB_displayText;
	
	if (TB_frm.email) {
		txt = txt.replace('%email%', TB_frm.email.value);
	} else if (TB_frm.fields_email) {
		txt = txt.replace('%email%', TB_frm.fields_email.value);
	}
		
	$("TB_window").innerHTML += "<div id='TB_title'><div id='TB_ajaxWindowTitle'>"+caption+"</div><div id='TB_closeAjaxWindow'><a href='#' id='TB_closeWindowButton'>close</a></div></div><div id='TB_ajaxContent' style='width:"+ajaxContentW+"px;height:"+ajaxContentH+"px;'></div>";
	$("TB_closeWindowButton").onclick = TB_remove;
	$("TB_ajaxContent").innerHTML = (txt);
	TB_position();
	TB_showWindow();

	window.onresize=function(){ TB_position(); TB_load_position(); TB_overlaySize();}  
	
	document.onkeyup = function(event){ 	
		var event = new Event(event);
		if(event.code == 27){ // close
			TB_remove();
		}	
	}
	
	return false;
}

//helper functions below

function TB_showWindow(){
	//$("TB_load").remove();
	//$("TB_window").setStyles({display:"block",opacity:'0'});
	
	if (TB_doneOnce==0) {
		TB_doneOnce = 1;
		var myFX = new Fx.Style('TB_window', 'opacity',{duration: 250, transition: Fx.Transitions.sineInOut, onComplete:function(){if ($('TB_load')) { $('TB_load').remove();}} }).start(0,1);
	} else {
		$('TB_window').setStyle('opacity',1);
	}
}

function TB_remove() {
 	$("TB_overlay").onclick=null;
	document.onkeyup=null;
	document.onkeydown=null;
	
	if ($('TB_closeWindowButton')) $("TB_closeWindowButton").onclick=null;

	new Fx.Style('TB_window', 'opacity',{duration: 250, transition: Fx.Transitions.sineInOut, onComplete:function(){$('TB_window').remove();} }).start(1,0);
	new Fx.Style('TB_overlay', 'opacity',{duration: 400, transition: Fx.Transitions.sineInOut, onComplete:function(){$('TB_overlay').remove();} }).start(0.6,0);

	window.onscroll=null;
	window.onresize=null;	
	
	TB_init();
	TB_doneOnce = 0;
	return false;
}

function TB_position() {
	$("TB_window").setStyles({width: TB_WIDTH+'px', 
				 left: (window.getScrollLeft() + (window.getWidth() - TB_WIDTH)/2)+'px',
				 top: (window.getScrollTop() + (window.getHeight() - TB_HEIGHT)/2)+'px'});
}

function TB_positionEffect() {
	new Fx.Styles('TB_window', {duration: 75, transition: Fx.Transitions.sineInOut}).start({
		'left':(window.getScrollLeft() + (window.getWidth() - TB_WIDTH)/2)+'px',
		'top':(window.getScrollTop() + (window.getHeight() - TB_HEIGHT)/2)+'px'});
}

function TB_overlaySize(){
	// we have to set this to 0px before so we can reduce the size / width of the overflow onresize 
	$("TB_overlay").setStyles({"height": '0px', "width": '0px'});
	$("TB_overlay").setStyles({"height": window.getScrollHeight()+'px', "width": window.getScrollWidth()+'px'});
}

function TB_load_position() {
	if ($("TB_load")) { $("TB_load").setStyles({left: (window.getScrollLeft() + (window.getWidth() - 56)/2)+'px', top: (window.getScrollTop() + ((window.getHeight()-20)/2))+'px',display:"block"}); }
}

function TB_submit() {
	var email1 = $('confirm_email').value;
	var email2 = $('confirm_email_2').value;
	
	if (email1 != email2) {
		TB_confirmed = false;
		alert("The emails did not match. Please try again.");
	} else {
		TB_confirmed = true;
		if (TB_frm.email) {
			TB_frm.email.value = $('confirm_email').value;
		} else if (TB_frm.fields_email) {
			TB_frm.email.value = $('confirm_email').value;
		}
		TB_frm.submit();
	}
	
	/*new Ajax(ngemail_base+'confirm_email.php', {
				method: 'post',
				postBody: 'cmd=captcha&code='+$('captcha').value,
				onComplete:function(rsp){
					var data = Json.evaluate(rsp);
					
					if (data.valid) {
						TB_confirmed = true;
						if (TB_frm.email) {
							TB_frm.email.value = $('confirm_email').value;
						} else if (TB_frm.fields_email) {
							TB_frm.email.value = $('confirm_email').value;
						}
						TB_frm.submit();
					} else {
						TB_confirmed = false;
						alert("Invalid security code");
					}
				}.bind(this)
	}).request();*/
}

window.extend({
	getScrollHeight : function() {
		if (this.ie) return Math.max(document.documentElement.offsetHeight, document.documentElement.scrollHeight);
		if (this.webkit) return document.body.scrollHeight;
		return document.documentElement.scrollHeight;
	},
	getScrollWidth: function(){
		if (this.ie) return Math.max(document.documentElement.offsetWidth, document.documentElement.scrollWidth);
		if (this.webkit) return document.body.scrollWidth;
		return document.documentElement.scrollWidth;
	},
	getScrollLeft: function(){
		return this.pageXOffset || document.documentElement.scrollLeft;
	},
	getScrollTop: function(){
		return this.pageYOffset || document.documentElement.scrollTop;
	}
});

Fx.Slide = Fx.Base.extend({
	options: {
		mode: 'vertical'
	},

	initialize: function(el, options){
		this.element = $(el);
		this.wrapper = new Element('div', {'styles': $extend(this.element.getStyles('margin'), {'overflow': 'hidden'})}).injectAfter(this.element).adopt(this.element);
		this.element.setStyle('margin', 0);
		this.setOptions(options);
		this.now = [];
		this.parent(this.options);
		this.open = true;
		this.addEvent('onComplete', function(){
			this.open = (this.now[0] === 0);
		});
		if (window.webkit419) this.addEvent('onComplete', function(){
			if (this.open) this.element.remove().inject(this.wrapper);
		});
	},

	setNow: function(){
		for (var i = 0; i < 2; i++) this.now[i] = this.compute(this.from[i], this.to[i]);
	},

	vertical: function(){
		this.margin = 'margin-top';
		this.layout = 'height';
		this.offset = this.element.offsetHeight;
	},

	horizontal: function(){
		this.margin = 'margin-left';
		this.layout = 'width';
		this.offset = this.element.offsetWidth;
	},

	/*
	Property: slideIn
		Slides the elements in view horizontally or vertically.

	Arguments:
		mode - (optional, string) 'horizontal' or 'vertical'; defaults to options.mode.
	*/

	slideIn: function(mode){
		this[mode || this.options.mode]();
		return this.start([this.element.getStyle(this.margin).toInt(), this.wrapper.getStyle(this.layout).toInt()], [0, this.offset]);
	},

	/*
	Property: slideOut
		Sides the elements out of view horizontally or vertically.

	Arguments:
		mode - (optional, string) 'horizontal' or 'vertical'; defaults to options.mode.
	*/

	slideOut: function(mode){
		this[mode || this.options.mode]();
		return this.start([this.element.getStyle(this.margin).toInt(), this.wrapper.getStyle(this.layout).toInt()], [-this.offset, 0]);
	},

	/*
	Property: hide
		Hides the element without a transition.

	Arguments:
		mode - (optional, string) 'horizontal' or 'vertical'; defaults to options.mode.
	*/

	hide: function(mode){
		this[mode || this.options.mode]();
		this.open = false;
		return this.set([-this.offset, 0]);
	},

	/*
	Property: show
		Shows the element without a transition.

	Arguments:
		mode - (optional, string) 'horizontal' or 'vertical'; defaults to options.mode.
	*/

	show: function(mode){
		this[mode || this.options.mode]();
		this.open = true;
		return this.set([0, this.offset]);
	},

	/*
	Property: toggle
		Slides in or Out the element, depending on its state

	Arguments:
		mode - (optional, string) 'horizontal' or 'vertical'; defaults to options.mode.

	*/

	toggle: function(mode){
		if (this.wrapper.offsetHeight == 0 || this.wrapper.offsetWidth == 0) return this.slideIn(mode);
		return this.slideOut(mode);
	},

	increase: function(){
		this.element.setStyle(this.margin, this.now[0] + this.options.unit);
		this.wrapper.setStyle(this.layout, this.now[1] + this.options.unit);
	}
});
