$(document).ready(function(){
//  window.setInterval(function(){
//    document.getElementById('antiTimeoutTransparentImage').src='/ping_server?refresh='+Math.floor(Math.random()*999999);
//  }, 60*1000);



  // attach help tooltips
  $('.help-message-tooltip').live('mouseover', function(event){
    if ($(this).data('qtip'))
    {
      return;
    }

    $(this).qtip({
      overwrite: false,
//      content: actionContent,
      content: $(this).find('.tooltip-content').html(),
//      hide: {
//            fixed: true // Make it fixed so it can be hovered over
//         },
      position: {
//        adjust: { x: 16, y: 16 },
        corner: {
          target: 'topMiddle',
          tooltip: 'bottomMiddle'
        }
      },

      style: {
        border: {
          width: 1,
          radius: 0,
          color: '#999'
        }
      }
    });

    if($(this).find('.tooltip-detailed-info-link').length > 0)
    {
      $(this).css('cursor', 'pointer');
      $(this).click(function(){

        var popupWindowWidth = 825;
        var popupWindowHeight = 540;
        var popupWindowTop = (screen.height/2)-(popupWindowHeight/2);
        var popupWindowLeft = (screen.width/2)-(popupWindowWidth/2);

        var url = $(this).find('.tooltip-detailed-info-link').html().replace(/\&amp;/g,'&');
        newwindow = window.open(url, 'youseeu_help', 'width='+popupWindowWidth+', height='+popupWindowHeight+', resizable, left='+popupWindowLeft+', top=' + popupWindowTop);
        if (window.focus) {
          newwindow.focus()
        }
        return false;
      });

    }

    $(this).trigger('mouseover');
  });

  $('.qTipsPopup').live('click', function(){
    var popupWindowWidth = 825;
        var popupWindowHeight = 540;
        var popupWindowTop = (screen.height/2)-(popupWindowHeight/2);
        var popupWindowLeft = (screen.width/2)-(popupWindowWidth/2);

        var url = $(this).attr('href');
        newwindow = window.open(url, 'youseeu_help', 'width='+popupWindowWidth+', height='+popupWindowHeight+', resizable, left='+popupWindowLeft+', top=' + popupWindowTop);
        if (window.focus) {
          newwindow.focus()
        }
    return false;
  })

});

$.fn.decorateDialog = function()
  {
  if($(this).find('.ui-widget-dialog-border').length == 0){
    $(this).prepend('<div class="ui-widget-dialog-border ui-dialog-border-top">\n\
    <div class="ui-border-tl"></div>\n\
    <div class="ui-border-b"></div>\n\
    <div class="ui-border-tr"></div>\n\
    </div>');
    $(this).append('<div class="ui-widget-dialog-border ui-dialog-border-bottom">\n\
      <div class="ui-border-bl"></div>\n\
      <div class="ui-border-b"></div>\n\
      <div class="ui-border-br"></div>\n\
    </div>\n\
    <div class="ui-widget-dialog-border ui-dialog-border-left"></div>\n\
    <div class="ui-widget-dialog-border ui-dialog-border-right"></div>');
      };
  }

// some patches for IE array functions
//@see http://stackoverflow.com/questions/2790001/fixing-javascript-array-functions-in-internet-explorer-indexof-foreach-etc
// 'use strict';

// Add ECMA262-5 method binding if not supported natively
//
if (!('bind' in Function.prototype)) {
    Function.prototype.bind= function(owner) {
        var that= this;
        if (arguments.length<=1) {
            return function() {
                return that.apply(owner, arguments);
            };
        } else {
            var args= Array.prototype.slice.call(arguments, 1);
            return function() {
                return that.apply(owner, arguments.length===0? args : args.concat(Array.prototype.slice.call(arguments)));
            };
        }
    };
}

// Add ECMA262-5 string trim if not supported natively
//
if (!('trim' in String.prototype)) {
    String.prototype.trim= function() {
        return this.replace(/^\s+/, '').replace(/\s+$/, '');
    };
}

// Add ECMA262-5 Array methods if not supported natively
//
if (!('indexOf' in Array.prototype)) {
    Array.prototype.indexOf= function(find, i /*opt*/) {
        if (i===undefined) i= 0;
        if (i<0) i+= this.length;
        if (i<0) i= 0;
        for (var n= this.length; i<n; i++)
            if (i in this && this[i]===find)
                return i;
        return -1;
    };
}
if (!('lastIndexOf' in Array.prototype)) {
    Array.prototype.lastIndexOf= function(find, i /*opt*/) {
        if (i===undefined) i= this.length-1;
        if (i<0) i+= this.length;
        if (i>this.length-1) i= this.length-1;
        for (i++; i-->0;) /* i++ because from-argument is sadly inclusive */
            if (i in this && this[i]===find)
                return i;
        return -1;
    };
}
if (!('forEach' in Array.prototype)) {
    Array.prototype.forEach= function(action, that /*opt*/) {
        for (var i= 0, n= this.length; i<n; i++)
            if (i in this)
                action.call(that, this[i], i, this);
    };
}
if (!('map' in Array.prototype)) {
    Array.prototype.map= function(mapper, that /*opt*/) {
        var other= new Array(this.length);
        for (var i= 0, n= this.length; i<n; i++)
            if (i in this)
                other[i]= mapper.call(that, this[i], i, this);
        return other;
    };
}
if (!('filter' in Array.prototype)) {
    Array.prototype.filter= function(filter, that /*opt*/) {
        var other= [], v;
        for (var i=0, n= this.length; i<n; i++)
            if (i in this && filter.call(that, v= this[i], i, this))
                other.push(v);
        return other;
    };
}
if (!('every' in Array.prototype)) {
    Array.prototype.every= function(tester, that /*opt*/) {
        for (var i= 0, n= this.length; i<n; i++)
            if (i in this && !tester.call(that, this[i], i, this))
                return false;
        return true;
    };
}
if (!('some' in Array.prototype)) {
    Array.prototype.some= function(tester, that /*opt*/) {
        for (var i= 0, n= this.length; i<n; i++)
            if (i in this && tester.call(that, this[i], i, this))
                return true;
        return false;
    };
}

