var JoinNowForm = new Class({
	Implements: [Options, Events],
	options: {
	},
	initialize: function(options) {
		this.setOptions(options);
			$$('.join .button a').addEvent('click', this.joinNowClick.bind(this));
			$$('.join .back a').addEvent('click', this.backClick.bind(this));
			$$('.join .item1 a').addEvent('click', this.item1Click.bind(this));
			$$('.join .item2 a').addEvent('click', this.item2Click.bind(this));
	},
	gotoStep1: function() {
		$$('.join .question span').set('html', "CHOOSE YOUR PAYMENT METHOD:");
		$$('.join .back a').set('html', "");
		$$('.join .item1 a').set('html', "<span></span>PAY BY CREDIT CARD");
		$$('.join .item2 a').set('html', "<span></span>NO CREDIT CARD?");
		
		this.item1_checked = true;
		this.item2_checked = false;
		$$('.join .item1 span').removeClass('nothing');
		$$('.join .item2 span').addClass('nothing');
		this.step = 1;
	},
	gotoStep2: function() {
		$$('.join .question span').set('html', "STEP 2 OF 3<br />CHOOSE YOUR COUNTRY:");
		$$('.join .back a').set('html', "Back");
		$$('.join .item1 a').set('html', "<span></span>UNITED STATES <img src='images/flag-us.png' width='20' height='13' />");
		$$('.join .item2 a').set('html', "<span></span>EUROPEAN UNION <img src='images/flag-eu.png' width='20' height='13' />");
		
		this.item1_checked = true;
		this.item2_checked = false;
		$$('.join .item1 span').removeClass('nothing');
		$$('.join .item2 span').addClass('nothing');
		this.step = 2;
	},
	gotoStep3: function() {
		if (this.item1_checked) {
			this.step2_result = 1;
		} else {
			this.step2_result = 2;
		} 
		$$('.join .question span').set('html', "STEP 3 OF 3<br />CHOOSE YOUR PAYMENT METHOD:");
		if (this.step2_result == 1) {
			$$('.join .item1 a').set('html', "<span></span>ONLINE CHECK");
			$$('.join .item2 a').set('html', "<span></span>PHONE 0900");
		} else {
			$$('.join .item1 a').set('html', "<span></span>DIRECT PAY <img src='images/flag-eu.png' width='20' height='13' />");
			$$('.join .item2 a').set('html', "<span></span>EU DEBIT (DE, AT, NL)");
		}
		
		this.item1_checked = true;
		this.item2_checked = false;
		$$('.join .item1 span').removeClass('nothing');
		$$('.join .item2 span').addClass('nothing');
		this.step = 3;
	},
	backClick: function(event) {
		event.stop();
		switch (this.step) {
			case 1:
				break;
			case 2:
				this.gotoStep1();
				break;
			case 3:
				this.gotoStep2();
				break;
		}
	},
	joinNowClick: function(event) {
		event.stop();
		switch (this.step) {
			case 1:
				if (this.item1_checked) {
					// go to 
					window.open(this.options.credit_card);
				} else {
					this.gotoStep2();
				}
				break;
			case 2:
				this.gotoStep3();
				break;
			case 3:
				if (this.step2_result == 1) {
					// us
					if (this.item1_checked) {
						window.open(this.options.usa_check);
					} else {
						window.open(this.options.usa_phone);
					}
				} else {
					// eu
					if (this.item1_checked) {
						window.open(this.options.eu_directpay);
					} else {
						window.open(this.options.eu_debit);
					}
				}
				break;
		}

	},
	item1Click: function(event) {
		event.stop();
		if (!this.item1_checked) {
			if ($$('.join .item1 span').hasClass('nothing')) {
				$$('.join .item1 span').removeClass('nothing');
				$$('.join .item2 span').addClass('nothing');
				this.item1_checked = true;
				this.item2_checked = false;
			}
		}
		
	},
	item2Click: function(event) {
		event.stop();
		if (!this.item2_checked) {
			if ($$('.join .item2 span').hasClass('nothing')) {
				$$('.join .item1 span').addClass('nothing');
				$$('.join .item2 span').removeClass('nothing');
				this.item1_checked = false;
				this.item2_checked = true;
			}
		}

	},
	item1_checked: true,
	item2_checked: false,
	step: 1,
	step2_result: 0
});

