/* @minify true */

// GENERIC COOKIE SCRIPT (Next 3 functions)
function getCookieVal(offset) {
	var endstr=document.cookie.indexOf(";",offset);

	if (endstr==-1) {
		endstr=document.cookie.length;
	}
	return unescape(document.cookie.substring(offset,endstr));
}

function GetCookie(cname) {
	var arg=cname+"=";
	var alen=arg.length;
	var clen=document.cookie.length;
	var i=0;

	while (i<clen) {
		var j=i+alen;
		if (document.cookie.substring(i,j)==arg) { return getCookieVal (j); }
		i=document.cookie.indexOf(" ",i)+ 1;
		if (i===0) { break; }
	} return null;
}

function SaveCookie(cname,cvalue,cdays,cpath) {
	var ex = new Date();
	ex.setTime(ex.getTime() + (cdays*86400000));

	if (cpath === null) { cpath = "/"; }
	cpath = "; path=" + cpath;
	if (cdays === null) { cdays = ""; } else { cdays = "; expires=" + ex.toGMTString(); }
	document.cookie = cname + '=' + cvalue + cdays + cpath + ";domain=" + location.host.replace(/.*\.(.*\.com)/,"$1") + ";";
}

function readCookie(name) {
	var cookieValue = "";
	var search = name + "=";
	if(document.cookie.length > 0) { 
		var offset = document.cookie.indexOf(search);
		if (offset != -1) { 
			offset += search.length;
			var end = document.cookie.indexOf(";", offset);
			if (end == -1) { end = document.cookie.length; }
			cookieValue = unescape(document.cookie.substring(offset, end));
		}
	}
	return cookieValue;
}

function parseCookie(cookie, key) {
	var valArray = cookie.split("|");
  var i, l = valArray.length;
	for (i = 0; i < l; i++) {
		if (valArray[i].match(key)) {var value = valArray[i].split("="); return value[1];}
	}
	return null;
}


// UPDATE COOKIES
var insitecookie="tacoma_user_auth";
var threshold=0;
var daystokeep=1;

if (GetCookie(insitecookie) && !GetCookie(insitecookie).match("threshold")) {
    if (!GetCookie('seentoday')) {
			document.write('<img width="1" height="1" border="0" ');
			document.write("src='http://www.thenewstribune.com/static/secure/log.gif?pg="+escape(document.location)+"'>");
			SaveCookie('seentoday',1,0.9,'/');
    }
}

function tntLongDate() {
	var months = ['January','February','March','April','May','June','July','August','September','October','November','December'];
	var days = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
	var today = new Date();
	var wday   = days[today.getDay()];
	var lmonth = months[today.getMonth()];
	var date = today.getDate();
	var fyear = today.getFullYear();
	
	return (wday +", " + lmonth + " " + date + ", " + fyear);
}




// ### Local Storage functions - added 04.22.11 Ian Swenson

// Array Remove - By John Resig (MIT Licensed)
Array.prototype.remove = function( from, to )
{
  var rest = this.slice((to || from) + 1 || this.length);
  this.length = from < 0 ? this.length + from : from;
  return this.push.apply(this, rest);
};

function supports_html5_storage()
{
  try { return 'localStorage' in window && window.localStorage !== null; }
  catch (e) { return false; }
}

function get_storage_array( name )
{
  var storage = localStorage.getItem(name);
  if ( storage !== null ) { storage = storage.split('|||'); }
  return storage;
}

function set_storage_array( name, array )
{
  if ( array.length === 0 ) {
    delete localStorage[name];
  } else {
    localStorage.setItem(name, array.join('|||'));
  }
}


// jQuery: Mixed bag of function
$(document).ready(function() {

	/**
	 * Controls the Local communities dropdown
	 */
  $(".local_dropdown").change(function(){
    window.location.href = $(this).children("option:selected").val();
  });



  /**
   * Story Tools button - larger/smaller font size
   */
  var hasLocalStorage = supports_html5_storage();
  var storageName = "tntstory_fontsize";
  var body = $('#storyBody');
  var maxSize = 18, minSize = 9;

  if ( $(body).length !== 0 ) {

    function alterFontSize( size )
    {
      $(body).css( 'font-size', size + 'px' );

      if ( hasLocalStorage ) {
        localStorage.setItem(storageName, size);
      }
    }

    function parseFontSize( size )
    {
      var cut, l = size.length;
      var unit = size[l - 1];

      if ( unit != '%' ) { cut = 2; }
      else { cut = 3; }

      return size.slice( 0, -cut );
    }

    function biggerFontSize( size )
    {
      size = parseFontSize( size );
      size++;
      if ( size > maxSize ) { size = maxSize; }
      alterFontSize( size );
    }

    function smallerFontSize( size )
    {
      size = parseFontSize( size );
      size--;
      if ( size < minSize ) { size = minSize; }
      alterFontSize( size );
    }
    
    if ( hasLocalStorage ) {
      var storage = localStorage.getItem(storageName);
      if ( storage !== null ) {
        alterFontSize(storage);
      }
    }

    $('.bigger_font').click(function() {
      biggerFontSize( $(body).css('font-size') );
      return false;
    });

    $('.smaller_font').click(function() {
      smallerFontSize( $(body).css('font-size') );
      return false;
    });

  }

});
