$(document).ready(function(){
	OA_getads($(".hswiAd").map(function(){
			return { e:$(this).attr("id"), z:$(this).attr("zoneid"), u:$(this).attr("proxy"), s:$(this).attr("source") };
		}).get());
	
	$(window).load(function(){
		adLoadComplete = true;
		$("div.hswiAd").trigger('load');
	});
});

function OA_getad(e,z,u,s){OA_getads([{'u':u, 'z':z,'s':s, 'e':e}]);}

function OA_getads(d){
	if (typeof(d) == 'object' && d.length > 0){
		//right now we are assuming that all the 
		//items are going to the same adproxy
		dyncontent = new Array();
		$.post(d[0].u, $.toJSON(d), function(ads){
			if (typeof(ads) == 'object') {
				for (i = 0; i < ads.length; i++){
					// we have ads 
					var j = ads[i];
					if ($('#' + j.e).length > 0){
						// and we have a place to put the ad 
						var ad = $('#' + j.e);
						if(typeof(j.html) != 'undefined' && j.html.length > 0) {
							// get a command object
							var oac = parseCmds(j.html);
							if (oac.zone.get("positionsize")) { // see if we have a positionsize comment 
								// get our width and height from j.html 
								var pw = Number(oac.zone.getValue("positionsize")['x']);
								var ph = Number(oac.zone.getValue("positionsize")['y']);

								// set initial sizes 
								ad.width(pw)
								  .height(ph)
								  .parent()
								  	.width(pw)
								  	.height(ph); // set the parent div's size 
								
								// the banner height may be different 
								var bh = Number(j.height);
								var bw = Number(j.width);
								
								// if they are not equal, we need to setup the clip areas 
								
								if ((pw != bw) || (ph != bh)) {
									ad.attr("bw", bw)
									  .attr("bh", bh)
									  .attr("pw", pw)
									  .attr("ph", ph)
									  .mouseover(function(){
											$(this).css({'clip': 'rect(0px ' + $(this).attr('bw') + 'px ' + $(this).attr('bh') + 'px 0px)'});
											var off = $(this).offset();
											var offset = {'left':off.left, 
														  'top':off.top, 
														  'right':off.left + parseInt($(this).attr('bw')), 
														  'bottom': off.top + parseInt($(this).attr('bh'))};
											var adId = '#' + $(this).attr('id');
											
											$('#doc').bind('mousemove', function(e){
												if (e.clientX < offset.left || e.clientX > offset.right || e.clientY < offset.top || e.clientY > offset.bottom) {
													$(adId).trigger('mouseout');
													$(this).unbind('mousemove');
												}
											});
											
									  })
									  .mouseout(function(){
											$(this).css({'clip': 'rect(0px ' + $(this).attr('pw') + 'px ' + $(this).attr('ph') + 'px 0px)'});
									  })
									  .parent()
									  	.css("z-index", "101");
								}
										
							}else{ // otherwise just use the ad size 
								ad.width(Number(j.width))
								  .height(Number(j.height))
								  .parent()
								  	.width(Number(j.width))
								  	.height(Number(j.height)); // set the parent div's size 
							}
							
							// should we hide the ad. 
							if (oac.zone.get("type") == 'float' 
									|| oac.ad.get("trans")
									|| oac.zone.get("trans"))
								ad.hide();
								
							// do we need a close button 
							if(oac.zone.get("type") == 'float')
								ad.append('<div class="closefloat" style="width:' + j.width + 'px;">FECHAR</div>')
								  .parent()
									.removeClass("hswiAdContainer")
									.addClass("hswiFloatingAdContainer");
											
							// place the ad 
							if (j.contenttype.length == 0)	{ //must be HTML
								ad.append('<iframe id="frame-' + ad.attr("id") + '" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="" height="' + j.height + '" width="' + j.width + '"></iframe>');
								adframe = frames[frames.length - 1];
								adframe.document.write('<html><head></head><body style="margin:0px;padding:0px;"></body>');
								adframe.document.write(j.html);
								adframe.document.write('</body></html>');
								//adframe.document.close();
							}else{
								ad.append(j.html);	
							}
								  
							// if the ad isn't visible, we need to do something to make it that way
							if (ad.is(":hidden")){
								// if we have  show time we should automatically trigget the hide action
								var ah = (oac.ad.getValue("show"));
								
								// calculate how long to show the ad 
								var s = ((oac.ad.getValue("show")) ? oac.ad.getValue("show") * 1000 : 10000);
								
								// find possible transition
								var t = ((oac.ad.get("trans")) ? oac.ad.get("trans") : oac.zone.get("trans"));
								switch(t){
									case 'slide':
										// assign the right display for the close function 
										ad.children(".closefloat").click(function(){$(this).parent().parent().slideUp();});
										
										// create the display for the close function 
										tf = function(){if ($(this).is(":hidden")) $(this).slideDown("def", function(){if($(this).is(":visible")) setTimeout("$('#" + $(this).attr('id') + "').parent().slideUp()", s)});};
										break;
									case 'fade':
									default:
										// assign the right display for the close function 
										ad.children(".closefloat").click(function(){$(this).parent().parent().fadeOut();});
										
										// create the display for the close function 
										tf = function(){if ($(this).is(":hidden"))$(this).fadeIn("def", function(){ if($(this).is(":visible")) setTimeout("$('#" + $(this).attr('id') + "').parent().fadeOut()", s)});};
										break;	
								}
								
								/* need to make sure the load event for the window hasn't already fired.
							   		if it has, then go ahead a trigger the display */
								(typeof(adLoadComplete) != 'undefined') ? tf.call(ad) : ad.load(tf);
							}
									
						}
					}
				}
			}
		}, "json");
	}
}

