if (typeof(jQuery) != "undefined") {
   jQuery.fn.initLoc = function(o) {
      jQuery.loc = jQuery.extend({}, jQuery.loc, o);
      
      for (var k in o) {
         this.append('<a href="#" onclick="$.setLoc(\''+k+'\'); return false;">'+
                     '<img src="locales/'+k+'/icon.png" alt="'+o[k]+'" width="21" height="16" /></a>');
      }
      
      // if ((jQuery.getCurrLoc() == null) || (jQuery.getCurrLoc == "")) {
      //    jQuery.setLoc(jQuery.currLoc);
      // }
   }
   
   jQuery.extend({
      loc: {},
      currLoc: null,
      setLoc: function(l) {
         jQuery.currLoc = l;
   	   var pos = document.location.href.indexOf("#");
   	   var url = (pos >= 0) ? document.location.href.substr(pos+1) : "";
   	   var urlParts = url.split("/");
   	   var dest = "";
	      
   	   if (urlParts[0] == "")
   	      urlParts.shift();
	      
   	   if (jQuery.loc[urlParts[0]] != null) {
   	      urlParts.shift();
   	   }
	      
   	   window.location.href = unescape("#/"+l+"/"+urlParts.join("/"));
   	},
   	getCurrLoc: function() {
	      var pos = document.location.href.indexOf("#");
   	   var url = (pos >= 0) ? document.location.href.substr(pos+1) : "";
   	   var urlParts = url.split("/");

   	   if (urlParts[0] == "")
   	      urlParts.shift();
         
   	   if (jQuery.loc[urlParts[0]] != null) {
   	      return urlParts.shift();
   	   }
         
   	   return null;
   	}
   });
}

function changeBg(style) {
   $("#body").css("background", style);
}

function reloadWindow() {
   window.location.href = unescape(window.location.pathname);
}

function launchPopupWindow(url, width, height, x, y) {
   if (x == null) {
      x = 0;
   }
   
   if (y == null) {
      y = 0;
   }
   
   var w = window.open(url, 'window', 'height='+height+',width='+width+'resizable=1,menubar=0,location=0,toolbar=0,status=0');
   w.moveTo(x, y)
}

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

function getCookie(name) {
	var nq = 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(nq) == 0) {
	      return c.substring(nq.length, c.length);
	   }
	}
	return null;
}

function clearCookie(name) {
   setCookie(name,"",-1)
}

