var Mobajl = Object();

Mobajl.Signup = new Class({
	initialize: function() {
		this.currentstep = 1;
		this.email = "";
		this.name = "";
		this.adress = "";
		this.city = "";
		this.zip = "";
		this.userid = 0;
		this.loadinglocked = false;
		this.citydisabled = true;
		this.interestscache = [];
		this.firstsignup = true;
		this.init();
		this.cba = new Mobajl.Checkbox('cba', 'cbabox', 'cbalabel');
//		this.expandSignupForm();
	},
	
	init: function() {
		$('step1button').addEvent('click', this.step1.bind(this));
		$('startform').addEvent('submit', this.step1.bind(this));
		$('ianswer').addEvent('keypress', this.submitOnEnter.bind(this));
		$('iemail').addEvent('keypress', this.submitOnEnter.bind(this));
		$('step2button').addEvent('click', this.step2.bind(this));
		$('signupform').addEvent('submit', this.step2.bind(this));

		$('izipcode').addEvent('keypress', this.checkZipInput.bind(this));
		$('izipcode').addEvent('keyup', this.checkZipLength.bind(this));
		$('icity').addEvent('keypress', this.checkCityInput.bind(this));

		$('iphone').addEvent('keypress', this.checkPhoneInput.bind(this));

		if ($('interest')) {
			var op = $('interest').options;
			for (var i = 0; i < op.length; i++) {
				this.interestscache[i] = {'value':op[i].value, 'name':op[i].text};
			}
			$('igender_male').addEvent('click', this.fixInterests.bind(this));
			$('igender_female').addEvent('click', this.fixInterests.bind(this));
		}

		$$('a').each(function(el) {
			if (el.get('rel') == 'termslink') {
				el.addEvent('click', this.termsLink.bind(this));
			}
		}, this);
	},
	
	submitOnEnter: function(e) {
		if (e.key == 'enter') {
			if ($('iemail').value.length == 0) {
				$('iemail').focus();
			} else {
				this.step1(e);
			}
		}
	},
	
	step1: function(e) {
		if (this.loadinglocked) {
			return;
		}
		$('iemail').value = $('iemail').value.replace(/ /gi,'');
		var iMsg = "";
		if ($('iemail').value.search(/^[-A-Za-z0-9_.]+[@][A-Za-z0-9_-]+([.][A-Za-z0-9_-]+)*[.][A-Za-z]{2,8}$/gi) == -1) {
			iMsg += alerts.email+"<br />";
		}
		if ($('ianswer').value.length < 2) {
			iMsg += alerts.answers+"<br />";
		}
		if (iMsg.length > 0) {
			makeAlert(alerts.errors, iMsg);
			return false;
		}

		var form = $('startform');
		form.handler = this;
		this.loadinglocked = true;

		form.set('send', {
 			onRequest: function(){
				signup.loading(1);
			},
			
			onSuccess: function(responseText, responseXML) {
				signup.loadinglocked = false;
				var result = JSON.decode(responseText);
				signup.loadingDone(1);
				if (result.success) {
					setGoal(1, 'step1');
					signup.expandSignupForm();
				} else {
//					signup.bad(1);
					signup.flagErrors(result.errors);
				}
			},

			onFailure: function() {
				this.handler.loadinglocked = false;
//				signup.bad(1);
			}
		});
		form.send();
		new Event(e).stop();
	},
	
	step2: function(e) {
		if (!this.checkForm()) {
			return false;
		}

		var form = $('iform');
		form.set('send', {url: baseurl+'ajax/step2', method: 'post',
 			onRequest: function(){
				signup.loading(2);
			},
			
			onSuccess: function(responseText, responseXML) {
				signup.loadingDone(2);
				var result = JSON.decode(responseText);
				if (result.success) {
					setGoal(1, 'step2');
					if (result.exitoffer) {
						$('exitofferlink').fireEvent('click');
					} else {
						window.location = baseurl + "invite/" + result.user.code;
					}
				} else {
					signup.flagErrors(result.errors);
				}
			},

			onFailure: function() {
				signup.loadingDone(2);
			}
		});
		this.citydisabled = false;
		form.send();		
		new Event(e).stop();
	},
	
	expandSignupForm: function() {
		$('step1button').setStyle('display', 'none');
		$('competitionstart').getElements('.inputboxfirst').each(function(element) {
			element.removeClass('inputboxfirst');
			element.getElement('.inputcontainer').addClass('disabledinputcontainer');
			element.getElement('input').disabled = true;
		});

		var form = $('competitionstep2');
		var inner = form.getElement('.innerwrapper');
		var innersize = inner.getSize();
//		inner.scrollTo(0,0);
	
		var pos = form.getPosition();
		new Fx.Scroll(window, {duration: 500, link: 'cancel', offset: {x: 0, y:innersize.y}}).start(0, pos.y);

		new Fx.Tween(form, {duration: 500, link: 'ignore'}).start('height', innersize.y + 'px');
		if (Browser.Engine.trident4) {
			fixIeShadows.delay(550);
		}	
		if (Browser.Engine.trident5) {
			$('footerornament').setStyle('display', 'none');
			var granfix = function () {
				$('footerornament').setStyle('bottom', '0px');
				$('footerornament').setStyle('display', 'block');
			}
			granfix.delay(550);
		}
		
	},
	
	fixInterests: function() {
		if (lang == 'sv') {
			var interests = $('interest');
			
			var interestcount = interests.options.length;
			var selected = 0;

			if (interestcount > 0) {
				selected = interests.options[interests.selectedIndex].value;
				for (var i = interests.options.length - 1; i >= 0; i--) {
					interests.remove(i);
				}
			}

			var ignore = -1;
			if ($('igender_male').checked) {
				ignore = 7;
			} else if ($('igender_female').checked) {
				ignore = 6;
			}

			if (selected == ignore) {
				selected = 0;
			}
			
			for (var i = 0; i < this.interestscache.length; i++) {
				
				if (this.interestscache[i].value != ignore) {
					var option = new Option(this.interestscache[i].name, this.interestscache[i].value);
					
					try {
						interests.add(option, null);
					} catch (err) {
						interests.add(option);
					}
					if (selected == this.interestscache[i].value) {
						interests.selectedIndex = i;
					}
				}
			}
		}
	},

	checkForm: function() {
		var errors = false;
		iMsg = "";
		var focus = false;
		if ($('iname').value.length < 2) {
			iMsg += alerts.name+"<br />\n";
			if (!focus) {
				focus = 'iname';
			}
		} else {
			var namesplit = $('iname').value.trim().split(" ");
			if (namesplit.length < 2) {
				iMsg += alerts.lastname+"<br />\n";
				if (!focus) {
					focus = 'iname';
				}
			}
		}

		var dateerrors = {year: false, month: false, day: false, fail: false};
		
		if ($('iyear').value == 9999) {
			dateerrors.year = alerts.year+'<br />\n';
			dateerrors.fail = true;
		}
	
		if ($('imonth').value == 99) {
			dateerrors.month = alerts.month+'<br />\n';
			dateerrors.fail = true;
		}
	
		if ($('iday').value == 99) {
			dateerrors.day = alerts.day+'<br />\n';
			dateerrors.fail = true;
		}
	
		if (dateerrors.fail) {
			if (lang == 'sv') {
				if (dateerrors.year) {
					iMsg += dateerrors.year;
					if (!focus)
						focus = 'iyear';
				}
				if (dateerrors.month) {
					iMsg += dateerrors.month;
					if (!focus)
						focus = 'imonth';
				}
				if (dateerrors.day) {
					iMsg += dateerrors.day;
					if (!focus)
						focus = 'iday';
				}
			} else {
				if (dateerrors.day) {
					iMsg += dateerrors.day;
					if (!focus)
						focus = 'iday';
				}
				if (dateerrors.month) {
					iMsg += dateerrors.month;
					if (!focus)
						focus = 'imonth';
				}
				if (dateerrors.year) {
					iMsg += dateerrors.year;
					if (!focus)
						focus = 'iyear';
				}
			}
		}
			
		if ($('iaddress').value.length < 2) {
			iMsg += alerts.address+"<br />\n";
			if (!focus) {
				focus = 'iaddress';
			}
		}

		if (!IsNumeric($('izipcode').value) || $('izipcode').value.length != zipcodemax) {
			iMsg += alerts.postcode+"<br />\n";
			if (!focus) {
				focus = 'izipcode';
			}
		}

		if ($('icity').value.length < 1) {
			iMsg += alerts.city+"<br />\n";
			if (!focus) {
				focus = 'icity';
			}
		}
	
		if (!$('igender_male').checked && !$('igender_female').checked) {
			iMsg += alerts.gender+"<br />\n";
			if (!focus) {
				focus = 'igender_male';
			}
		}
		
		$('iphone').value = $('iphone').value.replace(/[^0-9]/gi,'');
		if ($('iphone').value.length < cellphoneminlength) {
			iMsg += alerts.cellphone+"<br />\n";
			if (!focus) {
				focus = 'iphone';
			}
		} else if ($('iphone').value.search(/^000/) >= 0) {
			iMsg += alerts.cellphone+"<br />\n";
			if (!focus) {
				focus = 'iphone';
			}
		}
		
		if ($('interest')) {
			if ($('interest').selectedIndex == 0) {
				iMsg += alerts.interests + "<br />\n";
				if (!focus) {
					focus = 'interest';
				}
			}
		}

		if ($('cba').value != '1') {
			iMsg += alerts.agree+"<br />\n";
		}
		

		if (iMsg.length > 0) {
			var alert = makeAlert(alerts.errors, iMsg, focus);

			return false;
		}
		return true;
	},

	checkZipLength: function(e) {
		var zipcode = $('izipcode').value;
		if (zipcode.length == zipcodemax) {
			this.getCity();
		}

	},

	checkZipInput: function(e) {
		var zipcode = $('izipcode').value;
		var inputlength = zipcode.length;

		var keycode = (e.event.which) ? e.event.which : e.event.keyCode;

		var key = parseInt(e.key);
		if (isNaN(key) == false && key >= 0 && key <= 9) {
			inputlength++;
			if (inputlength > zipcodemax && $('izipcode').getSelectedText().length == 0) {
				new Event(e).stop();
			}		
		} else if ((typeof(e.meta) != 'undefined' && e.meta == true) || keycode == 8 || keycode == 9 || !(keycode > 31 && (keycode < 48 || keycode > 57))) {
		} else {
			new Event(e).stop();
		}
	},
	
	checkPhoneInput: function(e) {
		var phone = $('iphone').value;
		var inputlength = phone.length;
		var keycode = (e.event.which) ? e.event.which : e.event.keyCode;

		var key = parseInt(e.key);
		if (isNaN(key) == false && key >= 0 && key <= 9) {
			inputlength++;
			if (inputlength > cellphonelength && $('iphone').getSelectedText().length == 0) {
				new Event(e).stop();
			}		
		} else if ((typeof(e.meta) != 'undefined' && e.meta == true) || keycode == 8 || keycode == 9 || !(keycode > 31 && (keycode < 48 || keycode > 57))) {
		} else {
			new Event(e).stop();
		}
	},

	checkCityInput: function(e) {
		if (this.citydisabled) {
			if (e.event.charCode == 0) {
				if (e.event.keyCode != 9 && !(e.event.keyCode >= 33 && e.event.keyCode <= 39)) {
					new Event(e).stop();
				}
			} else {
				new Event(e).stop();
			}
		}
	},

	flagErrors: function(errors, focus) {
		iMsg = "";
		for (var i in errors) {
			iMsg += errors[i]+"<br />\n";
		}

		makeAlert(alerts.errors, iMsg, focus);
	},

	getCity: function() {
		this.citydisabled = true;
		var zipcode = $('izipcode').value;
		if (zipcode != this.zip && zipcode.length > 0) {
			$('icity').value = "";
			this.zip = zipcode;
			$('cityloading').setStyle('display', 'block');
			var jsonRequest = new Request.JSON({url: baseurl+"ajax/city", onComplete: function(result) {
				$('cityloading').setStyle('display', 'none');
				if (result.success) {
					this.city = result.city;
					$('icity').value = this.city;
					if (this.city.length == 0) {
						this.citydisabled = false;
						$('icity').focus();
					}
				} else {
					this.citydisabled = false;
					$('icity').focus();
				}
			}.bind(this)}).send("zip="+zipcode);
		}
	},
	
	loading: function(step) {
		if (typeof(step) == 'undefined') {
			step = 1;
		}
		$('step'+step+'loading').setStyle('display', 'block');
	},
	
	loadingDone: function(step) {
		if (typeof(step) == 'undefined') {
			step = 1;
		}
		$('step'+step+'loading').setStyle('display', 'none');
	}
	
});

