var VikanApp = {
	cookieLocation: '.vikan.com',
	loggingEnabled: true,
	
	initTabHandler: function() {
		$('.productsheet').height(60+$('.showing').height());	
		$('ol.tabnav').click(function(e) {
			var clicked = $(e.target);
			if (clicked.is('a')) {
				$('ol.tabnav li.selected').removeClass('selected');
				clicked.parent().addClass('selected');

				$('.tab.showing').removeClass("showing");
				$(clicked.attr("href")).toggleClass('showing');
			}
			e.stopPropagation();
			$('.productsheet').height(60+$('.showing').height());	
			return false;
		});
	},
	
	mainInitializer: function() {
		VikanApp.frontPageInitialRedirect();
		
		VikanApp.setupSectorSelector();
		VikanApp.countryPageCookie();
		VikanApp.imageSelector();
		VikanApp.dokumentationDropdown();
		VikanApp.Rewrite404URL();
		VikanApp.setupLanguageSelector();
		VikanApp.setupPrint();
		VikanApp.clearRadio();
		VikanApp.initToggleDisplayItems();
		VikanApp.initTabHandler();
		VikanApp.videoDialog();
		VikanApp.jSVideoSetup();
	},
	
	frontPageInitialRedirect: function() {
		// Only happens on the splash page, and only if not explicitly bypassed
		if ($("body.top #countries")[0] && document.location.search.indexOf("redirect=false") === -1) {
			var userCountryCode = VikanUser.getProperty("countryCode");
			var	userVikanSite = VikanUser.getProperty("vikanSite");
			
			if (typeof userVikanSite === "string" && /^(int|uk|de|es|se|dk|fr)$/i.test(userVikanSite) === true) {
				 document.location = "/" + userVikanSite + "/";
			}

			// VikanApp.log('userVikanSite = ' + userVikanSite);
			// VikanApp.log('userCountryCode = ' + userCountryCode);
		}
	},
		
	// Initializes the sector selector (unless on the front page)
	setupSectorSelector: function() {
		$("body:not(.front) #sectorselector:not(.showing) > h2").live('click', VikanApp.showSectorSelector);
		$("body:not(.front) #sectorselector.showing").live('click', VikanApp.hideSectorSelector);
	},

	showSectorSelector: function() {
		VikanApp.toggleSectorSelector(true);
	},
	
	hideSectorSelector: function() {
		VikanApp.toggleSectorSelector(false);
	},

	// Displays/hides the sector selector
	toggleSectorSelector: function(addClass) {
		$("#sectorselector ul").slideToggle("fast");
		if (addClass) {
			 $("#sectorselector").addClass('showing');
		} else {
			$("#sectorselector").removeClass('showing');
		}
	},
	
	countryPageCookie: function() {
		
		$('.countryList a').click(function() {
			var url = $(this).attr("href");
			var site = url.replace(/\//g, '');
			var country = $(this).attr('data-countrycode');
			VikanUser.setProperty('vikanSite', site);
			VikanUser.setProperty('countryCode', country);
			window.location.href = url;
		});
		
		$('span > .contain a').click(function() {
			VikanUser.setProperty('vikanSite', location.pathname.split("/")[1]);
		});
	},
	
	setupLanguageSelector: function() {
		$('select#langselect').change(function() {
			var	url = $(this).val();
			var site = url.replace(/\//g, '');
			VikanUser.setProperty('vikanSite', site);
			VikanUser.setProperty('countryCode', site);
			window.location.href = url;
		});
	},
	
	dokumentationDropdown: function() {
		$('#dokumentationList .readmore').click(function(){
			if(!$(this).parents('.round').find('.readmoreText').is(":visible"))
			{
				$(this).parents('.round').find('.readmoredots').text(" ");
				$(this).parents('.round').find('.readmoreimagebutton img').attr("src", "/images/btn_hide.png");
				$(this).parents('.round').find('.readmoreText').removeClass("iefix");
			}
			else
			{
				$(this).parents('.round').find('.readmoreimagebutton img').attr("src", "/images/btn_show.png");
				$(this).parents('.round').find('.readmoredots').text("...");
				$(this).parents('.round').find('.readmoreText').addClass("iefix");
			}
			$(this).parents('.round').find('.readmoreText').toggle('fast');	
			return false;
		}).parents('.round').find('.readmoreText').hide();
	},
	
	// FAQ Items and Contacts expand and collapse
	initToggleDisplayItems: function(){
		$('.faqItem a.readmore, .contact a.readmore').click(function(){
		
			if($(this).siblings('div').is(":visible"))
				$(this).children().attr("src", "/images/btn_show.png");
			else
				$(this).children().attr("src", "/images/btn_hide.png");
			$(this).siblings('div').toggle("fast");
			return false;
		}).siblings('div').hide();
	},

	Rewrite404URL: function() {
		if($('.maincontent').hasClass('404')) {
			var url = VikanApp.getURLParameter("url")
			
			window.history.pushState("404", "404", url);
		}
	},
	
	imageSelector: function () {
		
		var src =$('#imageList img:first-child');
		if(src.length>0)
			$('#bigImage').attr('src', src.attr('src').replace("80x80", "290x300"));
		$('#imageList a').click(function(){
			var src = $(this).find('img').attr('src');
			
			$('#bigImage').attr('src', src.replace("80x80", "290x300"));
			return false;
		});
	},
	
	getURLParameter: function(name) {
		return decodeURI(
	 	(RegExp(name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1]);
	},
	
	getClientLabel: function(key) {
		if (VikanLabels) {
			return VikanLabels[key];
		}
	},
	
	setupPrint: function() {
		$(".print").show().click(function() {window.print();});
	},
	clearRadio: function() {
		
		$('input:radio').bind('click mousedown', (function() {
			
			//http://stackoverflow.com/questions/2117538/jquery-how-to-uncheck-a-radio-button
			var isChecked;
			return function(event) {
			
				if(event.type == 'click') {
					if(isChecked) {
						isChecked = this.checked = false;
					} else {
						isChecked = true;
					}
				} 
				else {
					isChecked = this.checked;
				}
		}})());
	},
	videoDialog: function() {
		
		$('#flashdialog').dialog({
			autoOpen: false,
			width: 527,
			height: 362
		});

		$('#dialog_link').click(function(){
			$('#flashdialog').dialog('open');
			return false;
		});
	},
	jSVideoSetup: function(){
		 VideoJS.setupAllWhenReady();	
	},
	
	log: (typeof console != 'undefined' ? function(msg) { if (VikanApp.loggingEnabled ) { console.log(msg); }} : function() {})
	
};

// User Object - uses cookie for storage for now, but ideally should use sessionStorage, if supported by browser
var VikanUser = {
	
	setProperty: function(key, value) {
		$.cookie(key, value, { expires: 7, path: '/', domain: VikanApp.cookieLocation});
	},
	
	getProperty: function(key) {
		return $.cookie(key);
	},
	
	removeProperty: function(key) {
		// Not implemented
	},

	clear: function() {
		// Not implemented
	}
}

// Startup
$(VikanApp.mainInitializer);
