/**
	Checkin.no javascripts
	
*/


function Checkin()
{
	this.loader_big	= '<img src="/images/loader.gif" style="border:0; height: 32px; width:32px" />';
	this.loader_small = '<img src="/images/loader_small.gif" style="border:0; height: 16px; width:16px" />';
	this.loader_submit = '<img src="/images/loader_submit.gif" style="border:0; height: 16px; width:16px" />';
	
	this.return_var;
	this.last_submit_var;

	// this function does 
	this.load = function(id, path, data, post, hide_loader) {
		hide_loader == null ?  hide_loader = false : hide_loader = true;
		var old_text = $(id).html();
		post == null ? method = 'GET' : method = 'POST';
		if (!hide_loader) $(id).html(this.loader_big);
		this.return_var = true;
		$.ajax({
			type:	method,
			url:	path,
			data:	data,
			async:	true,
			success: function(data, status, xhr) {
				if (xhr.getResponseHeader("Content-Type") == "application/pdf") {
					$(id).html(old_text);
					checkin.return_var = false;
				} else if (xhr.getResponseHeader("Content-Type") == "text/javascript" || xhr.getResponseHeader("Content-Type") == "text/javascript; charset=UTF-8") {
					if (!hide_loader) $(id).html(old_text);
					checkin.return_var = true;
				} else {
					$(id).html(data);
					checkin.return_var = true;
				}
			},
			timeout: 4000,
			error: function(xhr, errorMsg) {
				if (!hide_loader) $(id).html(old_text);
				//alert(xhr.responseText);
			}
		});
		return this.return_var;
	};
	
	this.load_silent = function(path, data, post) {
		post == null ? method = 'GET' : method = 'POST';
		$.ajax({
			type:	method,
			url:	path,
			data:	data,
			async:	false,
			success: function(data) {
				return true;
			},
			timeout: 4000,
			error: function(XMLHttpRequest, errorMsg) {
				return false;
			}
		});
		return true;
	};
	
	this.history_add = function() {
		
		
	};
	
	this.warning = function()
	{
		
	};
	
	// This function handles ajax forms
	this.form_post = function(form, id, path, async) {
		this.return_var = false;
		async == null ? async = 'GET' : async = 'POST';
		$.ajax({
			type:	'POST',
			url:	path,
			data:	$(form).serialize(),
			async:	true,
			success: function(data, status, xhr) {
				if (xhr.getResponseHeader("Content-Type") == "application/pdf") {
					checkin.return_var = true;
				} else if (xhr.getResponseHeader("Content-Type") == "text/javascript" || xhr.getResponseHeader("Content-Type") == "text/javascript; charset=UTF-8") {
					checkin.return_var = false;
				} else {
					$(id).html(data);
				}
			},
			timeout: 4000,
			error: function(xhr, errorMsg) {
				$('button', id).html(checkin.last_submit_var);
				$('button', id).removeAttr('disabled');
				//alert(xhr.responseText);
			}
		});
		return this.return_var;
	};
	
	// This function handles ajax forms
	this.colorbox_form_post = function(form, path, resize) {
		if (resize == null) resize = true;
		this.return_var = false;
		$.ajax({
			type:	'post',
			url:	path,
			data:	$(form).serialize(),
			async:	true,
			success: function(data, status, xhr) {
				$('#cboxLoadedContent').html(data);
			},
			complete: function (xhr, status) {
				$.colorbox.resize();
			},
			timeout: 4000,
			error: function(xhr, errorMsg) {
				$('button', '#cboxLoadedContent').html(checkin.last_submit_var);
				$('button', '#cboxLoadedContent').removeAttr('disabled');
				//alert(xhr.responseText);
			}
		});
		return this.return_var;
	};
	
	this.reload = function()
	{
		if ($('#mypage-content')) {
			this.load('#mypage-content', window.location.pathname+window.location.hash.replace('#!', ''));
		}
		else {
			this.load('#content', window.location.pathname+window.location.hash.replace('#!', ''));
		}
	};
	
	this.remove_tinymce = function()
	{
		$('textarea').tinymce().remove();
		$('#mce_fullscreen_container').remove();
	};
}

// Load Checkin() class to var
var checkin = new Checkin();
var stophashchange = false;
var stopclick = false;