Mobajl.Checkboxes = new Class({
	initialize: function(listid) {
		if (typeof(listid) == 'undefined') {
			listid = 'interestslist';
		}
		this.listid = listid;
		this.bindElements();
	},
	
	bindElements: function() {
		$(this.listid).getElements('li').each(function(element) {
			element.handler = this;
			element.addEvent('click', this.toggleCheckbox);
			element.getElement('input').value = 0;
		}, this);
	},
	
	toggleCheckbox: function(event) {
		var storage = this.getElement('input');

		if (storage.value == 1) {
			storage.value = 0;
			this.getElement('div').setStyle('background-image', 'url('+baseurl+'img/checkbox.png)');
		} else {
			storage.value = 1;
			this.getElement('div').setStyle('background-image', 'url('+baseurl+'img/checkbox_checked.png)');
		}
	}
});

Mobajl.MyPage = new Class({
	initialize: function() {
		$('copylinkbutton').addEvent('click', this.copyLinkToClipboard.bind(this));
		$('invitelink').addEvent('keypress', this.checkLinkInput.bind(this));
		if ($('ianswer')) {
			$('ianswer').addEvent('keypress', this.checkAnswerKeypress.bind(this));
		}
		$('answerform').addEvent('submit', this.sendAnswer.bind(this));
	},
	
	copyLinkToClipboard: function(e) {
		Clipboard.copy($('invitelink').value);

		$('invitelink').get('tween', {property: 'opacity', duration: 'long'}).start(0);
		$('copylinkbutton').set('html', alerts.copied);
		var afterlink = function(){
			$('invitelink').get('tween', {
				property: 'opacity',
				duration: 'long'
			}).start(1);

		};
		afterlink.delay(1000);

		var aftertext = function(){
			$('copylinkbutton').set('html', alerts.copy);
		};
		aftertext.delay(3000);

		if (e) {
			new Event(e).stop();
		}
	},
	
	checkLinkInput: function(e) {
		if (e.meta) {
		} else if (e.event.keyCode == 9 || (e.event.keyCode >= 33 && e.event.keyCode <= 39)) {
		} else {
			new Event(e).stop();
		}
	},
	
	checkAnswerKeypress: function(e) {
		if (e.key == 'enter') {
			this.sendAnswer(e);
		}
	},

	sendAnswer: function(e) {
		new Event(e).stop();
		
		var iMsg = '';
		var answer = $('ianswer').value.trim();
		if (answer.length < 2) {
			iMsg += alerts.answers;
		} else if (answer.split(" ") > 1) {
			iMsg += alerts.answeronlyoneword;
		}
		
		if (iMsg.length > 0) {
			var alert = makeAlert(alerts.errors, iMsg, 'ianswer');
		} else {
			var jsonRequest = new Request.JSON({
				url: baseurl+"ajax/answer",
				onComplete: function(result){
					if (result.success) {
						$('myanswersent').set('html', result.answer);
						$('answerform').setStyle('display', 'none');
						$('myanswersent').setStyle('display', 'block');
						setGoal(4, 'success');
					} else {
						if (typeof(result.errors.alreadyanswered) != 'undefined') {
							$('myanswersent').set('html', result.answer);
							$('answerform').setStyle('display', 'none');
							$('myanswersent').setStyle('display', 'block');
						}
						iMsg = "";
						for (var i in result.errors) {
							iMsg += result.errors[i]+"<br />\n";
						}
				
						makeAlert(alerts.errors, iMsg);
					}
				}
			});
			jsonRequest.send("answer="+answer);
						
			
		}
	}
})

