var months = [ "January",
	       "February",
	       "March",
	       "April",
	       "May",
	       "June",
	       "July",
	       "August",
	       "September",
	       "October",
	       "November",
	       "December" ];

var wdays = [ "Sun",
	      "Mon",
	      "Tue",
	      "Wed",
	      "Thu",
	      "Fri",
	      "Sat" ];



$(document).ready(function() {

	
	$('input.categories')
		.click(function() {
			if (this.checked ==  true) {
				$("tr.item-" + this.value).show().fadeIn();
			}
			else {
				$("tr.item-" + this.value).fadeOut();
			}
			
			setprefs(this.name, this.checked);
		});

	$('input.summary')
		.click(function() {
			if (this.checked == true) {
				$("span.description").show();
			}
			else {
				$("span.description").hide();
			}
			setprefs(this.name, !this.checked);
		});

	$('select.choosesource')
		.change(function() {
			location.href = Vr.webroot + Vr.name + '/source/' + $('select.choosesource').attr('value');
		});

});

function setprefs(category, checked)
{
	if (checked)
	{
		newPref = new Array();
		for(i=0; i < hide.length; i++)
		{
			if (hide[i] != category)
				newPref.push(hide[i]);
		}
		hide = newPref;
		if (newPref.length>0)
			setCookie("jprefs-"+Vr.name, newPref.join(","), 365, "/");
		else
			deleteCookie("jprefs", "/" + Vr.name);
	}
	else
	{
		hide.push(category);
		setCookie("jprefs-"+Vr.name, hide.join(","), 365, "/");
	}

	//$("#prefs-updated").show(); setTimeout('$("#prefs-updated").hide()', 4000);
	return true;
}

function track(id)
{
	urchinTracker('/outgoing/example_com');
	if(document.images)
	{
		(new Image()).src= Vr.webroot + "track/"+id;
	}

	return true;
}

function qDate(date)
{
	var d = date.getDate() + '';
	if (d.length == 1)
   	{
   		d = "0" + d;
   	}

   	var m = date.getMonth()+1;
   	m = m + '';
	if (m.length == 1)
   	{
   		m = "0" + m;
   	}
   	
   	return date.getFullYear() + "-" + m + "-" + d;
}

function expand(id)
{
	$("span#"+id).toggle();
}



/* Calendar */