/* OpenX Functions */
function parseCmds(txtToParse){
	var cmds = {zone:new openXCmd, ad:new openXCmd};
	re = /!--\s*(ZONE|AD):([^\-]*)\s*-->/g;
	if (txtToParse.match(re))
		$.each(txtToParse.match(re), function(){
			if (this.match(re)){
			//this is needed to work in ie6
			if (typeof(re) != 'function')
				re = /!--\s*(ZONE|AD):([^\-]*)\s*-->/g;
				var res = re.exec(this);
				cmdtype = $.trim(res[1].toLowerCase());
				// Only read a zone command once 
				if (cmdtype != 'zone' || !cmds.zone.locked){
					$.each(res[2].toLowerCase().split(":"), function(){
						// seperate the values and commands 
						var v = /([^ \(]*)(\(?.*\)?)/.exec($.trim(this));
						v[2] = $.trim(v[2].replace(/^\((.*)\)$/, "$1"));
						
						if (v[2].match(/([0-9]+)x([0-9])/i)){ // check if any of the values are xy coordinates 
							v[2] = {x:v[2].split(/x/i)[0], y:v[2].split(/x/i)[1]}
						}else if(v[2].match(/([^ \(]+)(\(.*\))/)){ // see if the have sub parameters 
							var subi = v[2].match(/([^ \(]+)(\(.*\))/)
							v[2] = {};
							v[2][subi[1]]=subi[2].replace(/^\((.*)\)$/, "$1");
						}
						
						if (typeof(v[1]) != "undefined" && v[1].length > 0) cmds[cmdtype][v[1]] = v[2];
					});
					
					if (cmdtype.toLowerCase() == 'zone') cmds.zone.locked = true;
				}// end zone dup check 
			}
		});
	
	return cmds;
}

function openXCmd(){
	// get the name of the value 
	this.get = function(r){
		switch(typeof(this[r])){
			case 'string':
				return this[r];
				break;
			case 'object':
				for (var i in this[r])
					return i;
				break;
			default:
				return null;	
		}
	}
	
	// get the actual value 
	this.getValue = function(r){return ((typeof(this[r]) != 'undefined') ? this[r] : null);}
}
/* jQuery JSON plugin */
(function ($) {
    var m = {
            '\b': '\\b',
            '\t': '\\t',
            '\n': '\\n',
            '\f': '\\f',
            '\r': '\\r',
            '"' : '\\"',
            '\\': '\\\\'
        },
        s = {
            'array': function (x) {
                var a = ['['], b, f, i, l = x.length, v;
                for (i = 0; i < l; i += 1) {
                    v = x[i];
                    f = s[typeof v];
                    if (f) {
                        v = f(v);
                        if (typeof v == 'string') {
                            if (b) {
                                a[a.length] = ',';
                            }
                            a[a.length] = v;
                            b = true;
                        }
                    }
                }
                a[a.length] = ']';
                return a.join('');
            },
            'boolean': function (x) {
                return String(x);
            },
            'null': function (x) {
                return "null";
            },
            'number': function (x) {
                return isFinite(x) ? String(x) : 'null';
            },
            'object': function (x) {
                if (x) {
                    if (x instanceof Array) {
                        return s.array(x);
                    }
                    var a = ['{'], b, f, i, v;
                    for (i in x) {
                        v = x[i];
                        f = s[typeof v];
                        if (f) {
                            v = f(v);
                            if (typeof v == 'string') {
                                if (b) {
                                    a[a.length] = ',';
                                }
                                a.push(s.string(i), ':', v);
                                b = true;
                            }
                        }
                    }
                    a[a.length] = '}';
                    return a.join('');
                }
                return 'null';
            },
            'string': function (x) {
                if (/["\\\x00-\x1f]/.test(x)) {
                    x = x.replace(/([\x00-\x1f\\"])/g, function(a, b) {
                        var c = m[b];
                        if (c) {
                            return c;
                        }
                        c = b.charCodeAt();
                        return '\\u00' +
                            Math.floor(c / 16).toString(16) +
                            (c % 16).toString(16);
                    });
                }
                return '"' + x + '"';
            }
        };

	$.toJSON = function(v) {
		var f = isNaN(v) ? s[typeof v] : s['number'];
		if (f) return f(v);
	};
	
	$.parseJSON = function(v, safe) {
		if (safe === undefined) safe = $.parseJSON.safe;
		if (safe && !/^("(\\.|[^"\\\n\r])*?"|[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t])+?$/.test(v))
			return undefined;
		return eval('('+v+')');
	};
	
	$.parseJSON.safe = false;

})(jQuery);

jQuery.fn.dump = function(showTypes, showAttributes) {
	jQuery.dump($(this), showTypes, showAttributes);
	return this;
};

jQuery.dump = function(object, showTypes, showAttributes) {
  var dump = '';
  var st = typeof showTypes == 'undefined' ? true : showTypes;
  var sa = typeof showAttributes == 'undefined' ? true : showAttributes;  
  var winName = 'dumpWin';
  var w = 760;
  var h = 500;
  var leftPos = screen.width ? (screen.width - w) / 2 : 0;
  var topPos = screen.height ? (screen.height - h) / 2 : 0;
  var settings = 'height=' + h + ',width=' + w + ',top=' + topPos + ',left=' + leftPos + ',scrollbars=yes,menubar=yes,status=yes,resizable=yes';
  var title = 'Dump';
  var script = 'function tRow(s) {t = s.parentNode.lastChild;tTarget(t, tSource(s)) ;}function tTable(s) {var switchToState = tSource(s) ;var table = s.parentNode.parentNode;for (var i = 1; i < table.childNodes.length; i++) {t = table.childNodes[i] ;if (t.style) {tTarget(t, switchToState);}}}function tSource(s) {if (s.style.fontStyle == "italic" || s.style.fontStyle == null) {s.style.fontStyle = "normal";s.title = "click to collapse";return "open";} else {s.style.fontStyle = "italic";s.title = "click to expand";return "closed" ;}}function tTarget (t, switchToState) {if (switchToState == "open") {t.style.display = "";} else {t.style.display = "none";}}';	
  
 var _recurse = function (o, type) {
    var i;
	var j = 0;
	var r = '';
	type = _dumpType(o);
	switch (type) {		
	  case 'regexp':
	    var t = type;
	    r += '<table' + _dumpStyles(t,'table') + '><tr><th colspan="2"' + _dumpStyles(t,'th') + '>' + t + '</th></tr>';
	    r += '<tr><td colspan="2"' + _dumpStyles(t,'td-value') + '><table' + _dumpStyles('arguments','table') + '><tr><td' + _dumpStyles('arguments','td-key') + '><i>RegExp: </i></td><td' + _dumpStyles(type,'td-value') + '>' + o + '</td></tr></table>';  
	    j++;
	    break;
	  case 'date':
	    var t = type;
	    r += '<table' + _dumpStyles(t,'table') + '><tr><th colspan="2"' + _dumpStyles(t,'th') + '>' + t + '</th></tr>';
	    r += '<tr><td colspan="2"' + _dumpStyles(t,'td-value') + '><table' + _dumpStyles('arguments','table') + '><tr><td' + _dumpStyles('arguments','td-key') + '><i>Date: </i></td><td' + _dumpStyles(type,'td-value') + '>' + o + '</td></tr></table>';  
	    j++;
	    break;
	  case 'function':
	    var t = type;
	    var a = o.toString().match(/^.*function.*?\((.*?)\)/im); 
	    var args = (a == null || typeof a[1] == 'undefined' || a[1] == '') ? 'none' : a[1];
	    r += '<table' + _dumpStyles(t,'table') + '><tr><th colspan="2"' + _dumpStyles(t,'th') + '>' + t + '</th></tr>';
	    r += '<tr><td colspan="2"' + _dumpStyles(t,'td-value') + '><table' + _dumpStyles('arguments','table') + '><tr><td' + _dumpStyles('arguments','td-key') + '><i>Arguments: </i></td><td' + _dumpStyles(type,'td-value') + '>' + args + '</td></tr><tr><td' + _dumpStyles('arguments','td-key') + '><i>Function: </i></td><td' + _dumpStyles(type,'td-value') + '>' + o + '</td></tr></table>';  
	    j++;
	    break;
	  case 'domelement':
	    var t = type;
		var attr = '';
		if (sa) {
		  for (i in o) {if (!/innerHTML|outerHTML/i.test(i)) {attr += i + ': ' + o[i] + '<br />';}}
		}
	    r += '<table' + _dumpStyles(t,'table') + '><tr><th colspan="2"' + _dumpStyles(t,'th') + '>' + t + '</th></tr>';
	    r += '<tr><td' + _dumpStyles(t,'td-key') + '><i>Node Name: </i></td><td' + _dumpStyles(type,'td-value') + '>' + o.nodeName.toLowerCase() + '</td></tr>';  
		r += '<tr><td' + _dumpStyles(t,'td-key') + '><i>Node Type: </i></td><td' + _dumpStyles(type,'td-value') + '>' + o.nodeType + '</td></tr>'; 
		r += '<tr><td' + _dumpStyles(t,'td-key') + '><i>Node Value: </i></td><td' + _dumpStyles(type,'td-value') + '>' + o.nodeValue + '</td></tr>';
		if (sa) {
		  r += '<tr><td' + _dumpStyles(t,'td-key') + '><i>Attributes: </i></td><td' + _dumpStyles(type,'td-value') + '>' + attr + '</td></tr>';  		
		  r += '<tr><td' + _dumpStyles(t,'td-key') + '><i>innerHTML: </i></td><td' + _dumpStyles(type,'td-value') + '>' + o.innerHTML + '</td></tr>'; 
		  if (typeof o.outerHTML != 'undefined') {
		    r += '<tr><td' + _dumpStyles(t,'td-key') + '><i>outerHTML: </i></td><td' + _dumpStyles(type,'td-value') + '>' + o.outerHTML + '</td></tr>'; 
		  }
		}
	    j++;
	    break;		
	}
	if (/object|array/.test(type)) {
      for (i in o) {
	    var t = _dumpType(o[i]);
	    if (j < 1) {
	      r += '<table' + _dumpStyles(type,'table') + '><tr><th colspan="2"' + _dumpStyles(type,'th') + '>' + type + '</th></tr>';
		  j++;	  
	    }
	    if (typeof o[i] == 'object' && o[i] != null) { 
		  r += '<tr><td' + _dumpStyles(type,'td-key') + '>' + i + (st ? ' [' + t + ']' : '') + '</td><td' + _dumpStyles(type,'td-value') + '>' + _recurse(o[i], t) + '</td></tr>';	
	    } else if (typeof o[i] == 'function') {
		  r += '<tr><td' + _dumpStyles(type ,'td-key') + '>' + i + (st ? ' [' + t + ']' : '') + '</td><td' + _dumpStyles(type,'td-value') + '>' + _recurse(o[i], t) + '</td></tr>';  	
		} else {
		  r += '<tr><td' + _dumpStyles(type,'td-key') + '>' + i + (st ? ' [' + t + ']' : '') + '</td><td' + _dumpStyles(type,'td-value') + '>' + o[i] + '</td></tr>';  
	    }
	  }
	}
	if (j == 0) {
	  r += '<table' + _dumpStyles(type,'table') + '><tr><th colspan="2"' + _dumpStyles(type,'th') + '>' + type + ' [empty]</th></tr>'; 	
	}
	r += '</table>';
	return r;
  };	
  var _dumpStyles = function(type, use) {
  var r = '';
  var table = 'font-size:xx-small;font-family:verdana,arial,helvetica,sans-serif;cell-spacing:2px;';
  var th = 'font-size:xx-small;font-family:verdana,arial,helvetica,sans-serif;text-align:left;color: white;padding: 5px;vertical-align :top;cursor:hand;cursor:pointer;';
  var td = 'font-size:xx-small;font-family:verdana,arial,helvetica,sans-serif;vertical-align:top;padding:3px;';
  var thScript = 'onClick="tTable(this);" title="click to collapse"';
  var tdScript = 'onClick="tRow(this);" title="click to collapse"';
  switch (type) {
	case 'string':
	case 'number':
	case 'boolean':
	case 'undefined':
	case 'object':
	  switch (use) {
		case 'table':  
		  r = ' style="' + table + 'background-color:#0000cc;"';
		  break;
		case 'th':
		  r = ' style="' + th + 'background-color:#4444cc;"' + thScript;
		  break;
		case 'td-key':
		  r = ' style="' + td + 'background-color:#ccddff;cursor:hand;cursor:pointer;"' + tdScript;
		  break;
		case 'td-value':
		  r = ' style="' + td + 'background-color:#fff;"';
		  break;
	  }
	  break;
	case 'array':
	  switch (use) {
		case 'table':  
		  r = ' style="' + table + 'background-color:#006600;"';
		  break;
		case 'th':
		  r = ' style="' + th + 'background-color:#009900;"' + thScript;
		  break;
		case 'td-key':
		  r = ' style="' + td + 'background-color:#ccffcc;cursor:hand;cursor:pointer;"' + tdScript;
		  break;
		case 'td-value':
		  r = ' style="' + td + 'background-color:#fff;"';
		  break;
	  }	
	  break;
	case 'function':
	  switch (use) {
		case 'table':  
		  r = ' style="' + table + 'background-color:#aa4400;"';
		  break;
		case 'th':
		  r = ' style="' + th + 'background-color:#cc6600;"' + thScript;
		  break;
		case 'td-key':
		  r = ' style="' + td + 'background-color:#fff;cursor:hand;cursor:pointer;"' + tdScript;
		  break;
		case 'td-value':
		  r = ' style="' + td + 'background-color:#fff;"';
		  break;
	  }	
	  break;
	case 'arguments':
	  switch (use) {
		case 'table':  
		  r = ' style="' + table + 'background-color:#dddddd;cell-spacing:3;"';
		  break;
		case 'td-key':
		  r = ' style="' + th + 'background-color:#eeeeee;color:#000000;cursor:hand;cursor:pointer;"' + tdScript;
		  break;	  
	  }	
	  break;
	case 'regexp':
	  switch (use) {
		case 'table':  
		  r = ' style="' + table + 'background-color:#CC0000;cell-spacing:3;"';
		  break;
		case 'th':
		  r = ' style="' + th + 'background-color:#FF0000;"' + thScript;
		  break;
		case 'td-key':
		  r = ' style="' + th + 'background-color:#FF5757;color:#000000;cursor:hand;cursor:pointer;"' + tdScript;
		  break;
		case 'td-value':
		  r = ' style="' + td + 'background-color:#fff;"';
		  break;		  
	  }	
	  break;
	case 'date':
	  switch (use) {
		case 'table':  
		  r = ' style="' + table + 'background-color:#663399;cell-spacing:3;"';
		  break;
		case 'th':
		  r = ' style="' + th + 'background-color:#9966CC;"' + thScript;
		  break;
		case 'td-key':
		  r = ' style="' + th + 'background-color:#B266FF;color:#000000;cursor:hand;cursor:pointer;"' + tdScript;
		  break;
		case 'td-value':
		  r = ' style="' + td + 'background-color:#fff;"';
		  break;		  
	  }	
	  break;
	case 'domelement':
	case 'document':
	case 'window':
	  switch (use) {
		case 'table':  
		  r = ' style="' + table + 'background-color:#FFCC33;cell-spacing:3;"';
		  break;
		case 'th':
		  r = ' style="' + th + 'background-color:#FFD966;"' + thScript;
		  break;
		case 'td-key':
		  r = ' style="' + th + 'background-color:#FFF2CC;color:#000000;cursor:hand;cursor:pointer;"' + tdScript;
		  break;
		case 'td-value':
		  r = ' style="' + td + 'background-color:#fff;"';
		  break;		  
	  }	
	  break;	  
  }
  return r;
  };
  var _dumpType = function (obj) {
    var t = typeof(obj);
    if (t == 'function') {
      var f = obj.toString();
      if ( ( /^\/.*\/[gi]??[gi]??$/ ).test(f)) {
        return 'regexp';
      } else if ((/^\[object.*\]$/i ).test(f)) {
        t = 'object'
      }
    }
    if (t != 'object') {
      return t;
    }
    switch (obj) {
      case null:
        return 'null';
      case window:
        return 'window';
	  case document:
	    return 'document';
      case window.event:
        return 'event';
    }
    if (window.event && (event.type == obj.type)) {
      return 'event';
    }
    var c = obj.constructor;
    if (c != null) {
      switch(c) {
        case Array:
          t = 'array';
          break;
        case Date:
          return 'date';
        case RegExp:
          return 'regexp';
        case Object:
          t = 'object';	
        break;
        case ReferenceError:
          return 'error';
        default:
          var sc = c.toString();
          var m = sc.match(/\s*function (.*)\(/);
          if (m != null) {
            return 'object';
          }
      }
    }
    var nt = obj.nodeType;
    if (nt != null) {
      switch(nt) {
        case 1:
          return 'domelement';
        case 3:
          return 'string';
      }
    }
    if (obj.toString != null) {
      var ex = obj.toString();
      var am = ex.match(/^\[object (.*)\]$/i);
      if (am != null) {
        var am = am[1];
        switch(am.toLowerCase()) {
          case 'event':
            return 'event';
          case 'nodelist':
          case 'htmlcollection':
          case 'elementarray':
            return 'array';
          case 'htmldocument':
            return 'htmldocument';
        }
      }
    }
    return t;
  };  
  dump += (/string|number|undefined|boolean/.test(typeof(object)) || object == null) ? object : _recurse(object, typeof object);
  winName = window.open('', '', settings);
  if (jQuery.browser.msie || jQuery.browser.browser == 'opera' || jQuery.browser.browser == 'safari') {
	winName.document.write('<html><head><title> ' + title + ' </title><script type="text/javascript">' + script + '</script><head>');
	winName.document.write('<body>' + dump + '</body></html>');
  } else {
	winName.document.body.innerHTML = dump;
	winName.document.title = title;
	var ffs = winName.document.createElement('script');
	ffs.setAttribute('type', 'text/javascript');
	ffs.appendChild(document.createTextNode(script));
	winName.document.getElementsByTagName('head')[0].appendChild(ffs);
  }
  winName.focus();  
};
