﻿function URLEncode (clearString) {
  var output = '';
  var x = 0;
  clearString = clearString.toString();
  var regex = /(^[a-zA-Z0-9_.]*)/;
  while (x < clearString.length) {
    var match = regex.exec(clearString.substr(x));
    if (match != null && match.length > 1 && match[1] != '') {
     output += match[1];
      x += match[1].length;
    } else {
      if (clearString[x] == ' ')
        output += '+';
      else {
        var charCode = clearString.charCodeAt(x);
        var hexVal = charCode.toString(16);
        output += '%' + ( hexVal.length < 2 ? '0' : '' ) + hexVal.toUpperCase();
      }
      x++;
    }
  }
  return output;
}

osjs(function($){

 //Open popups if specified in URL
 var url = document.location.href;
 if (url.indexOf('#')!=-1) {
  var popup_url = url.replace(/.*#/,'');
  popup_url = popup_url.replace(/.*\//,'');
  popup_url = popup_url.replace(/&_=.*/,'');
  if (popup_url!='_')
   setTimeout(function() {osjs('a[href$=' + popup_url + ']').click();},1000);
 }
 
    //when popup open add url # 
    osjs('script').each(function(){
 var text = ($.browser.msie)? osjs(this).html():osjs(this).text();
 if (/RichWidgets_Popup_Editor_init\('([^']*)/.test(text)) {
  var link_id= /RichWidgets_Popup_Editor_init\('([^']*)/.exec(text)[1];
  $('#' + link_id).click(function() {
   document.location.href='#' + $('#' + link_id).attr('href');
   if(typeof(pageTracker) != 'undefined')
    pageTracker._trackPageview($('#' + link_id).attr('href')); 
       setTimeout(function(){osjs('.ui-dialog-content').bind('dialogclose',function() { document.location.href='#_';});},1000)
  });
 }
 })

 $(".popup_link").each(function() {
   if (!osjs(this).attr('id')) 
     osjs(this).attr('id', Math.random().toString().replace('.',''));
    RichWidgets_Popup_Editor_init(osjs(this).attr('id'),'',' ',-1,-1,osjs(this).attr('href'));
  });
  
 $('.popup_link').click(function() {
   var link_id = $(this).attr('id');
   document.location.href='#' + $('#' + link_id).attr('href');
   if(typeof(pageTracker) != 'undefined')
    pageTracker._trackPageview($('#' + link_id).attr('href')); 
       setTimeout(function(){osjs('.ui-dialog-content').bind('dialogclose',function() { document.location.href='#_';});},1000)
  }); 

 //Automatically finds IFRAMEs on the page and resizes them based on their content.
var iFrames=document.getElementsByTagName("iframe");function iResize(){for(var i=0,j=iFrames.length;i<j;i++){try{iFrames[i].style.height=iFrames[i].contentWindow.document.body.offsetHeight+"px"}catch(e){}}}if($.browser.safari||$.browser.opera){$("iframe").load(function(){setTimeout(iResize,0)});for(var i=0,j=iFrames.length;i<j;i++){var iSource=iFrames[i].src;iFrames[i].src="";iFrames[i].src=iSource}}else{$("iframe").load(function(){try{this.style.height=this.contentWindow.document.body.offsetHeight+"px"}catch(e){}})};
});