// css table box object, can be dragged and closed
function cssWindow(c)
{
  var me = this;
  
  this.topic = c["topic"]?c["topic"]:"";
  this.w = c["width"]?c["width"]:"";
  this.h = c["height"]?c["height"]:"";
  this.x = c["x"]?c["x"]:0;
  this.y = c["y"]?c["y"]:0;
  this.z = c["z"]?c["z"]:0;
  this.c = c["class"]?c["class"]:null;

  this.cb = null;// close callback

  // CREATE NEEDED ELEMENTS
  this.table = document.createElement("table");
  //this.table.border = "1";
  this.table.style.width = this.w;
  this.table.style.height = this.h;
  this.table.cellSpacing = "0";
  this.table.cellPadding = "0";
  this.table.style.left = this.x;
  this.table.style.top = this.y;
  this.table.style.zIndex = this.z;

  this.thead = document.createElement("thead");
  this.tfoot = document.createElement("tfoot");
  this.tbody = document.createElement("tbody");

  this.trhead = document.createElement("tr");
  this.trfoot = document.createElement("tr");
  this.trbody = document.createElement("tr");

  this.tdhead = document.createElement("td");
  this.td1head = document.createElement("td");
  this.td2head = document.createElement("td");

  this.tdfoot = document.createElement("td");
  this.td1foot = document.createElement("td");
  this.td2foot = document.createElement("td");

  this.tdbody = document.createElement("td");
  this.td1body = document.createElement("td");
  this.td2body = document.createElement("td");


  // APPEND THE ELEMENTS
  this.thead = this.table.appendChild(this.thead);
  this.tfoot = this.table.appendChild(this.tfoot);
  this.tbody = this.table.appendChild(this.tbody);

  this.trhead = this.thead.appendChild(this.trhead);
  this.trfoot = this.tfoot.appendChild(this.trfoot);
  this.trbody = this.tbody.appendChild(this.trbody);

  this.tdhead = this.trhead.appendChild(this.tdhead);
  this.td1head = this.trhead.appendChild(this.td1head);
  this.td2head = this.trhead.appendChild(this.td2head);

  this.tdfoot = this.trfoot.appendChild(this.tdfoot);
  this.td1foot = this.trfoot.appendChild(this.td1foot);
  this.td2foot = this.trfoot.appendChild(this.td2foot);

  this.tdbody = this.trbody.appendChild(this.tdbody);
  this.td1body = this.trbody.appendChild(this.td1body);
  this.td1body.style.width = this.w;
  this.td1body.style.height = this.h;
  this.td2body = this.trbody.appendChild(this.td2body);

  // set head
  this.tdhead.appendChild(document.createTextNode(" "));
  this.td2head.appendChild(document.createTextNode(" "));

  // hide callback
  this.doHide = function()
  {
    if(me.tbody.style.display == "none")
    {
      me.table.style.height = me.h;
      me.table.style.width = me.w;
      me.tfoot.style.display = "";
      me.tbody.style.display = "";
    }
    else
    {
      me.table.style.height = "";
      me.table.style.width = me.w;
      me.tbody.style.display = "none";
      me.tfoot.style.display = "none";
    }
  };

  // close callback
  this.doClose = function()
  {
    if(me.cb)
      me.cb();
    me.table.parentNode.removeChild(me.table);
  };

  // initialice drag engine
  if(c["drag"])
  {
    this.drag = new Drag();
    this.drag.init(this.thead,this.table);
  }

  // set element classes
  this.setClass = function(cl)
  {
    me.table.className = cl;
    me.thead.className = cl;
    me.tfoot.className = cl;
    me.tbody.className = cl;
    me.trhead.className = cl+"head";
    me.trfoot.className = cl+"foot";
    me.trbody.className = cl+"body";
    me.tdhead.className = cl+"head";
    me.td1head.className = cl+"head1";
    me.td2head.className = cl+"head2";
    me.tdfoot.className = cl+"foot";
    me.td1foot.className = cl+"foot1";
    me.td2foot.className = cl+"foot2";
    me.tdbody.className = cl+"body";
    me.td1body.className = cl+"body1";
    me.td2body.className = cl+"body2";
  };

  // if class is defined, set the class
  if(this.c) this.setClass(this.c);

  // shorcut for appending valid childs
  this.setDOM = function(obj,ct)
  {
    switch(typeof ct)
    {
      case "number":
      case "string":
        return  obj.appendChild(document.createTextNode(ct));
      case "object":
        return  obj.appendChild(ct);
      default:
        alert("type not supported: "+(typeof ct));
        return null;
    }
  };

  // set a tdbody object as body
  this.setBody = function(ct)
  {
    me.clear(me.td1body);
    return me.setDOM(me.td1body,ct);
  };

  // set a tdfoot object as body
  this.setFoot = function(ct)
  {
    me.clear(me.td1foot);
    return me.setDOM(me.td1foot,ct);
  };

  // set a tdhead object as body
  this.setHead = function(ct)
  {
    me.clear(me.td1head);
    return me.setDOM(me.td1head,ct);
  };

  // clears table special DOM's
  this.clear = function(dom)
  {
    while(dom.firstChild) dom.removeChild(dom.firstChild);
  };

  // shows the box
  this.show = function()
  {
    document.body.appendChild(me.table);
  };

  // returns window
  this.get = function()
  {
    return me.table;
  };
}

// returns the last day of the month
function getLastDay(date)
{
  var start = new Date(date);
  start.setMonth(start.getMonth()+1);
  start.setDate(0);
  return start.getDate();
}