Mobajl.LoginPage = new Class({
	initialize: function(usercode, first) {
		this.bindElements();
		this.cba = new Mobajl.Checkbox('cba', 'cbabox', 'cbalabel');
	},
	
	bindElements: function() {
		$('logininvite').addEvent('click', this.loginInvite.bind(this));
	},
	
	loginInvite: function(e) {
		new Event(e).stop();
		var errors = {};
		var formcheck = true;
		var focus = false;
		var iMsg = "";
		if ($('iusername').value.length == 0) {
			iMsg += alerts.email+"<br />";
			formcheck = false;
			focus = 'iusername';
		}
		if ($('ipassword').value.length == 0) {
			iMsg += alerts.password+"<br />";
			formcheck = false;
			if (!focus) {
				focus = 'ipassword';
			}
		}
		
		if (!this.cba.checked()) {
			iMsg += alerts.agree+"<br />";
			formcheck = false;
		}

		if (!formcheck) {
			var alert = makeAlert(alerts.errors, iMsg, focus);
			return;
		}
		$('iform').submit();
		
	}
});

Mobajl.Checkbox = new Class({
	initialize: function(storage, box, label) {
		this.storage = $(storage);
		if (this.storage) {
			this.storage.value = 0;
		}
		this.box = $(box);
		this.label = $(label);

		if (this.box) {
			this.box.addEvent('click', this.toggleCheckbox.bind(this));
		} else {
			return false;
		}
		if (this.label) {
			this.label.addEvent('click', this.toggleCheckbox.bind(this));
		}
	},
	
	toggleCheckbox: function() {
		if (this.storage.value == 1) {
			this.storage.value = 0;
			this.box.setStyle('background-image', 'url('+baseurl+'img/checkbox.png)');
		} else {
			this.storage.value = 1;
			this.box.setStyle('background-image', 'url('+baseurl+'img/checkbox_checked.png)');
		}
	},
	
	checked: function () {
		if (this.storage.value == 1) {
			return true;
		} else {
			return false;
		}
	}
})

