var LANG = "dk";
var i18n = {
	dk: {
		main: {
			title: 'Nyhedsbrev'
		},
		leftBox: {
			errorInvalidInput: 'Du skal indtaste både dit navn og din e-mail-adresse for at fortsætte.'
		},
		mainweb: 'http://www.jensens.com/',
		newsletterPage: 'nyhedsbrev.htm'
	},

	de: {
		main: {
			title: 'Neuheiten'
		},
		leftBox: {
			errorInvalidInput: 'Sie müssen sowohl Ihren Namen und Ihre E-Mail-Adresse, um fortzufahren.'
		},
		mainweb: 'http://www.bofhus.de/',
		newsletterPage: 'newsletter.htm' // 'newsletter.htm'
	},

	se: {
		main: {
			title: 'Nyhetsbrev'
		},
		leftBox: {
			errorInvalidInput: 'Du måste ange både ditt namn och e-postadress för att fortsätta.'
		},
		mainweb: 'http://www.jensens.se/',
		newsletterPage: 'nyhetsbrev.htm' // 'nyhetsbrev.htm'
	},

	template: {
		main: function(lang) {
			var str = '<span style="'+CSS.h1style+'" class="modulefont28">'+lang.title+'</span>'+
				CSS.hr;
			
			return str;
		}
	}
};var CSS = {
	hr: '<div style="background-image: url(\'http://www.jensens.com/files/images/jensens/layout/liner_bg.gif\'); width: 560px; background-repeat: repeat-x; height: 16px;"></div>',
	h1style: 'padding-top: 5px; padding-bottom: 6px; width: 560px;'
};if (window.location.href.indexOf("jensens.com") != -1) {
	document.domain = "jensens.com";
	LANG = 'dk';
}
if (window.location.href.indexOf("bofhus.de") != -1) {
	document.domain = "bofhus.de";
	LANG = 'de'
}
if (window.location.href.indexOf("jensens.se") != -1) {
	document.domain = "jensens.se";
	LANG = 'se'
}

var signupPage = 'http://nyhedsservice.jensens.com/signup2010/signup_form.php?lang='+LANG+'&';
var activatePage = 'http://nyhedsservice.jensens.com/signup2010/signup_activate.php?lang='+LANG+'&';
var unsubscribePage = 'http://nyhedsservice.jensens.com/signup2010/signup_unsubscribe.php?lang='+LANG+'&';
var updateProfilePage = 'http://nyhedsservice.jensens.com/signup2010/signup_updateprofile.php?lang='+LANG+'&';
var iframeHeight = 500;
var iframeId = null;



/**
 * Add the signup form to the newsletter-page
 **/
var NewsletterPage = {
	_mainContent: null,
	_headerfont: null,
	_newsletterPage: i18n[LANG].newsletterPage,

	construct: function() {
		this._mainContent = $('.standard_maincontent');
		this._headerfont = {
			cssClass: '.modulefont28',
			cufonParams: {
				fontFamily: "insigna",
				hover: true,
				hoverables: {
					span: true,
					div: true,
					a: true
				}
			}
		}
	},

	showMain: function (appendUrl) {
		iframeId = 'newssignup_content';
		this._mainContent.html(i18n.template.main(i18n[LANG].main));
		this._mainContent.append('<iframe id="'+iframeId+'" ALLOWTRANSPARENCY="true" src="'+signupPage+(appendUrl != undefined?appendUrl:'')+'" frameborder="0" width="560" height="'+iframeHeight+'"></iframe>');
	},
	
	showUpdate: function (appendUrl) {
		iframeId = 'newssignup_update';
		this._mainContent.html(i18n.template.main(i18n[LANG].main));
		this._mainContent.append('<iframe id="'+iframeId+'" ALLOWTRANSPARENCY="true" src="'+signupPage+(appendUrl != undefined?appendUrl:'')+'" frameborder="0" width="560" height="'+iframeHeight+'"></iframe>');
	},
	
	showUpdateProfile: function () {
		iframeId = 'newssignup_updateprofile';
		this._mainContent.html(i18n.template.main(i18n[LANG].main));
		this._mainContent.append('<iframe id="'+iframeId+'" ALLOWTRANSPARENCY="true" src="'+updateProfilePage+'" frameborder="0" width="560" height="'+iframeHeight+'"></iframe>');
	},

	runCufon: function() {
		Cufon.replace(NewsletterPage._headerfont.cssClass, NewsletterPage._headerfont.cufonParams);
	}
};

/**
 * Handle signups from the user in the left sidebar form on Jensens.com
 **/
var NewsletterBoxSignup = {
	_form: ['#newsmailForm form'],
	_nameDefault: '',
	_emailDefault: '',
	
	getForm: function(tag) {
		var str = '';

		for(var i=0; i<NewsletterBoxSignup._form.length; i++) {
			if (i>0)
				str += ', ';
			str += NewsletterBoxSignup._form[i] + (tag != undefined ? (' ' + tag) : '');
		}

		return str;
	},
	
	construct: function() {
		$(NewsletterBoxSignup.getForm('#name')).val();
		this._nameDefault = $(this.getForm(" #name")).val();
		this._emailDefault = $(this.getForm("#email")).val();
		$(this.getForm()).attr('onSubmit', ''); // remove built-in onSubmit
		$(this.getForm()).submit(this.formSubmit);
	},
	// $('.standard_leftcontent form').submit(function(e){window.alert("submit"); return false;});
	formSubmit: function() {
		if (($(NewsletterBoxSignup.getForm('#name')).val() == NewsletterBoxSignup._nameDefault) || $(NewsletterBoxSignup.getForm('#name')).val() == NewsletterBoxSignup._emailDefault) {
			window.alert(i18n[LANG].leftBox.errorInvalidInput);
			return false;
		}
		
		window.location = i18n[LANG].mainweb + NewsletterPage._newsletterPage + '?' + Math.random() + '#name=' + escape($(NewsletterBoxSignup.getForm('#name')).val()) + '&email=' + escape($(NewsletterBoxSignup.getForm('#email')).val());
		
		return false;
	}
}

function updateIframeHeight(height) {
	iframeHeight = height;
	$('#newssignup_content').height(height);
}

function updateTitle(newTitle) {
	$('.modulefont28').html(newTitle);
	NewsletterPage.runCufon();
}

function userPrompt(msg) {
	return window.prompt(msg);
}

$(document).ready(function() {
	if (window.location.pathname.indexOf(NewsletterPage._newsletterPage) != -1) {
		NewsletterPage.construct();
		
		// activation page
		if (window.location.hash.indexOf('activationCode=') != -1) {
			NewsletterPage.showActivate();
		}
		// unsubscribe page
		else if (window.location.hash.indexOf('unsubscribe') != -1) {
			NewsletterPage.showUnsubscribe();		
		}
		// update page with details
		else if (window.location.hash.indexOf('update=') != -1) {
			NewsletterPage.showUpdate(window.location.hash.substr(1));
		}
		// update page
		else if (window.location.hash.indexOf('updateprofile') != -1) {
			NewsletterPage.showUpdateProfile();
		}
		// sign-up page with pre-filled name and e-mail
		else if (window.location.hash.indexOf('name=') != -1) {
			NewsletterPage.showMain(window.location.hash.substr(1));
		}
		// sign-up page
		else {
			NewsletterPage.showMain();
		}
	}
	
	NewsletterBoxSignup.construct();
});