// compare month, date and year from 2 dates
function sameDates(date1, date2)
{
  if(date1.getMonth() == date2.getMonth() &&
     date1.getDate() == date2.getDate() &&
     date1.getFullYear() == date2.getFullYear())
    return true;
  return false;
}

// Calendar Object
function Calendar(fromYear, toYear)
{
  var me = this;
  this.win = null;

  this.from_year = fromYear;
  this.to_year = toYear;

  this.current_date = new Date();
  this.selected_date = new Date();
  this.browse_date = new Date();

  this.select_month = document.createElement("select");
  this.select_month.className = "cmy";
  this.select_year = document.createElement("select");
  this.select_year.className = "cmy";  

  this.tbody = document.createElement("table");
  this.tbody.className = "caltbody";
  this.tbody.cellSpacing = 0;
  this.tbody.cellPadding = 0;
  this.thead = document.createElement("table");
  this.thead.className = "calthead";
  this.thead.cellSpacing = 0;
  this.thead.cellPadding = 0;

  // callback called when day's are clicked,
  // called as function(date)
  this.callback = null;

  this.select_month.onchange = function()
  {
    me.browse_date.setMonth(me.select_month.value);
    me.body();
  };

  this.select_year.onchange = function()
  {
    me.browse_date.setFullYear(me.select_year.value);
    me.body();
  };

  this.newDay = function(d)
  {
    var c = "cd";
    var td = document.createElement("td");
    var date = new Date(me.browse_date);
    date.setDate(d);

    if(sameDates(date, me.selected_date))
      c = "csd";
    else if(sameDates(date, me.current_date))
      c = "ccd";

    td.className = c;
    td.day=d;

    td.onclick = function()
    {
      me.selected_date = new Date(date);
      me.body();
      me.callback(date);
    }
    td.onmouseover = function()
    {
      td.className = "cod";
    }
    td.onmouseout = function()
    {
      td.className = c;
    }
    td.appendChild(document.createTextNode(d));
    return td;
  };

  // sets the selected date, its displayed on different color
  this.setSelectedDate = function(date)
  {
    me.selected_date = new Date(date);
    me.browse_date = new Date(date);
  };

  //Draw head of calendar,  month , year,  week days.
  this.head = function()
  {
    var tbody = document.createElement("tbody");
    var tr = document.createElement("tr");
    var td = document.createElement("td");
    var form = document.createElement("form");
    var option;// = document.createElement("option");

    form.className = "cmy";

    td.className = "cmy";
    td.colSpan = "7";
      
    //draw months
    for( i = 0; i < months.length; i++ )
    {  
      option = document.createElement("option");
      option.value = i;
      option.appendChild(document.createTextNode(months[i]));
      me.select_month.appendChild(option);
    }

    // draw years
    for( i = me.from_year; i <= me.to_year; i++ )
    {
      option = document.createElement("option");
      option.value = i;
      option.appendChild(document.createTextNode(i));
      me.select_year.appendChild(option);
    }
      
    me.select_month = form.appendChild(me.select_month);
    me.select_year = form.appendChild(me.select_year);
    td.appendChild(form);
    tr.appendChild(td);
    tbody.appendChild(tr);
    me.thead.appendChild(tbody);

    me.select_month.selectedIndex = me.browse_date.getMonth();
    me.select_year.value = me.browse_date.getFullYear();
  };

  // Draw days
  this.body = function()
  {
    while(me.tbody.firstChild) me.tbody.removeChild(me.tbody.firstChild);
    var tbody = document.createElement("tbody");
    var tr = document.createElement("tr");
    var td = new Array();
    var th;
    var date = new Date(me.browse_date);
    var day;
    var i;

    date.setDate(1);

    // draw week days
    for(i = 0; i < wdays.length; i++ )
    {
      th = document.createElement("th");
      th.className = "cwd";
      th.appendChild(document.createTextNode(wdays[i]));
      tr.appendChild(th);
    }
    tbody.appendChild(tr);
    tbody = me.tbody.appendChild(tbody);

    // draw initial blank days
    for(i = 0; i < date.getDay(); i++ )
      td[i] = document.createElement("td");

    var last_day = getLastDay(date);
    // draw days
    for(day = 1; day < last_day+1; day++)
      td[i++] = me.newDay(day);

    // draw last blank days
    for(i; i < 42; i++ )
      td[i] = document.createElement("td");

    // insert days in rows
    for(i = 0; i < 6; i++)
    {
      tr = document.createElement("tr");
      for(var j = i*7; j < (i*7)+7; j++)
        tr.appendChild(td[j]);
      tbody.appendChild(tr);
    }
  };

  // returns calendar table element
  this.get = function()
  {
    me.head();
    me.body();

    var conf = new Array();
    conf["topic"] = "JSCalendar";
    conf["class"] = "cal";

    me.win = new cssWindow(conf);

    me.thead = me.win.setHead(me.thead);
    //this.win.setFoot();
    me.tbody = me.win.setBody(me.tbody);

    return me.win.get();
  };
}

