$(document).ready(function(){
	
	// Main navigation menu
	$('.level1 li').hoverIntent({
		sensitivty: 1,
		interval: 50,
		over: function(){ $(this).children('.level2').slideDown('fast'); },
		timeout: 300,
		out: function(){ $(this).children('.level2').slideUp('fast'); }
	});
	
	// Search box placeholder for non-webkit browsers
	if(!$.browser.safari){
		$('#query').css('color', '#a9a9a9').val('SEARCH');
		$('#query').blur(function(){
			if ($(this).val() == '') $('#query').css('color', '#a9a9a9').val('SEARCH');
		});
		$('#query').focus(function(){
			if ($(this).val() == 'SEARCH') $('#query').css('color', '#000').val('');
		});
	}
	
	// Featured content box	
	var bgpos    = new Array();
		bgpos[0] = -260;
		bgpos[1] = -130;
		bgpos[2] = 0;
		
		$('#featured-content .item:not(:first-child)').css({display:'none', marginTop:-20});
		$('#featured-content .item:first-child').addClass('visible');
		var last_item_visible = $('.visible').index();
		function changeItem(item) {
			
			var direction = 0;
			
			if (item == null) { // If timer fired
				
				var next = last_item_visible + 1;
				
				if (next == 3) {
					next = 0;
				}
				
				if (last_item_visible < next) {
					direction = -20;
				} else if (last_item_visible > next) {
					direction = 20;
				}
				
				/*
				
				$('#featured-content .item:not(:eq(' + next + '))').animate({
					marginTop: direction
				}, 200, function(){ $(this).css({'display': 'none'}); }).removeClass('visible');
				
				$('#featured-content .item:eq(' + next + ')').css({'display': 'block'}).animate({
					marginTop: 0
				}, 200).addClass('visible');
				
				*/
				
				$('#featured-content .item:not(:eq(' + next + '))').stop().animate({
					marginTop: direction
				}, 100, function() {
					
					$(this).css({display:'none'});
					
					$('#featured-content .item:eq(' + next + ')').css({display:'block'}).stop().animate({
						marginTop: 0
					}, 100).addClass('visible');
					
				}).removeClass('visible');
				
				$('#featured-list').stop().animate({backgroundPosition: '(0 ' + bgpos[next] + 'px)'}, {duration: 200});
				
				last_item_visible = next;
				
			} else { // If user interacted
				
				if (last_item_visible < item) {
					direction = -20;
				} else if (last_item_visible > item) {
					direction = 20;
				}
				
				last_item_visible = item;
				
				/*
				
				$('#featured-content .item:not(:eq(' + item + '))').animate({
					marginTop: direction
				}, 200, function(){ $(this).css({'display': 'none'}); }).removeClass('visible');
				
				$('#featured-content .item:eq(' + item + ')').css({'display': 'block'}).animate({
					marginTop: 0
				}, 200).addClass('visible');
				
				*/
				
				$('#featured-content .item:not(:eq(' + item + '))').stop().animate({
					marginTop: direction
				}, 100, function() {
					
					$(this).css({display:'none'});
					
					$('#featured-content .item:eq(' + item + ')').css({display:'block'}).stop().animate({
						marginTop: 0
					}, 100).addClass('visible');
					
				}).removeClass('visible');
				
				$('#featured-list').stop().animate({backgroundPosition: '(0 ' + bgpos[item] + 'px)'}, {duration: 200});
				
				last_item_visible = item;
			}
		}
	
	$(document).everyTime(8000, function(i){
		changeItem();
	});
	$('#featured-list .item-link').click(function(){
		$(document).stopTime();
		changeItem($(this).index());
	});
	
	// Client room username/password error
	function checkform(){
		if(document.ExtUserForm.Username.value.length <= 0){
			alert('Specify username');
			document.ExtUserForm.Username.focus();
			return false;
		}
		
		if(document.ExtUserForm.Password.value.length <= 0){
			alert('Specify password');
			document.ExtUserForm.Password.focus();
			return false;
		}
		
		return true;
	}
	
	$('#extranet-login').live('submit', function(){
		
		if ($(this).children('#username').val().length == 0) {
			alert('Specify username');
			$(this).children('#username').focus();
			return false;
		}
		
		if ($(this).children('#password').val().length == 0) {
			alert('Specify password');
			$(this).children('#password').focus();
			return false;
		}

		/*
		var room = $('#room').val();
		window.location = 'http://kemetyl.net.dynamicweb.no/clientroom/' + room + '/home.aspx';
		return false;
		*/
		
		//$(this).attr('action', 'http://kemetyl.net.dynamicweb.no/clientroom/' + $(this).children('#room').val() + '/home.aspx');
		
		return true;
	});
	
	// Client Room login box
	$('#clientroom').append('<div id="client-room-login"><form action="default.aspx?ID=41" method="post" id="extranet-login"><strong>Client room login:</strong><div class="label"><label for="room">Client:</label> <input type="text" id="room" name="room" /></div><div class="label"><label for="username">Username:</label> <input type="text" id="username" name="Username" /></div><div class="label"><label for="password">Password:</label> <input type="password" id="password" name="Password" /> <button type="submit" id="submit" name="submit">OK</button></div></form></div>');
	$('#client-room-login').css('display', 'none');
	
	// Local Sites  //onclick="_gaq.push(\[\'_link\', \'http://www.kemetyl.no\'\]); return false;"
	$('#localsites').append('<ul><li class="active"><a href="http://www.kemetyl.com"><img src="/Files/System/web/gfx/localsites/uk.gif" /> Global</a></li><li><a href="http://www.kemetyl.no"><img src="/Files/System/web/gfx/localsites/no.gif" alt="" /> Norway</a></li></ul>');
		//<li><a href="#se"><img src="/Files/System/web/gfx/localsites/se.gif" alt="" /> Sweden</a></li><li><a href="#nl"><img src="/Files/System/web/gfx/localsites/nl.gif" alt="" /> Netherlands</a></li></ul>')
	$('#localsites ul').css('display', 'none');

	// Client Room login box - NO
	$('#kunderom').append('<div id="client-room-login"><form action="default.aspx?ID=41" method="post" id="extranet-login"><strong>Innlogging, kunderom:</strong><div class="label"><label for="room">Kunde:</label> <input type="text" id="room" name="room" /></div><div class="label"><label for="username">Bruker:</label> <input type="text" id="username" name="Username" /></div><div class="label"><label for="password">Passord:</label> <input type="password" id="password" name="Password" /> <button type="submit" id="submit" name="submit">OK</button></div></form></div>');
	$('#client-room-login').css('display', 'none');

	// Local Sites - NO  //onclick="_gaq.push(\[\'_link\', \'http://www.kemetyl.com\'\]); return false;"
	$('#lokalenettsteder').append('<ul><li><a href="http://www.kemetyl.com"><img src="/Files/System/web/gfx/localsites/uk.gif" />  Global</a></li><li class="active"><a href="http://www.kemetyl.no"><img src="/Files/System/web/gfx/localsites/no.gif" alt="" />  Norge</a></li></ul>');
		//<li><a href="#se"><img src="/Files/System/web/gfx/localsites/se.gif" alt="" /> Sweden</a></li><li><a href="#nl"><img src="/Files/System/web/gfx/localsites/nl.gif" alt="" /> Netherlands</a></li></ul>')
	$('#lokalenettsteder ul').css('display', 'none');
	
	
	$('#clientroom, #kunderom').hoverIntent({
		sensitivity: 1,
		interval: 50,
		over: function(){ $(this).children('#client-room-login').slideDown('fast'); },
		timeout: 300,
		out: function(){ $(this).children('#client-room-login').slideUp('fast'); }
	});
	
	
	$('#lokalenettsteder, #localsites').hoverIntent({
		sensitivity: 1,
		interval: 50,
		over: function(){ $(this).children('ul').slideDown('fast'); },
		timeout: 300,
		out: function(){ $(this).children('ul').slideUp('fast'); }
	});
	
	$('#clientlogo.Jernia').html('<a href="http://www.jernia.no" target="_blank">Jernia</a>');
	
});
