$(document).ready(function() { 
	/*console.log("1");
	
	$('#right').append('<div id="loader">Laster...</div>').delay(5000).queue(function(){ 
		console.log("2");
		$('<iframe />', {
			id: 'frame_right',
			class:   'db',
			src:   'http://dagbladet.no/',
			width:   '50%',
			seamless:   'seamless',
		}).appendTo('footer');
		$(this).dequeue(); 
	});
	
	console.log("3");
	
	
	
console.log("1");
$('#right').load('index2.html', function() {
      console.log("2");
});
console.log("3");


    var height = $(window).height() - 41;
	$('#frame_left,#frame_right').attr('height', height + 'px');
	
	
		$("<div>").load("index2.html #load", function() {

		$('#right').append('<div id="loader">Laster...</div>').delay(5000).queue(function(){ 
																						   
																						   console.log("2");
			$("#right").append($(this).find("#load").html());
			$("#loader").hide();
			
			console.log("3");
					   
			
			$('#frame_left,#frame_right').attr('height', height + 'px');
		});
	});

	var iframe = $("#frame_left").attr('src');
	$('#frame_left,#frame_right').attr('src', iframe);
	
	$('body').delay(15000).queue(function(){ 
		console.log("Delay");
		
	
		$(this).dequeue(); 
	});
	
	*/
	

	var timer;
	
	function refresh_frames (){
		timer = setTimeout( function() {
			var frame_left = $("#frame_left").attr('src');
			$('#frame_left').attr('src', frame_left);
			
			var frame_right = $("#frame_right").attr('src');
			$('#frame_right').attr('src', frame_right);
			
			console.log("Refresh");
			var autorefresh = localStorage.getItem('autorefresh');
			
			refresh_frames ();
		}, 300000 );
	}

	
	var height = $(window).height() - 41;
	$('#frame_left,#frame_right').attr('height', height + 'px');
	
	$(window).resize(function() {
		var height = $(window).height() - 41;
		$('#frame_left,#frame_right').attr('height', height + 'px');
	});

	$("#close").live("click", function(){
		$("#open").hide();
		$('header').slideUp('slow', function() {
			$("#open").fadeIn();
		});
	});
	
	$("#open").live("click", function(){
		$('#open').fadeOut('fast', function() {
			$("header").slideDown();
		});
	});
	


	/* Check if a autorefresh is set */
	var autorefresh = localStorage.getItem('autorefresh');
	console.log(autorefresh);
	
	if ( autorefresh == null) {
		/* Set autorefresh */
		localStorage.setItem('autorefresh', 'on');
		$('#autorefresh').css('opacity', '1');
		
		refresh_frames ();
	} else {
		if ( autorefresh == 'on') {
			$('#autorefresh').css('opacity', '1');
			
			refresh_frames ();
		} else {
			$('#autorefresh').css('opacity', '0.3');
			
			clearTimeout (timer);

		}
	}
	
	/* autorefresh switcher */
	$('#autorefresh').live('click', function() {
		var autorefresh = localStorage.getItem('autorefresh');

		if ( autorefresh == 'off') {
			localStorage.setItem('autorefresh', 'on');
			$('#autorefresh').css('opacity', '1');
			
			refresh_frames ();
		} else {
			localStorage.setItem('autorefresh', 'off');
			$('#autorefresh').css('opacity', '0.3');
			
			clearTimeout (timer);

		}

      return false;
   });
	
	$('a.poplight[href^=#]').click(function() {
		var popID = $(this).attr('rel'); //Get Popup Name
		var popURL = $(this).attr('href'); //Get Popup href to define size
	
		//Pull Query & Variables from href URL
		var query= popURL.split('?');
		var dim= query[1].split('&');
		var popWidth = dim[0].split('=')[1]; //Gets the first query string value
	
		//Fade in the Popup and add close button
		$('#' + popID).fadeIn().css({ 'width': Number( popWidth ) }).prepend('<a href="#" class="close"><img src="gfx/close.png" class="btn_close" title="Lukk vindu" alt="Lukk" /></a>');
	
		//Define margin for center alignment (vertical   horizontal) - we add 80px to the height/width to accomodate for the padding  and border width defined in the css
		var popMargTop = ($('#' + popID).height() + 80) / 2;
		var popMargLeft = ($('#' + popID).width() + 80) / 2;
	
		//Apply Margin to Popup
		$('#' + popID).css({
			'margin-top' : -popMargTop,
			'margin-left' : -popMargLeft
		});
	
		//Fade in Background
		$('body').append('<div id="fade"></div>'); //Add the fade layer to bottom of the body tag.
		$('#fade').css({'filter' : 'alpha(opacity=80)'}).fadeIn(); //Fade in the fade layer - .css({'filter' : 'alpha(opacity=80)'}) is used to fix the IE Bug on fading transparencies 
	
		return false;
	});

	//Close Popups and Fade Layer
	$('a.close, #fade').live('click', function() { //When clicking on the close or fade layer...
    	$('#fade , .popup_block').fadeOut(function() {
        $('#fade, a.close').remove();  //fade them both out
		});
		return false;
	});

	var r1085271 = 'post';
	var b0185165 = 'vgdb.no';
	$('#usermail').html('<a href="mailto:' + r1085271 + '@' + b0185165 + '">' + r1085271 + '@' + b0185165 + '</a>')
});


