$(document).ready(function(){

	if($('#nav ul>li.selected').length <= 0){
		$('#nav ul>li:first').addClass('selected');
	}
	$('#main div.sitePath span span:nth-child(n+3)').addClass('on');

    /*
    if($('#top .serviceNav ul:last li').length <= 0){
		$('#top .serviceNav div.navSplit').addClass('skip');
	}
	*/
	
    /* Temp Disabled */
    /*
	$.sifr({
		path: '/Style%20Library/VELUX/Includes/',
		font: 'VeluxGothic', 
		save: true
	});
	$('.spot h1, .spot h2, div.teaser h2').sifr({  });
	*/
	
    $('#content .spot>ul>li.hasPopUpLinks>a').toggle(
      function () {
         $(this).parent('li').addClass('on');
      },
      function () {
        $(this).parent('li').removeClass('on');
      }
    );
	
	$('#main a.printButton').attr('href','javascript:window.print();');
	
	$('#main .sitePath a.contact').click(function () {
      $('#main .sitePath div.recBox').slideToggle('fast');
    });
    
    $('#main .sitePath div.recBox a.addRecipient').click(function () {
    	var email = $(this).parent().find('input').attr('value');
    	var $error = $(this).next('div.error');
    	if(valEmail(email))addRecipient($(this));
    });
    
    $('#main .sitePath div.recBox input.valEmail').blur(function () {
    	doInputField($(this));	
    });
    
    
	$('#main .sitePath div.recBox div.submit input').click(function () {
		$('#main .sitePath div.recBox input.valEmail').each(function () {
	    	myState = doInputField($(this));
	    });
	    return myState;
	    //$('#main .sitePath div.recBox textarea').attr('value',$('#main .sitePath div.recBox input').eq(0).attr('value'));
    });
	
    	
	$('#main .faqList dt').click(function () {
      $(this).next().slideToggle('fast');
    });
     
     $('#main .flashObject').each(function () {
    	  $(this).flash(
    	    { 
    	      src: $(this).find('.SWFFile').attr('href'),
    	      width: $(this).find('.SWFWidth').html(),
    	      height: $(this).find('.SWFHeight').html(),
    	      wmode: 'transparent',
    	      flashvars: { url1: $(this).find('.XMLFile').attr('href') }
    	    },
    	    { version: 8 }
    	);
    });    
	
    $('#content .pgThumbs .thumb').click(function(){
		showGalleryItem($(this));
    });
    $('#content .pgThumbs .thumb').hover(
    	function () {
         $(this).addClass('over');
      },
      function () {
        $(this).removeClass('over');
      }

    );
    $('iframe').parent('.ms-WPBody').css('overflow','hidden');
    
    $('#main table.customLayoutGrey > tr:nth-child(even), #main table.customLayoutGrey tbody > tr:nth-child(even), #main table.customLayoutBlue > tr:nth-child(even), #main table.customLayoutBlue tbody > tr:nth-child(even), #main table.customLayoutPurple > tr:nth-child(even), #main table.customLayoutPurple tbody > tr:nth-child(even)').addClass('odd');
    $('#main table.customLayoutGrey > tr:first-child, #main table.customLayoutGrey tbody > tr:first-child, #main table.customLayoutBlue > tr:first-child, #main table.customLayoutBlue tbody > tr:first-child, #main table.customLayoutPurple > tr:first-child, #main table.customLayoutPurple tbody > tr:first-child').addClass('th');
    
});

function doInputField($input){
	var $recBox = $('#main .sitePath div.recBox');
	var email = $input.attr('value');
	var index = $('#main .sitePath div.recBox input.valEmail').index($input);
	
	var $error = $recBox.find('div.error').eq(index);
	
	if(valEmail(email)){
		$error.addClass('skip');
		if(index > 0)addRecipientMail(email);
		return true;
	}
	else{
		$error.removeClass('skip');
		return false;
	}
}

function addRecipient($recipient){
	$recipientLine = $recipient.parent().clone();
   	$recipient.parent().find('input').attr({value: ''});
   	
   	$recipientLine.find('input').attr({disabled: 'disabled'});
   	$recipientLine.find('a.addRecipient').attr('class','clearRecipient').click(function () {
    	clearRecipient($(this));
    	
    });
   	$recipient.parent().before($recipientLine);
}

function clearRecipient($recipient){
	$recipient.parent().remove();
	delRecipientMail($recipient.parent().find('input').attr('value'));
   	
}

function addRecipientMail(mail){
	$recipients = $('#main .sitePath div.recBox input').eq(0);
	currentRecipients = $recipients.attr('value');
	
	if(currentRecipients.indexOf(mail) == -1){
		currentRecipients += mail + ';'
		$recipients.attr('value',currentRecipients);
	}
}

function delRecipientMail(mail){
	$recipients = $('#main .sitePath div.recBox input').eq(0);
	currentRecipients = $recipients.attr('value');
	
	currentRecipients = currentRecipients.replace(mail + ';','')
	$recipients.attr('value',currentRecipients);
}

function showGalleryItem($item){

	var $thumbs = $('#content .pgThumbs');
	var $index = $thumbs.find('.thumb').index($item);
	
	var $img = $('#content .photoGallery img.pgView');
	var $desc = $('#content div.pgHTML');
	var $data = $('#content div.pgData>div');
	
	$item.siblings('.on').removeClass('on');
	$item.addClass('on');
	
	$img.attr({src:$item.find('img').attr('longdesc'),alt:$item.find('img').attr('alt')});
	$desc.html($data.eq($index).html());
}

function valEmail(email){	
	var pattern=/^([a-åA-å0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;
    return pattern.test(email);
}

try{
	document.execCommand('BackgroundImageCache', false, true);
}
catch(e){}