function postToFacebook(tmpe){
			
	FB.Connect.get_status().waitUntilReady( function( status ) {

		if(status == 1){
			
//			FB.Facebook.apiClient.users_hasAppPermission("publish_stream",function(res){
//				if(res != 1){
//					FB.Connect.showPermissionDialog('publish_stream', null, null);			
//				}
//			});
	
		    FB.Connect.streamPublish(
		      "", 
		      { 
		        name: tmpe.parent().parent().find('.left a').text(),
		        href: tmpe.parent().parent().find('.left a').attr('href'),
		        caption: 'Mdg.no',
		        description: ""
		      },  
		      null,  
		      null, 
		      '', 
		      function() {
		        $.modal.close();
		      }
		    );	
		}			
	});
}

__show_menu__ = false;

function extract_data(){
		
		var sub = '';
		var vol = '';
		var bday = '01';
		var bmonth = '01';
		var byear = '2001';
		
		if($("input[name='member_subscribe']").attr('checked')){
			sub = 'on';
		}
		
		if($("input[name='opt_checkbox01']").attr('checked')){
			vol = 'on';
		}

		if($("select[name='bday']").val()){
			bday = $("select[name='bday']").val();
		}
		
		if($("select[name='bday']").val()){
			bmonth = $("select[name='bmonth']").val();
		}	
		
		if($("select[name='bday']").val()){
			byear = $("select[name='byear']").val();
		}

		var data = {
			
//			'member_firstname' : $("input[name='member_firstname']").val(),
//			'member_lastname' : $("input[name='member_lastname']").val(),
//			'member_email' : $("input[name='member_email']").val(),
//			'member_subscribe' :  sub ,
//			'member_addr1' : $("input[name='member_addr1']").val(),
//			'member_postalcode' : $("input[name='member_postalcode']").val(),
//			'opt_textbox01' : $("input[name='opt_textbox01']").val(),
//			'member_phone' : $("input[name='member_phone']").val(),
//			'opt_checkbox01' : vol,
//			'member_comments' : $("textarea[name='member_comments']").val(),
//			'member_birthdate' : bday + "." + bmonth + "." + byear  

			'fornnavn' : $("input[name='member_firstname']").val(),
			'etternavn' : $("input[name='member_lastname']").val(),
			'epost' : $("input[name='member_email']").val(),
			'nyhetsbrev' :  sub ,
			'adresse1' : $("input[name='member_addr1']").val(),
			'postnr' : $("input[name='member_postalcode']").val(),
			'opt_textbox01' : $("input[name='opt_textbox01']").val(),
			'telefon' : $("input[name='member_phone']").val(),
			'opt_checkbox01' : vol,
			'kommentar' : $("textarea[name='member_comments']").val(),
			'dato' : bday + "." + bmonth + "." + byear  
		}
		
		return data;
}

function set_field_data(key,val){

	$("input[name='" + key + "']").val(val);

}
	