Mobajl.CountDown = new Class({
	initialize: function(){
		this.offset = 0;
		this.next = 0;
		this.nextDate = new Date();
		this.timecontainer = $('countdown');
		this.periodicalsetup = false;
		this.header = "";
		this.newcheck = 3600;
		this.checking = false;
		this.closing = new Date();
		this.closed = false;
		this.closeUpcoming = false;
		this.periodicaltimer = null;
		this.getNext();
	},
	
	getNext: function() {
		if (this.checking) {
			return;
		}
		this.checking = true;
		var jsonRequest = new Request.JSON({
			url: baseurl+"ajax/time",
			onComplete: function(result){
				if (result.closed) {
					this.closed = true;
					this.competitionClosed();					
				} else if (result.success) {
					this.next = result.next;
					this.offset = result.offset;
					this.nextDate.setTime(result.next*1000);
					if (this.header != result.header) {
						this.header = result.header;
						this.setupHeader();
					}
					this.setupCounter();
					this.checking = false;
					if (result.closing) {
						this.closing.setTime(result.closing*1000);
						this.closeUpcoming = true;
					} else {
						this.closeUpcoming = false;
					}
				}
			}.bind(this)
		});
		var d = new Date();
		jsonRequest.send("time="+d.getTime());
	},
	
	setupHeader: function() {
		$('countdownheader').set('html', alerts[this.header]);
	},

	setupCounter: function() {
		if (!this.periodicalsetup) {
			this.periodicaltimer = this.checkCountdown.periodical(1000, this);
		}
	},
	
	competitionClosed: function() {
		$clear(this.periodicaltimer);
		$('countdownheader').set('html', alerts.competitionclosed);
		this.timecontainer.set('html', "00:00");
	},

	checkCountdown: function() {
		var current = new Date();
		
		if (this.closeUpcoming) {
			var diff = this.closing.getTime()-current.getTime();
		} else {
			var diff = this.nextDate.getTime() - current.getTime();
		}
		this.newcheck--;

		var t = Math.round(diff/1000);
		if (t < 0 || this.newcheck <= 0) {
			this.newcheck = 3600;
			this.getNext();
		} 
		if (t < 0) {
			t = 0;			
		}

		var hours = parseInt(t/3600);
		t = t-(hours*3600);
		var minutes = parseInt(t/60);
		t = t-(minutes*60);
		if (minutes < 10) {
			minutes = "0"+minutes;
		}
		if (t < 10) {
			t = "0"+t;
		}
		
		var separator = ":";
		var timestring = "";
		if (hours > 0) {
			timestring += hours+separator;
		}
		timestring += minutes+separator+t;
		this.timecontainer.set('html', timestring);
		
		if (diff <= 0 && this.closeUpcoming) {
			this.closeCompetition();
		} else if (diff <= 0) {
			this.updateImage();
			this.openCompetition();
		}
	},
	
	closeCompetition: function() {
		var form = $('answerform');
		if (form) {
			if (form.getStyle('display') != 'none') {
				form.setStyle('display', 'none');
				var h1 = $('myanswersent');
				h1.set('html', '');
				h1.setStyle('display', 'block');
			}
		}
		this.closed = true;
		this.updateImage();
	},
	
	openCompetition: function() {
		var form = $('answerform');
		if (form) {
			if (form.getStyle('display') == 'none') {
				form.setStyle('display', 'block');
				var h1 = $('myanswersent');
				h1.set('html', '');
				h1.setStyle('display', 'none');
			}
		}		
	},
	
	updateImage: function() {
		var img = $('competitionimage');
		var src = img.src;
		var d = new Date();
		src = src.substring(0, src.indexOf('=')+1)+d.getTime();
		img.src = src;		
	}
});

