$(document).ready(function(){
  
  $('a[target=_self]').lightBox();

  var color = $("#colorvariation").val();
  
  $(".portfolioLink").mouseover(function(){
    $(this).find(".portfolioLinkHover").css("background", "url(/images/" + color + "_portfolio_link_hover.png)");
    $(this).find(".portfolioLinkHover").fadeIn(300);
  });
  $(".portfolioLinkHover").mouseout(function(){
    $(this).fadeOut(300);
  });

  $("#killercontent a").css("background", "#" + color);
  $("#column_two p a").css("color", "#" + color);
  $("#column_two .h1link").css("color", "#" + color);
  $(".error_list li").css("color", "#" + color);
  $("#blogroll a").css("color", "#" + color);
  $("#jobs a").css("color", "#" + color);
  $("#column_three a").css("color", "#" + color);
  $(".submitButton").css("background", "url(/images/killer_content_images/" + color + "_50.png)");
  $(".newsPagerButton").css("background", "url(/images/killer_content_images/" + color + "_100.png)");

  $("#header_links a").mouseover(function(){
    $(this).css("background", "#" + color);
  });
  $("#header_links a").mouseout(function(){
    $(this).css("background", "none");
  });

  var i = 1;
  $(".killercontentLink").each(function(){

    var a = Math.abs(100 - (i*10));
    $(this).css("background", "url(/images/killer_content_images/" + color + "_" + a + ".png)");
    i++;

  });

  $(".main_item").click(function(){
    var href = this.href;
    if($(this).parent().find("ul").is(":hidden"))
    {
      $("#menu li ul").slideUp();

      var submenulength = $(this).parent().find("ul").find("li").length
      if(submenulength > 0)
      {
        $(this).parent().find("ul").slideDown(function(){
          window.location = href;
        });
      }
      else
      {
        window.location = href;
      }
    }
    else
    {
      window.location = href;
    }
  });

});

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function getSlideShow(id, showAll, home)
{
  if(readCookie('grouptotal') == 'NaN' || !readCookie('grouptotal'))
  {
    createCookie('grouptotal', 2);
  }

  var totalgroups = readCookie('grouptotal');
  
  //console.debug('grouptotal: ' + totalgroups);

  if(readCookie('currentslide') == 'NaN' || !readCookie('currentslide'))
  {
    createCookie('currentslide', 1);
  }

  //console.debug('showall: ' + showAll);
  //console.debug('current_a: ' + readCookie('currentslide'));
  
  if(showAll == 1)
  {
    var id = parseInt(readCookie('currentslide'));
  }
  $("#slideshow").fadeOut(200);
  getSlideShowContent(id, showAll, home);

  var nextId;

  if(showAll == 1 && home == 0)
  {
    if(id >= totalgroups)
    {
      nextId = 1;
    }
    else
    {
      nextId = id + 1;
    }
    createCookie('currentslide', nextId);

    //console.debug('nextid: ' + nextId)

    //console.debug('current slide = ' + $.cookie('currentslide'));

    var timeout = setTimeout('getSlideShow(' +  nextId + ', 1)', 7000);
  }

  $("#portfolio_link_area").mouseover(function(){
    clearTimeout(timeout);
  });

}

function getSlideShowContent(id, showAll, home)
{
  $.get('/ajax/getSlideshow/' + id + '/' + showAll + '/' + home, {}, function(data)
  {
    $("#slideshow").html(data);
    setTimeout('activateSlideShow()', 500);
  });
}

function activateSlideShow()
{
  $("#slideshow").fadeIn();
  
  $("div#slideshow").slideView({toolTip: true});
  var width = $("#stripTransmitter0").find("ul").width();
  var marginLeft = 475 - (width / 2);
  $("#stripTransmitter0").find("ul").css("marginLeft", marginLeft);
  $(".nextbutton").fadeIn();
  $(".prevbutton").fadeIn();
}