//
// jq-corner.js - jQuery method for creating corner effects
//
// If this works, it was written by Dave Methvin (dave.methvin@gmail.com).
// If it's broken, please fix it and send me a working copy.
// Modified by M. Alsup (malsup@gmail.com) to support more styles
// Version 1.01, 10/24/2006
//
jQuery.fn.corner = function(o) {
    function hex2(s) {
        var s = parseInt(s).toString(16);
        return ( s.length < 2 ) ? "0"+s : s;
    }
    function gpc(node) {
        for ( ; node && node.nodeName.toLowerCase() != "html"; node = node.parentNode  ) {
            var v = jQuery.css(node,"backgroundColor");
            if ( v.indexOf("rgb") >= 0 ) { 
                rgb = v.match(/\d+/g); 
                return "#"+ hex2(rgb[0]) + hex2(rgb[1]) + hex2(rgb[2]);
            }
            if ( v && v != "transparent" )
                return v;
        }
        return "#ffffff";
    };
    function getW(i) {
        switch(fx) {
        case "round":  return Math.round(width*(1-Math.cos(Math.asin(i/width))));
        case "cool":   return Math.round(width*(1+Math.cos(Math.asin(i/width))));
        case "sharp":  return Math.round(width*(1-Math.cos(Math.acos(i/width))));
        case "bite":   return Math.round(width*(Math.cos(Math.asin(i/width))));
        case "slide":  return Math.round(width*(Math.atan2(i,width/i)));
        case "jut":    return Math.round(width*(Math.atan2(width,i)));
        case "curl":   return Math.round(width*(Math.atan(i)));
        case "tear":   return Math.round(width*(Math.cos(i)));
        case "wicked": return Math.round(width*(Math.tan(i)));
        case "long":   return Math.round(width*(Math.sqrt(i)));
        case "sculpt": return Math.round(width*(Math.log(i,width)));
        case "dog":    return (i&1) ? (i+1) : width; // Thanks, Dave!
        case "dog2":   return (i&2) ? (i+1) : width; // a bit wider than 'dog'
        case "dog3":   return (i&3) ? (i+1) : width; // a bit wider than 'dog2'
        case "fray":   return (i%2)*width;
        case "notch":  return width; 
        default:       return i+1; // bevel
        }
    };
    o = (o||"").toLowerCase();
    var edges = { T:0, B:1 };
    var width = parseInt((o.match(/(\d+)px/)||[])[1]) || 10;
    var re = /round|bevel|notch|bite|cool|sharp|slide|jut|curl|tear|fray|wicked|sculpt|long|dog3|dog2|dog/;
    var fx = ((o.match(re)||["round"])[0]);
    var opts = {
        TL:     /top|tl/.test(o),       TR:     /top|tr/.test(o),
        BL:     /bottom|bl/.test(o),    BR:     /bottom|br/.test(o)
    };
    if ( !opts.TL && !opts.TR && !opts.BL && !opts.BR )
        opts = { TL:1, TR:1, BL:1, BR:1 };
    opts.swap = /bite|jut|sculpt/.test(fx); // flag to reorder element insertion
    var strip = document.createElement("div");
    strip.style.overflow = "hidden";
    strip.style.height = "1px";
    strip.style.backgroundColor = "transparent";
    strip.style.borderStyle = "solid";
    return this.each(function(){
        var pad = {
            T: parseInt(jQuery.css(this,"paddingTop"))||0,     R: parseInt(jQuery.css(this,"paddingRight"))||0,
            B: parseInt(jQuery.css(this,"paddingBottom"))||0,  L: parseInt(jQuery.css(this,"paddingLeft"))||0
        };
        strip.style.borderColor = gpc(this.parentNode);
        for ( var j in edges) {
            var bot = edges[j];
            strip.style.borderStyle = "none "+(opts[j+'R']?"solid":"none")+" none "+(opts[j+'L']?"solid":"none");
            var d=document.createElement("div");
            d.style.margin = !bot ? "-"+pad.T+"px -"+pad.R+"px "+(pad.T-width)+"px -"+pad.L+"px" : 
                    (pad.B-width)+"px -"+pad.R+"px -"+pad.B+"px -"+pad.L+"px";
            d.style.backgroundColor = "transparent";
            var append = (bot && !opts.swap) || (!bot && opts.swap);
            for ( var i=0; i < width; i++ ) {
                var w = Math.max(0,getW(i));
                var e = strip.cloneNode(false);
                e.style.borderWidth = "0 "+(opts[j+'R']?w:0)+"px 0 "+(opts[j+'L']?w:0)+"px";;
                append ? d.appendChild(e) : d.insertBefore(e, d.firstChild);
            }
            bot ? this.appendChild(d) : this.insertBefore(d, this.firstChild);
        }
    });
};