function makeAlert(header, msg, focus) {
	if (Browser.Engine.trident4) {
		var slask = new Object();
		alert(header+"\n"+msg.trim().replace(/<br \/>\n?/gi, "\n"));
		if (focus) {
			$(focus).focus();
		}
	} else {
		var slask = Mobajl.Alert(header, msg);

		if (focus) {
			slask.addEvent('close', function(){
				$(focus).focus();
			});
		}
	}

	return slask;
}

Mobajl.Alert = function(msghdr, msg) {
	baseHref = '/img/alert/';
	
	var body = Mobajl.StickywinAlert(msghdr, msg, {width: 250});
	var modaloptions = {
		modalOptions: {
			modalStyle: {
				zIndex: 11000
			}
		},
		zIndex: 110001,
		content: body,
		position: 'center' //center, corner
	};

	return new StickyWinModal(modaloptions);
};


Mobajl.StickywinAlert = function(caption, body){
	options = {
		css: "",
		width: 250,
		cornerHandle: false,
		cssClass: '',
		baseHref: '/img/alert/',
		buttons: []
	};

	new StyleWriter().createStyle(options.css.substitute({baseHref: options.baseHref}, /\\?\{%([^}]+)%\}/g), 'defaultStickyWinStyle');
	caption = $pick(caption, '%caption%');
	body = $pick(body, '%body%');
	var container = new Element('div').setStyle('width', options.width).addClass('DefaultStickyWin');
	if(options.cssClass) container.addClass(options.cssClass);

	//header
	var h1Caption = new Element('h1').addClass('caption').setStyle('width', (options.width.toInt()-(options.cornerHandle?70:60)));

	if($(caption)) h1Caption.adopt(caption);
	else h1Caption.set('html', caption);
	
	var bodyDiv = new Element('div').addClass('body');
	if($(body)) bodyDiv.adopt(body);
	else bodyDiv.set('html', body);

	//bodyDiv.addClass('clearfix');
	
	container.adopt(
		new Element('div').addClass('top').adopt(
			new Element('div').addClass('closeButton').addClass('closeSticky')
		).adopt(h1Caption)
	);
	//body
	container.adopt(bodyDiv);

	//footer
	container.adopt(
		new Element('div').addClass('bottom')
	);
	return container;
};