$(document).ready(function(){

//$("	#post-to-facebook-open").modal({minHeight:250});
//	console.warn(FBConnect);

	$("#logout-all").click(function(e){

		//FBConnect.logout();
		//FB.Connect.get_status().
		
		FB.Connect.get_status().waitUntilReady( function( status ) {
			if(status == 1){
				FBConnect.logout();
				e.preventDefault();
			}
		});

	});
	
	/** initialize scrollable for for front page candidates **/
	
	$(".scrollable").scrollable();
	
	$(".can-hide:first").show();	
	$(".scrollable a:first").attr('class','current');
	
		
	$(".scrollable a").bind( 'click', function(e){
		$(".scrollable a").each(function(){
			$(this).attr('class','');	
		});
		
		$(".can-hide").each(function(){
			$(this).hide();	
		});	
		
		$("#can-" + $(this).attr('title') ).show();
		
		$(this).attr('class','current');
		e.preventDefault();
	});
	
	

	$(".addthis_button").html('Del');
	
	/** Login menu animation **/
	
	$("#close-menu").click(function(){
		$("#top-detail").animate({'height': 4});
		$(this).hide();		
	});
	
	
	$("#top-detail").mouseover(function(){
		if($(this).css('height') == "4px"){
			$(this).animate({'height': 26});
			$("#close-menu").show();
		}		
	});
	
	
	/** Panel menu animation**/

	
	$("#green-top .right").mouseover(function(){
		$("#green-blogs").show();	
	});
	
	$("#green-blogs").bind({
		  mouseleave: function(){
		   $(this).hide();
		 }
	});
	
	
	/** Panel sub-blogs animation**/

	

	$('.page-item-7').hover(function(){
          $("#panel").show();	
       },
       function() {

        	$("#panel").hide();	  	
    });

	/** Front page posts show options**/
	
	$("#most-liked-posts-click").click(function(e){
		e.preventDefault();
		$(this).attr('class','current');
		$("#latest-posts-click").attr('class','');
		$("#latest-posts").hide();
		$("#most-liked-posts").show();	
	});

	$("#latest-posts-click").click(function(e){
		e.preventDefault();
		$(this).attr('class','current');
		$("#most-liked-posts-click").attr('class','');
		$("#most-liked-posts").hide();	
		$("#latest-posts").show();
	});


	$("#most-liked-politics-click").click(function(e){
		$(this).attr('class','current');
		$("#most-liked-latest-click").attr('class','');
		e.preventDefault();
		$("#politics-latest").hide();
		$("#politics-liked").show();	
	});

	$("#most-liked-latest-click").click(function(e){
		$(this).attr('class','current');
		$("#most-liked-politics-click").attr('class','');
		e.preventDefault();		
		$("#politics-liked").hide();	
		$("#politics-latest").show();
	});
	
	
	$("#events-past-click").click(function(e){
		$(this).parent().attr('class','current');
		$("#events-next-click").parent().attr('class','');
		e.preventDefault();
		$("#events-next").hide();
		$("#events-past").show();	
		
	});

	$("#events-next-click").click(function(e){
		$(this).parent().attr('class','current');
		$("#events-past-click").parent().attr('class','');
		e.preventDefault();
		$("#events-past").hide();	
		$("#events-next").show();
	});
	

	// For like counting
	var status = 0;
		
	$("#ns-signup").click(function(){
		$("#signup-open").modal({minHeight:250,minWidth: 500});
 
	});

	$("#ns-signup-post").click(function(){
	
		$.post('/?__action__=signup',$("#signup-open form").serialize(),function(data){
			$("#signup-open").html(data);
		});
	});

	
	$("#ns-sigin").click(function(){
		$("#login-open").modal({minHeight:250,minWidth: 500});
	});
	
	
	$("#password-field-enter").bind('keypress',function(e){
		if(e.keyCode == 13){
			$("#ns-signin-post").trigger('click');
		}
	});

	$("#ns-signin-post").bind('click',function(){
		$.post('/?__action__=login',$("#login-open form").serialize(),function(data){
			if(data == "<h1 class='center'>Du er logget inn</h1>"){
				setTimeout(function(){
					window.location.href = window.location.href;
				}, 1000);				
			}
			$("#login-open").html(data);
		});
	});
	
	$("#login-register-open-login").click(function(){
		$.post('/?__action__=login',$("#login-register-open .left form").serialize(),function(data){
			if(data == "<h1 class='center'>Du er logget inn</h1>"){
				setTimeout(function(){
					window.location.href = window.location.href;
				}, 1000);				
			}
			$("#login-register-status").html(data);
		});
	});
	
	$("#login-register-open-register").click(function(){
		$.post('/?__action__=signup',$("#login-register-open .right form").serialize(),function(data){
			$("#login-register-status").html(data);
		});
	});
	
	$("#save-fb-settings").click(function(){
		
		if($("#share-true:checked").length){
			$.post('/?__update_settings__=true',{'share':1},function(data){
				$.modal.close();
				var tmpId = $("#"+ $("#post-to-facebook-open").data('salike') );
				postToFacebook(tmpId);	
			});
		}
		else{
			$.post('/?__update_settings__=true',{'share':0},function(data){
				__show__ = "off";
				$.modal.close();	
			});
		}

		if(	__show__ == "on"){
			var tmpId = $("#"+ $("#post-to-facebook-open").data('salike') );
			postToFacebook(tmpId);	
		}
	
	});

	/*
		Like functionality
	*/
	
	$("div.like").bind('click',function(){
		
		if (__logged__)
		{
			var tmpe = $(this);
			
			if(__show__ != "on"){
				$("#post-to-facebook-open").data('salike', tmpe.attr('id') );
				$("#post-to-facebook-open").modal({minHeight:350});
			}
			else{
				$("#post-to-facebook-open").data('salike', tmpe.attr('id') );
				var tmpId = $("#"+ $("#post-to-facebook-open").data('salike') );
				postToFacebook(tmpId);
			}
			
			$.post(window.location.href, {'ilikeit': $(this).find('.ent').text()}, function(data){
				tmpe.find('.con').text( parseInt(tmpe.find('.con').text()) + 1 );
				tmpe.unbind();
			});
		}
		else{
			$("#login-register-open").modal({minHeight:250});
		}

	});
	
	$("div.like").bind('mouseover',function(){
		
		status = $(this).find('.con').text();
		
		$(this).find('.con').text('+1');
		$(this).find('.mask').show();
	//	$(this).css('height','32px');

	});
	
	
	$("div.like").bind('mouseout',function(){
		$(this).find('.con').text(status);
		$(this).find('.mask').hide();
	//	$(this).css('height','16px');
	
	});

	$("#reg-web-click, #bli-main-menu").bind('click',function(e){
		e.preventDefault();

		//$("#reg-web-form-open").modal( {onShow: function(){ $('#simplemodal-container').css('height', 'auto'); } } );

		$("#reg-web-form-open").modal({minHeight:350,minWidth:500});
		$("#reg-web-form-open-0").css('display','block');
	});
	
	
	$(".rw-action").bind('click',function(){

		var next = $(this).attr('alt');
		var pos = $(this).parent().parent().parent().parent();
		pos.css('display','none');
				
		if(pos.attr('id') == "reg-web-form-open-6")
		{
			var data = extract_data();
			$("#dyn-form-data").html( "<p>" + data.fornnavn +  " " +  data.etternavn + "</p><p>" + data.adresse1 + "</p><p>" + data.postnr + " " + data.opt_textbox01 + "</p><p>" +  data.epost + "</p><p>" +  data.telefon + "</p>");
		}
		
		$("#reg-web-form-open-" + next).css('display','block');

	})
	
	$("#rw-send").bind('click',function(){
		var data = extract_data();
			
		$.post( "?__api_call_rw__=1", data , function(res){
			$.modal.close();	
		});
	
	});


	$("a#next").bind('click',function(e){
		
		$("div.items div > a").each(function(){
			if( $(this).attr('class') == "current" ) {

				if($(this).next().attr('title')){
					$(this).attr('class','');					
					$(this).next().attr('class','current').trigger('click');
				}
				return false;
			} 
		});

		e.preventDefault();		
	});
	
	$("a#prev").bind('click',function(e){
		
		$("div.items div > a").each(function(){
			if( $(this).attr('class') == "current" ) {

				if($(this).prev().attr('title')){
					$(this).attr('class','');					
					$(this).prev().attr('class','current').trigger('click');
				}
				return false;
			} 
		});

		e.preventDefault();		
	});
	
	$("#candidates-sc .attachment-medium").attr( { 'width' : '225', 'height': '300' } );

});