// Things to do when launching the site
$(document).ready(function () {
	// Load content on page reload
	if (window.location.hash.substring(0, 2) == '#!' && $("#mypage-content")) {
		checkin.load('#page', window.location.pathname+window.location.hash.replace('#!', '')+'?killxmlrequest=true');
	}
	else if (window.location.pathname.substring(0, 8) == '/mypage/' && $("#mypage-content").length != 0 && $("#login_form").length == 0) {
		window.location = '/mypage#!'+window.location.pathname.substring(7);
	}
	
	
	// Trace hash changes (going back/forward)
	$(window).bind("hashchange", function() {
		if (stophashchange) {
			stophashchange = false;
			return false;
		}
		if (window.location.hash.length < 1) {
			return true;
		}
		if ((window.location.hash.substring(1,2) == '!' || window.location.pathname == '/mypage')) {
			checkin.remove_tinymce();
			checkin.load('#mypage-content', window.location.pathname+window.location.hash.replace('#!', ''));
		}
	});
	
	// Colorbox links (ajax update)
	$("#cboxLoadedContent a").live("click", function() {
		if ($(this).hasClass('download')) {
			return true;
		}
		$.colorbox({iframe:false, href:$(this).attr('href')});
		return false;
	});
	
	// Mypage buttons click action
	$("#mypage-top-buttons a").live("click", function() {
		$("#mypage-top-buttons a").removeClass('active');
		$(this).addClass('active');
		checkin.remove_tinymce();
	});
	
	// Mypage buttons click action
	$("#tab-buttons a").live("click", function() {
		$("#tab-buttons a").removeClass('active');
		$(this).addClass('active');
		checkin.remove_tinymce();
		checkin.load('#tab-content', $(this).attr('href'));
		stopclick = true;
		return false;
	});
	$("#ui-datepicker-div a").live("click", function() {
		$("#ui-datepicker-div").hide();
		return false;
	});

	// History click
	$("#navigator a").live("click", function() {
		if ($(this).attr('href') == '/mypage') {
			$("#mypage-top-buttons a").removeClass('active');
			$("#mypage-top-buttons a:first").addClass('active');
			return true;
		}
		stopclick = true;
		if (checkin.load('#mypage-content', $(this).attr('href'))) {
			//$(this).parent('div').nextAll().remove();

			if (window.location.pathname.substring(0, 8) == '/mypage/')
				window.location='/mypage#!'+window.location.pathname.substring(7);
			else
				window.location=window.location.pathname+'#!'+$(this).attr('href').replace(window.location.pathname, '');
		}
		return false;
	});
	
	// Mypage links (ajax update)
	$("#mypage-page a").live("click", function() 
	{
		if (stopclick) {
			stopclick = false;
			return false;
		}
		// Ignore links clicked in tinymce
		if ($('[id*=_parent] #'+$(this).attr('id')).length || $('#ui-datepicker-div #'+$(this).attr('id')).length) {
			 return true;
		}
		
		if ($(this).hasClass('colorbox')) {
			$.colorbox({iframe:false, href:$(this).attr('href'), open:true});
			return false;
		}
		// Remove TinyMce instance when clicking
		checkin.remove_tinymce();
		if ($(this).attr('href') == '#' || $(this).hasClass('ignore')) {return false;}
		if ($(this).hasClass('follow')) {return true;}
		
		stophashchange = true;		
		if ($(this).hasClass('download') || $(this).attr('href').match('download') != null || $(this).attr('href').match('/event/register') != null || ($(this).attr('target') && $(this).attr('target') == '_blank')) {
			return true;
		}
		// For customer links
		if ($("#mypage-top-buttons a.customer").is('visible') == false && $(this).hasClass('mypage-customer-link')) {
			$("#mypage-top-buttons a.customer").attr('href', $(this).attr('href'));
			$("#mypage-top-buttons a.customer").text($(this).text());
			$("#mypage-top-buttons a.customer").show();
			$("#mypage-top-buttons a").removeClass('active');
			$("#mypage-top-buttons a.customer").addClass('active');
		}
		
		$(this).attr('target') ? target = $(this).attr('target') : target = '#mypage-content';
		if (checkin.load(target, $(this).attr('href'))) {
			var text;
			$(this).attr('title') ? text = $(this).attr('title') : text = $(this).text();
			if ($(this).hasClass('nohistory') == false && !$(this).attr('target') && !$(this).attr('onclick')) {
				window.location = '/mypage#!'+$(this).attr('href').substring(7);
				return false;
			}
		}
		return false;
	});

	// Language change
	$("#mypage-top-language a").live("click", function() {
		if (checkin.load_silent($(this).attr('href'))) {
			checkin.load('#page', window.location.pathname+window.location.hash.replace('#!', '')+'?killxmlrequest=true');
		}
		return false;
	});

	// Colorbox forms (ajax update)
	$("#cboxLoadedContent form").live("submit", function() {
		if ($(this).hasClass('ignore')) {
			return false;
		}
		$('button', this).attr('disabled', 'disabled');
		checkin.last_submit_var = $('button', this).html();
		$('button', this).html(checkin.loader_submit);
		checkin.colorbox_form_post('#'+$(this).attr('id'), $(this).attr('action'));
		return false;
	});
	
	// Colorbox forms (ajax update)
	$("#tab-content form").live("submit", function() {
		if ($(this).hasClass('ignore')) {
			return false;
		}
		$('button', this).attr('disabled', 'disabled');
		checkin.last_submit_var = $('button', this).html();
		$('button', this).html(checkin.loader_submit);
		stopclick = true;
		checkin.form_post('#'+$(this).attr('id'),'#tab-content', $(this).attr('action'));
		return false;
	});
	
	// Mypage forms
	$("#mypage-page form").live("submit", function()
	{
		if (stopclick) {
			stopclick = false;
			return false;
		}
		if ($(this).attr('onsubmit')) {
			return true;
		}
		if ($(this).hasClass('ignore')) {
			return false;
		}
		if ($(this).attr('action').match('/event/register') != null) { //|| $(this).attr('action').match('download') != null) {
			return true;
		}
		$(this).attr('target') ? target = $(this).attr('target') : target = '#mypage-content';
		$('input[type=submit]', this).attr('disabled', 'disabled');
		$('input[type=submit]', this).parent().append(checkin.loader_small);
		$('button', this).attr('disabled', 'disabled');
		checkin.last_submit_var = $('button', this).html();
		$('button', this).html(checkin.loader_submit);
		if ($(this).hasClass('follow')) {
			stophashchange = true;
			window.location = '/mypage#!'+$(this).attr('action').substring(7);
		}
		return checkin.form_post('#'+$(this).attr('id'), target, $(this).attr('action'), true);
	});
	
	// Mottak forms
	$("#mottak-content form").live("submit", function() {
		if ($(this).attr('onsubmit')) {
			return true;
		}
		return checkin.form_post('#'+$(this).attr('id'), '#mottak-content', $(this).attr('action'));
	});
	
	// Mypage top links
	$("#mypage-buttons a").live("click", function()
	{
		if ($(this).attr('href') != '/scripts/mypage-user/orders') {
			return true;
		}
		checkin.load('#mypage-content', $(this).attr('href'));
		return false;
	});
});
