$(document).ready(function(){
	
  // var popup_state = getCookie('popup');
  // 
  // if(popup_state == null){
  //  show_popup();
  //  setCookie('popup', true);
  // }


	$(".share_message").live("click", share_content);
  // $("#close-overlay").live("click", close_overlay);

	$("#close-facebox").live("click", function(){
		$("#facebox_overlay").remove();
		$("#facebox").fadeOut("slow");
		
	});
	
	$("#reason").live("focus blur", function(){
		console.log("focus");
	});
	
	if($("#gallery-1").size() > 0){
	
	if($(".gallery-item").size() == 1){
		$(".gallery-item").eq(0).show();
		$(".gallery-nav").hide();
	}

	$("#gallery-1").find("br").remove();
	$("#total-items").text($(".gallery-item").size());
	get_picture_data();
	$('#gallery-1')
	.cycle({ 
   	 	fx:     'scrollLeft', 
    	speed:  'slow',
    	timeout: 0,
    	next:   '.gallery-next', 
    	prev:  '.gallery-prev',
    	prevNextClick: get_picture_data
	});

	
	}
	var $timeout;
	var $timeout_closer;
	$.each($(".main-menu li"), function(i){
		if($(this).find("ul").size() > 0){
			$(this).find("ul").addClass('hidden').addClass('dropdown_list');
			$(this).addClass('dropdown');
		}
	});
	
	$(".dropdown").hover(
		
	function () {
		clearTimeout($timeout_closer);
		$obj = $(this);
		this.timeout = setTimeout(function(){
    		
    		$obj.find(".dropdown_list").removeClass("hidden");
    	}, 100);
  	}, 
  	function () {
		var $object = $(this);
		$timeout = setTimeout(function(){
			$(".dropdown_list").addClass("hidden");
		}, 100);
  	});


	$(".dropdown_list", ".page_item").hover(
	function () {
    	clearTimeout($timeout);
  	}, 
  	function () {
  		$timeout_closer = setTimeout(function(){
  			$(this).addClass("hidden");
  		}, 100);
   		
   		
  	});
  	
  	
	$('#slide-front') 
		.cycle({ 
	   	 	fx:     'scrollLeft', 
	    	speed:  'slow', 
	    	timeout: 5000, 
	    	pager:  '#front-pager',
	    	next:   '.news-next', 
	    	prev:  '.news-prev' 
	});
	
	
	
	 $('.slideshow').cycle({
		fx: 'fade' // fade, scrollUp, shuffle, etc...
	}); 
	
	
		get_weekly_topic();
		$('.slide-weekly') 
		.cycle({ 
	   	 	fx:     'scrollLeft', 
	    	speed:  'slow', 
	    	timeout: 0, 
	    	next:   '.weekly-next', 
	    	prev:  '.weekly-prev',
	    	prevNextClick: get_weekly_topic
		});
	
	
	$("#front-pager a").text("");
	$('a[rel*=facebox]').facebox();
});

// function show_popup(){
//  $("body").append("<div class='overlay-bg'></div>");
//  $("body").append("<div class='popup-window'><span id='close-overlay'>Stäng</span></div>");
//  
//  
//  LeftPosition =(parseInt(window.screen.width)-1067)/2-140;
//  TopPosition=((parseInt(window.screen.height)-498)/2)-150;
// 
//  
//  $(".popup-window").css({
//    left: LeftPosition+"px",
//    top: TopPosition+"px"
//  });
//  
//  
// 
// }

// function close_overlay(){
//  $(".popup-window").remove();
//  $(".overlay-bg").remove();
//  
// }
// function setCookie(Name, Value)
// {
// var ArgumentCount;
// var ArgumentValues;
// var Domain;
// var Expires;
// var Path;
// var Secure;
// 
// ArgumentValues = setCookie.arguments;
// ArgumentCount = setCookie.arguments.length;
// 
// Expires = (ArgumentCount > 2) ? ArgumentValues[2] : null;
// Path = (ArgumentCount > 3) ? ArgumentValues[3] : null;
// Domain = (ArgumentCount > 4) ? ArgumentValues[4] : null;
// Secure = (ArgumentCount > 5) ? ArgumentValues[5] : false;
// 
// document.cookie = Name + "=" + escape (Value) +
// ((Expires == null) ? "" : ("; expires=" + Expires.toGMTString())) +
// ((Path == null) ? "" : ("; path=" + Path)) +
// ((Domain == null) ? "" : ("; domain=" + Domain)) +
// ((Secure == true) ? "; secure" : "");
// }
// 
// 
// function getCookie(Name)
// {
// var Argument;
// var ArgumetLength;
// var CookieLength;
// var EndString;
// var i;
// var j;
// 
// Argument = Name + "=";
// ArgumentLength = Argument.length;
// CookieLength = document.cookie.length;
// i = 0;
// while (i < CookieLength)
// {
// j = i + ArgumentLength;
// if (document.cookie.substring(i, j) == Argument)
// {
// EndString = document.cookie.indexOf (";", j);
// if (EndString == -1)
// EndString = document.cookie.length;
// return unescape(document.cookie.substring(j, EndString));
// }
// i = document.cookie.indexOf(" ", i) + 1;
// if (i == 0)
// break;
// }
// return (null);
// }
// 
// function set_popup_cookie(){
//  document.cookie = value;
// }


function get_weekly_topic(ifnext, eq, current){
	
	if(eq){
		$("#weekly-pager").text($(".slide-me-weekly").eq(eq).find("input").val());
	}
	else{
		$("#weekly-pager").text($(".slide-me-weekly").eq(0).find("input").val());
	}

}

function get_picture_data(ifnext, eq, current){
	if(eq){
		$("#gallery-topic").text($(".gallery-item").eq(eq).find("img").attr("alt"));
		$("#current-item").text(eq+1);
		$(".facebook").attr("share_url", 'http://www.facebook.com/sharer.php?u='+$(".gallery-item").eq(eq).find("a").attr("href")+'/&t='+$(".gallery-item").eq(eq).find("img").attr("alt"));
		$(".twitter").attr("href", 'http://twitter.com/home?status='+$(".gallery-item").eq(eq).find("img").attr("alt")+'%20-%20'+$(".gallery-item").eq(eq).find("a").attr("href")+'/');
	}
	else{
		
			
		$("#gallery-topic").text($(".gallery-item").eq(0).find("img").attr("alt"));
		$("#current-item").text(1);
		$(".facebook").attr("href", 'http://www.facebook.com/sharer.php?u='+$(".gallery-item").eq(0).find("a").attr("href")+'/&t='+$(".gallery-item").eq(0).find("img").attr("alt"));
		$(".twitter").attr("href", 'http://twitter.com/home?status='+$(".gallery-item").eq(0).find("img").attr("alt")+'%20-%20'+$(".gallery-item").eq(0).find("a").attr("href")+'/');
	}
	
	
		
		/*$("#gallery-topic").text($obj.find("img").attr("alt"));
		$("#current-item").text(i+1);
							
		$(".facebook").attr("href", 'http://www.facebook.com/share.php?u='+$obj.find("a").attr("href")+'/&t='+$obj.find("img").attr("alt"));
		$(".twitter").attr("href", 'http://twitter.com/home?status='+$obj.find("img").attr("alt")+'%20-%20'+$obj.find("a").attr("href")+'/');
		*/
		
	

}

function share_content(){
	if($(".share_content").hasClass('hidden')){
		$(".share_content").removeClass("hidden");
	}
	else{
		$(".share_content").addClass("hidden");
	}
}