/*

DISCLAIMER: THESE JAVASCRIPT FUNCTIONS ARE SUPPLIED 'AS IS', WITH 
NO WARRANTY EXPRESSED OR IMPLIED. YOU USE THEM AT YOUR OWN RISK. 
NEITHER PAUL STEPHENS NOR PC PLUS MAGAZINE ACCEPTS ANY LIABILITY FOR 
ANY LOSS OR DAMAGE RESULTING FROM THEIR USE, HOWEVER CAUSED. 

Paul Stephens' NetScape-based cookie-handling library

http://web.ukonline.co.uk/paul.stephens/index.htm

TO USE THIS LIBRARY, INSERT ITS CONTENTS IN A <script></script> BLOCK IN THE 
<HEAD> SECTION OF YOUR WEB PAGE SOURCE, BEFORE ANY OTHER JAVASCRIPT ROUTINES.

Feel free to use this code, but please leave this comment block in.

*/



function setCookie (name, value, lifespan, access_path) {
      
  var cookietext = name + "=" + escape(value)  
    if (lifespan != null) {  
      var today=new Date()     
      var expiredate = new Date()      
      expiredate.setTime(today.getTime() + 1000*60*60*24*lifespan)
      cookietext += "; expires=" + expiredate.toGMTString()
    }
    if (access_path != null) { 
      cookietext += "; PATH="+access_path 
    }
   document.cookie = cookietext 
   return null  
}


function setDatedCookie(name, value, expire, access_path) {
    var cookietext = name + "=" + escape(value)
      + ((expire == null) ? "" : ("; expires=" + expire.toGMTString()))
     if (access_path != null) { 
      cookietext += "; PATH="+access_path 
     }
   document.cookie = cookietext 
   return null        
}


function getCookie(Name) {
  var search = Name + "="                       
  var CookieString = document.cookie            
  var result = null                               
  if (CookieString.length > 0) {                
    offset = CookieString.indexOf(search)       
    if (offset != -1) {                         
      offset += search.length                   
      end = CookieString.indexOf(";", offset)   
      if (end == -1)                            
        end = CookieString.length               
      result = unescape(CookieString.substring(offset, end))         
                                                
      } 
    }
   return result                                
}


function deleteCookie(Name, Path) {
  setCookie(Name,"Deleted", -1, Path)
}