var IntroSwitching = new Class({
	Implements: [Options, Events],
	options: {
	},
	initialize: function(options) {
		var self = this; 
		
		$$("#menu .active a").each(function(el, i) {
			el.addEvent('mouseenter', self.menuOver.bind(self));
		});
	},
	menuOver: function(event) {
		if (event.target.getParent().get('id').substr(4) == this.currentVisible)
			return;
		
			
		var div1 = $(this.currentVisible);
		$("over" + this.currentVisible).removeClass('over');
		if (div1) {
			new Fx.Morph(div1, {
				link: 'cancel'
			});
			div1.setStyles({
				display: 'block',
				opacity: 1
			});
			new Fx.Morph(div1, {
				duration: 300,
				onComplete: function(event){
				//event.setStyles({display:'none'});
				}
			}).start({
				'opacity': 0
			});
		}
		
		this.currentVisible =  event.target.getParent().get('id').substr(4);
		var div2 = $(this.currentVisible);
		$("over" + this.currentVisible).addClass('over');
		if (div2) {
			
			div2.setStyles({
				display: 'block',
				opacity: 0
			});
			new Fx.Morph(div2, {
				link: 'cancel'
			});
			new Fx.Morph(div2, {
				duration: 300
			}).start({
				'opacity': 1
			});
		}
	},
	currentVisible: "item1"
});

function inputEnter(element, val, leave) {
	if (leave == undefined) 
		leave = '';
	if (element.value == val) {
		element.value = leave;
	}
}

function inputLeave(element, val, leave) {
	if (leave == undefined) 
		leave = '';
	if (element.value == leave) {
		element.value = val;
	} else if (element.value == '') {
		element.value = val;
	}
}

function logIn(page) {
	window.location = "/join-now.html";	
}

window.addEvent('domready', function() {
	Element.implement({
		show: function() {
			this.setStyle('display','');
		},
		hide: function() {
			this.setStyle('display','none');
		}
	});
	
	Array.implement({
		shuffle: function() {
			//destination array
			for(var j, x, i = this.length; i; j = parseInt(Math.random() * i), x = this[--i], this[i] = this[j], this[j] = x);
			return this;
		}
	});
	
	// menu highlighting
	var items = {
		"item1": ['^/issue', '^/photo', '^/video', '^/model', '^/top-',
			'^/wallpapers', '^/e-cards', '^/jokes', '^/joke', '^/e-card', '^/members/issue'],
		"item2": ['^/cams'],
		"item3": ['^/magazine', '^/members/magazine'],
		"item4": ['^/about-us', '^/news', '^/our-team', '^/support', '^/reviews', 
			'^/news', '^/book', '^/dvd', '^/posters', '^/our-sites', '^/mark', '^/choze', '^/jiri', '^/tyna',
			'^/bara', '^/verca', '^/dan', '^/place-prague', '^/place-petersburg', '^/place-margarita', '^/place-singapore',
			'^/place-grancan', '^/place-tenerife', '^/place-domrep', '^/place-budapest', '^/places', '^/forum'],
		"item5": ['^/join-now', '^/pricing', '^/benefits'],
		"item6": ['^/free-']
	}
	
	if ($('menu')) {
		var url = window.location.href.substr("http://".length);
		url = url.substr(url.search("/"));
		
 		$each(items, function(value, key){
			for (i = 0; i < value.length; i++) {
				var re = new RegExp(value[i]);
				if (url.match(re)) {
					$$("#menu ." + key).addClass('selected');
					break;
				}
			}
		});
	}
});

function showMaximized(path) {
	var w=window;
	var width=screen.availWidth;
	var height=screen.availHeight;
	var props="width="+width+",height="+height+",top=0,left=0,scrollbars=yes,resizable=yes";
	var win = window.open(path, "w4bpopup", props);
	win.focus();
}