$(document).ready(function(){
  
  // Toggle states of elements depending on cookies
  var showNewsFeed = $.cookie('showNewsFeed');
  var showPRNews = $.cookie('showPRNews');	
  var showPONews = $.cookie('showPONews');
  var showFeature = $.cookie('showFeature');
  var showHand = $.cookie('showHand');

  if (showNewsFeed == 'collapsed') { toggle_and_change_link($("#newsfeed"), $("#newsfeed-hide"), showNewsFeed); }  	
  if (showPRNews == 'collapsed') { toggle_and_change_link($("#pr-news-container"), $("#pr-news-hide"), showPRNews); }
  if (showPONews == 'collapsed') { toggle_and_change_link($("#po-news-container"), $("#po-news-hide"), showPONews); } 	
  if (showFeature == 'collapsed') { toggle_and_change_link($("#feature"), $("#feature-hide"), showFeature); }
  if (showHand == 'collapsed') { toggle_and_change_link($("#hand"), $("#hand-hide"), showHand); }
	
  // Toggle vivisbility of sections if clicked	
  $("#newsfeed-hide").click(function(){
	  toggle_and_change_link($("#newsfeed"), $(this), 'showNewsFeed');
	  track_in_google($("#newsfeed"))
    return false;
  });
  
  $("#pr-news-hide").click(function(){
	  toggle_and_change_link($("#pr-news-container"), $(this), 'showPRNews');
    track_in_google($("#pr-news-container"))
    return false;
  });

  $("#po-news-hide").click(function(){
	  toggle_and_change_link($("#po-news-container"), $(this), 'showPONews');
    track_in_google($("#po-news-container"))
    return false;
  });
 
  $("#feature-hide").click(function(){
	  toggle_and_change_link($("#feature"), $(this), 'showFeature');
	  track_in_google($("#feature"))
	  return false;
  });
  
  $("#hand-hide").click(function(){
	  toggle_and_change_link($("#hand"), $(this), 'showHand');
	  toggle_element($("#winner"));
	  track_in_google($("#hand"))
	  return false;
  });
  
  $(".toggle_link a").click(function(){
    toggle_story($(this));
    return false;
  });
  
  /*var new_story = $("#next-news").text();
  var newsoption1 = {
    firstname: "po-news-content",
    secondname: "po-news",
    thirdname: "po-title",
    fourthname: "po-menu",
    playingtitle:  "Now Playing:&nbsp;",
    nexttitle:  new_story,
    prevtitle:  "Prev News:&nbsp;",
    newsspeed:  '8000',
    effectis:  '0',
    mouseover:  false,
    newscountname:  "test",
    disablenewscount:  false,
    imagedir: "/images/layout/widjets/po/"
  }
  $.init_news(newsoption1);*/

});

function toggle_and_change_link(element, link, cookie) {
  if (element.is(":hidden")) {  
    element.show(null, link.removeClass("hidden"));
	  $.cookie(cookie, 'expanded');
  } else {
	  element.hide(null, link.addClass("hidden"));
	  $.cookie(cookie, 'collapsed');
  }	
}

function toggle_element(element) {
  if (element.is(":hidden")) {  
    element.show();
  } else {
	  element.hide();
  }	
}

function track_in_google(element) {
  if (element.is(":hidden")) {
    pageTracker._trackEvent("Dashboard", element.attr("id"), "Show");
  } else {
    pageTracker._trackEvent("Dashboard", element.attr("id"), "Hide");
  }
}

function toggle_story(element) {
  if (element.parent().nextAll(".image").is(":hidden")) {
    element.parent().nextAll(".image").show();
    element.parent().nextAll(".article").children(".heading").children("h4").show();
    element.parent().nextAll(".article").children(".exerpt").show(null, element.parent().removeClass("plus"));
  } else {
    element.parent().nextAll(".image").hide();
    element.parent().nextAll(".article").children(".heading").children("h4").hide();
    element.parent().nextAll(".article").children(".exerpt").hide(null, element.parent().addClass("plus"));
  }
}