/* Global javascript file */

/**
 * Rounded corners for IE 
 */ 
if(typeof DD_roundies == 'object') {
    DD_roundies.addRule('.box-rounded', '8px');
	DD_roundies.addRule('.sxmButtonLarge', '25px');
	DD_roundies.addRule('.sxmButtonSmall', '18px');	
    DD_roundies.addRule('.box-rounded-top', '8px 8px 0 0');
    DD_roundies.addRule('.box-rounded-bottom', '0 0 8px 8px');
}

/**
 * SXM global namespace
 */ 
var SXM = {};

/**
 * SXM.tooltip shows/hide tooltip overlay.  Links with class .show-tooltip and id#tooltip-N
 * will show a tooltip using the content of a hidden div id#tooltip-content-N.
 * Example:
 
     <a href="#tooltip" class="show-tooltip" id="tooltip-1">tooltip</a>
     
     <div class="show-tooltip-content" id="tooltip-content-1">
        <p>This channel is only available on XM with Best of Sirius.</p>
        <p><a href="#expand" class="tooltip-expand">Learn why this happens sometimes</a></p>
        <div class="tooltip-hidden">
            <p>This channel is only available on XM with Best of Sirius.  This channel is only available on XM with Best of Sirius.  This channel is only available on XM with Best of Sirius.</p>
        </div>
        <p class="tooltip-footer">
            Let us make it up to you. <a href="#">How about 50% off an upgrade to Best of Sirius?</a>
        </p>            
    </div>
 
 */  
SXM.tooltip = {

    timer: null,
    width:0,
    shown:false,

    /**
     * Mouseover event of the tooltip, shows the tooltip
     */  
    over: function(e) {
        e.stopPropagation();
        var self = SXM.tooltip;
        if(self.timer) clearTimeout(self.timer);
        if(self.shown) return;
        self.tip.show();
        self.shown = true;
        if(!self.width) self.width = self.tip.width();
        var obj = $(this);
        if(obj.hasClass('show-tooltip')) {
            self.tip.data('link',obj);
            var id = (' '+this.id).split('-')[1];
            id = '#tooltip-content-'+id; 
            var div = $(id);
            if(div.length > 0) {
                self.tipContent.html( $(id).html() );
                self.position();
            }
        }
    },
    
    /**
     * Mouseout event of the tooltip, hides the tooltip
     */   
    out: function(e) {
        e.stopPropagation();
        var self = SXM.tooltip;
        if(self.timer) clearTimeout(self.timer);
        self.timer = setTimeout(function() {
            self.tip.hide();
            self.shown = false;
        },100);
    },
    
    /**
     * Sets the position of the tooltip according to its content
     */     
    position: function() {
        var obj = this.tip.data('link');
        var fixWidth = obj.width();
        var h = this.tip.height();
        var t = obj.offset().top;
        var l = obj.offset().left - this.width/2 + fixWidth/2;
/*
        var fixLeft = 0;
        this.tip.removeClass('tooltip-left tooltip-right');
        if(l>950) {
            this.tip.addClass('tooltip-right');
            fixLeft = 91;
        }
        if(l<270) {
            this.tip.addClass('tooltip-left');
            fixLeft = 0;        
        }
*/
        var top = t - h;
        this.tip.css( { left:l, top:top } );    
    },
    

    /**
     * Expands the tooltip to show more content
     */    
    expand: function(e) {
        e.preventDefault();
        var self = SXM.tooltip;
        $(this).addClass('tooltip-expand-active');
        $('#SXM-tooltip div.tooltip-hidden').show();
        self.position();
    },

    /**
     * Initializes the tooltip.
     * Adds the tooltip DOM element and mouseover/out events
     */
    init: function() {
        var main = $('#main');
        //this.offsetLeft = main.offset().left;
        this.tip = $('<div class="tooltip" id="SXM-tooltip"><div class="tooltip-top">&nbsp;</div><div class="tooltip-content clearfix"></div><div class="tooltip-bottom">&nbsp;</div></div>').appendTo('#container');
        this.tipContent = this.tip.find('.tooltip-content');
        $('a.tooltip-expand', this.tip[0]).live('click', this.expand);
        $('.show-tooltip, #SXM-tooltip').live('mouseover', this.over);
        $('.show-tooltip, #SXM-tooltip').live('mouseout', this.out);
        $('.show-tooltip', main[0]).live('click', function(e) { e.preventDefault() });
    }

}


// On page ready...
jQuery(function($) {
    SXM.tooltip.init();
    
    /**
    * Matches the heights of a collection of DOM elements to equal the tallest.
    *
    * @param {Boolean} includeMargin Indicates whether or not to include the margins.
    * @returns {Object} jQuery DOM collection.
    */
    $.fn.matchHeights = function (includeMargin) {
        var heightMax = 0;
        return this.each(function () {
            heightMax = Math.max(heightMax, $(this).outerHeight(includeMargin));
        }).each(function () {
            var that = $(this),
                property = $.browser.msie && $.browser.version < 7 ? 'height' : 'min-height',
                diff = heightMax - that.outerHeight(includeMargin),
                value = that.height() + diff + "px";
            that.css(property, value);
        });
    }

    $('#upcoming-live-sports').find('div.sport-group').each(function () {
        $(this).children().matchHeights();
    });
    
});

// usage: log('inside coolFunc',this,arguments);
// paulirish.com/2009/log-a-lightweight-wrapper-for-consolelog/
window.log = function(){
  log.history = log.history || [];   // store logs to an array for reference
  log.history.push(arguments);
  if(this.console){
    console.log( Array.prototype.slice.call(arguments) );
  }
};

// catch all document.write() calls
(function(doc){
  var write = doc.write;
  doc.write = function(q){ 
    log('document.write(): ',arguments); 
    if (/docwriteregexwhitelist/.test(q)) write.apply(doc,arguments);  
  };
})(document);