function returnAge() {
	ayear = 0;
	if ($('iyear').value==9999 || $('imonth').value==99 || $('iday').value==99) {
		return -1;
	} else {
		var myDate = new Date();
		myDate.setFullYear($('iyear').value, ($('imonth').value-1), $('iday').value);

		var ayear = (today.getFullYear() - myDate.getFullYear())-1;

		if (today.getMonth() > myDate.getMonth()) {
			ayear++;
		} else if (today.getMonth() == myDate.getMonth() && today.getDate() >= myDate.getDate()) {
			ayear++;
		}
	}

	return ayear;
}

function IsNumeric(sText) {
	if (sText.search(/^[0-9]*$/gi) == -1) { return false; }
	return true;
}


function preloadImages(preload) {
	if (typeof(preload) != 'undefined') {
		var myImages = new Asset.images(preload);
	}
 }

function makeModal(assignfilter, width, height, type, autosize) {
	if (typeof(type) == 'undefined') {
		type = 'iframe';
	}
	if (typeof(autosize) == 'undefined') {
		autosize = false;
	}
	var options = {
		size: {
			x: width,
			y: height
		},
		autosize: autosize
	};
		
	if (type == 'iframe') {
		options.handler = 'iframe';
	} else if (type == 'ajax') {
		options.ajaxOptions = {
			method: 'get' // we use GET for requesting plain HTML
		};
	}
		

	SqueezeBox.assign($$(assignfilter), options);
}

