/* Scripts.js - Global
Name: 
URI: http://
Version: 0.1
Author: Studio Lift - kc
Author URI: http://studiolift.com/
*/

/*
 * @collapsible lists
 * hides something, previous sibling 'a' toggles it. simple :)
 */
function collapsible(node){
  $(node).hide();
  
  //we set the category as a class to both the body and the anchor in mt
  //this means that the related list will show if on an article of same cat
  if($(node).prev('a').attr('class') == $('body').attr('class').split(' ').slice(-1)){
    $(node).show();
  }
  
  //use the preceding sibling 'a' as our toggle switch
  $(node).prev('a').click(function(){
    $(node).toggle();
    return false;
  });
}


/* 
 * @cookie time!
 * generic functions from: http://www.quirksmode.org/js/cookies.html
 */
function bakeCookie(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) {
bakeCookie(name,"",-1);
}


/*
 * @custom modal box
 * node is #id of content
 * pos = central OR corner 
 */
function modalBox(node,pos){
//select all the a tag with name equal to modal

//set the defaults
//var node = $(this).attr('href');
  if(undefined===window.pos){
    window.pos = 'central';
  }

  /*
//Get the screen height and width - then create background mask
var maskHeight = $(document).height();
var maskWidth = $(window).width();
$('#mask').css({'width':maskWidth,'height':maskHeight});//Set height and width to mask to fill up the whole screen
$('#mask').fadeIn(1000);//transition effect
$('#mask').fadeTo("slow",0.8);//transition effect
  */

//Get the window height and width
var winH = $(window).height();
var winW = $(window).width();

  if(pos == 'central'){            
  //Set the popup window to center
  $(node).css('top',  winH/2-$(node).height()/2);
  $(node).css('left', winW/2-$(node).width()/2);
  } else if(pos == 'corner'){
    $(node).css('bottom', 5);
  $(node).css('left', 5);
  }

//transition effect
$(node).fadeIn(2000); 

//if close button is clicked
$('.window .close').click(function (e) {
e.preventDefault();//Cancel the link behavior
$(node).hide();

if(readCookie('adset1') == 1){
  bakeCookie('adset1',2,999)
  manageModalOnCookie('adset1')
} else if(readCookie('adset1') == 2){
  bakeCookie('adset1',3,999);
}

});
}

/*
 * 
 */
function manageModalOnCookie(cookieName){
  var cookieState = readCookie(cookieName);
  switch(cookieState){
    case '1':
      modalBox('#hover-ad1','central',999);
      break;
    case '2':
      modalBox('#hover-ad2','corner',14);
      break;
    case '3':
      //do nothing :)
      break;
    default:
      modalBox('#hover-ad1','central');
      bakeCookie(cookieName,1,999);
      break;
  }
}

//-- all done? let's roll!
$('document').ready(function(){

  //- hide left nav sub-items
  $("ul.innerlist").each(function(){
    collapsible(this);
  });
  
  manageModalOnCookie('adset1');

});