/* GENERAL STUFF */
var isIE=(navigator.appName.indexOf("Microsoft")!=-1)?1:0;//may be useful
var currentRenderedLink = false;//currently rendered link

//show / hide the flash on main page
function toggleFlash(show){
	if(show){
		$('#main_flash').show();		
		$('#main_flash_dummy').css('visibility', 'hidden');
 		$('#menu_bottom_flash').css('visibility', 'hidden');
 		$('#body_footer .wrapper').addClass('nobg');
	}
	else{
		$('#main_flash_dummy').css('visibility', 'visible');
		$('#main_flash').hide();
 		$('#menu_bottom_flash').css('visibility', 'visible');
 		$('#body_footer .wrapper').removeClass('nobg');
	}
}

//renders the bottom link if needed
function renderBookNowLink(link){
	//if empty link
	if(link == ''){
		$('#body_inline_book_now').css('display', 'none');
		return;
	}
	//check if starts with http
	$('#body_inline_book_now').attr('href', link).attr('target', link.substr(0,4).toLowerCase()=='http'?'_blank':'').css('display', 'block');
}

//renders the inline content from a link
function renderInlineContent(link, setCurrentAnchorLinkToo, extraData, type){
	//hide book now link by default
	$('#body_inline_book_now').css('display', 'none');

	if(typeof(extraData) == 'undefined') extraData = '';
	if(typeof(type) == 'undefined') type = 'GET';
	
	//generate the rendered link
	newCurrentRenderedLink = link+'?t='+type+'&'+extraData;

	//check if the link is the same or there is any link
	if(currentRenderedLink == newCurrentRenderedLink) return false;
	//replace the current link with the new link	
	currentRenderedLink = newCurrentRenderedLink;
	
	$.fancybox.close();
	$.fancybox.showActivity();
	setAnchorText(link, setCurrentAnchorLinkToo);
	$.ajax({
	  url: link,
	  type: type,
	  data: 'ajax=1'+(extraData.length>0?('&'+extraData):''),
	  success: function(data) {
	  	data = $.trim(data);
	    $('#all_txt').html(data);
	    if(data.length != 0){
	  		if($('#main_flash_dummy').css('visibility') == 'hidden'){
				toggleFlash(false);
	  			$('#body_inline_all').hide().fadeIn('normal');
	  		}
  		}
  		else if($('#main_flash_dummy').css('visibility') != 'hidden'){
			$('#body_inline_all').fadeOut('fast', function(){
				toggleFlash(true);
				$('#body_inline_all').show();
			});
  		}
		prepareLinksForAjax();
		$.fancybox.hideActivity();
	  }
	});
}

//prepares links for being accessed trough ajax if internal
function prepareLinksForAjax(){
	$('a[href*=\''+localLink+'\']').each(function(){
		if(!$(this).hasClass('preparedForAjax')){
			$(this).click(function(){
				renderInlineContent($(this).attr('href'), true);
				return false;
			})
			$(this).addClass('preparedForAjax');
		}
	});
}

/* ANCHOR MANIPULATION FUNCTIONS */
if(typeof(currentAnchorLink) == 'undefined') var currentAnchorLink = false;

//get anchor text
function getAnchorText(excludeKeys){
	return document.location.hash.substr(1);
}

//set anchor text
function setAnchorText(anchorText, setCurrentAnchorLinkToo){
	document.location.hash = anchorText;
	if(typeof(setCurrentAnchorLinkToo) != 'undefined' && setCurrentAnchorLinkToo) currentAnchorLink = anchorText;
}

//go to link pointed by anchor text
function refreshAnchorLink(){
	if(currentAnchorLink != getAnchorText()){
		currentAnchorLink = getAnchorText();
		renderInlineContent(currentAnchorLink, false);
	}
	setTimeout('refreshAnchorLink();', 100);
}

//rewrites the email addresses so they can't be read by the bots
function writeEmailAddress(domain, user, tld, extra, boxId){
	var html = '<a href="mai'+'lto:'+user+'@'+domain+'.'+tld+'" '+extra+'>'+user+'@'+domain+'.'+tld+'</'+'a>';
	if(typeof(boxId) == 'undefined') document.write(html);
	else document.getElementById(boxId).innerHTML = html;
}

//submit the search
function submitSearch(searchLink){
	var searchString = $.trim($('#iptMainSearch').val());
	if(searchString == '') return false; 
	renderInlineContent(searchLink+'/'+escape(searchString), true);
	return false;
}

function extendSuperfishLayerHeight(){
	if($('#menu ul:visible').length > 0) $('#body_header').height(700);
	else if( $('#body_header').height() != 51) $('#body_header').height(51);
	setTimeout('extendSuperfishLayerHeight();', 100);
}

function fancyAlert(msg, title) {
	var options = {
        //'modal' : true,
        'content' : '<div style="margin:1px;width:350px;">'+msg+'</div>',
        'centerOnScroll' : true
    };
    if(typeof(title) != 'undefined' && title != '') options['title'] = title;
    jQuery.fancybox(options);
}

/* INITALIZATION */
$(document).ready(function(){
	//superfish menu
	$('#menu').superfish();
	
	var z = $("#all_txt").css("z-index");
	
	//add bgiframe to some divs
	$('#body_footer,#body_content,#main_logo,#body_footer').bgiframe();
	
	//load flash
	if(!sIsMobile){
		$('#main_flash').flash({ 
			src: localLink+'resources/superland/flash/intro.swf',
			height: 900,
			width: '100%',
			wmode: 'transparent'
		});			
		$('#menu_bottom_flash').flash({ 
			src: localLink+'resources/superland/flash/btm_menu.swf',
			height: 100,
			width: 1200,
			wmode: 'transparent'
		}).css('visibility', 'hidden');				
	}

	//inline close button action
	$('#body_inline_close').click(function(){
		$('#body_inline_all').fadeOut('fast', function(){
			toggleFlash(true);
			$('#body_inline_all').show();
		});
		//return false;
	});
	
	//prepare the links for being opened with ajax	
	prepareLinksForAjax();
		
	//initialize anchor link if any and make back button work
	refreshAnchorLink();
	
	//empty search field on focus and fill search field on blur (if necessary)
	$('#iptMainSearch').focus(function(){ $(this).val(''); });
	$('#iptMainSearch').blur(function(){ if($.trim($(this).val()) == '') $(this).val('Søk i våre nettsider'); });
	
	//newsletter for inline pages
	$('#body_inline_frm_nwl').submit(function(){
		$.fancybox.showActivity();
		$.ajax({
			url: localLink+'modules/newsletter/ajax/subscribe.php',
			type: 'POST',
			data: 'email='+escape($('#body_inline_ipt_nwl').val()),
			success: function(json) {
				eval('var result = '+json);
				fancyAlert(result.message);
				if(result.status == 'ok') $('#body_inline_nwl').hide();
				$.fancybox.hideActivity();
			}
		});
		return false;
	});
	$('#body_inline_ipt_nwl').focus(function(){
		if($(this).val() == 'skriv inn din e-postadr.') $(this).val('');
	});
	$('#body_inline_ipt_nwl').blur(function(){
		if($(this).val().trim() == '') $(this).val('skriv inn din e-postadr.');	
	});
});