function setExternalLinks() {
	$$('a[rel=external]').each(function(element) {
		element.target='_blank';
	});
}

function makeExitofferModal() {
	var options = {
		size: {
			x: exitofferx,
			y: exitoffery
		},
		autosize: true,
		handler: 'iframe',
		closable: false,
		closeBtn: false,
/*		onClose: function() {
			alert("hej!");
		},*/
		onOpen: function() {
			setGoal(3, 'view');
			$('sbox-btn-close').addEvent('click', exitOfferNo);
		}
	};
		
	SqueezeBox.assign($$('a[rel=boxedexitoffer]'), options);
}

function exitOfferYes(){
	setGoal(3, 'accept');
	var jsonRequest = new Request.JSON({
		url: baseurl+"ajax/exitoffer",
		onComplete: function(result){
			exitOfferProceed();
		}
	});
	jsonRequest.send("accept=yes");
}

function exitOfferNo() {
	exitOfferProceed();
}

function exitOfferProceed() {
	SqueezeBox.setOptions({closable: true});
	SqueezeBox.close();
	window.location = baseurl+"invite/";
}

function setGoal(goal, name) {
	pageTracker._trackPageview("/funnel_P"+goal+"/"+name); 
}

function testAllCheck() {
	var i = 0;
	var allchecked = true;
	while(true) {
		var element = $('cb'+i);
		if (element) {
			if (!element.checked) {
				allchecked = false;
				break;
			}
		} else {
			break;
		}
		i++;
	}
	$('cbm').checked = allchecked;
}

function toogleAll() {
	var allstatus = $('cbm').checked;
	var i = 0;
	while(true) {
		var element = $('cb'+i);
		if (element) {
			element.checked = allstatus;
		} else {
			break;
		}
		i++;
	}
}

function checkForm3 () {
	var iMsg = '';

	if (!friendCheck ()) {
		iMsg += alerts.msnselect;
	}

	if (iMsg == '') {
		check3run = true;
		$('iform').submit();
	} else {
		makeAlert(alerts.errors, iMsg);
	}
}

function friendCheck () {
	i = 0; e = 0;
	while (1==1) {
		if ($('cb' + i)) {
			if ($('cb' + i).checked) {
				e++;
			}
		} else {
			if (e==0) { return false; } else { return e; }
		}
		i++;
	}
	if (e==0) { return false; } else { return e; }
}

function fbs_click() {
	u=location.href;t=document.title;window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');return false;
}

var fixIeShadows = function() {
	if (Browser.Engine.trident4) {
		var contentsize = $('content').getSize();
		$('content_iefix_left').setStyle('height', contentsize.y+"px");
		$('content_iefix_right').setStyle('height', contentsize.y+"px");
	}
}
