// This document contains confidential and proprietary information of ninemsn and 
// may be protected by patents, trademarks, copyrights, trade secrets, and/or other 
// relevant state, federal, and foreign laws.  Its receipt or possession does not 
// convey any rights to reproduce, disclose its contents, or to manufacture, use or 
// sell anything contained herein.  Forwarding, reproducing, disclosing or using 
// without specific written authorization of ninemsn is strictly forbidden.
// 
// 2008 ninemsn 2008.  All rights reserved.
// 
// ===================================================================================

// ===================================================================================
//  GLOBAL VARS
// ===================================================================================

// Sets the default silder position when the page first loads and the user does not 
// have a cookie containing their last used slider position
// ** This has been stored in a highlight WN_Def_Slider_Pos so producers can          
// ** change the default 

var documentURL = document.URL;

if(documentURL == "http://optusfeed.ninemsn.com.au/ninemsn_window.aspx")
    var LINKTRACKINGPREFIX = "OPTUS_WRP_";
else    
    var LINKTRACKINGPREFIX = "OPTUS_OTP_";

var JS_SECTION = 'homepage';
var JS_OMNTR_SUITEID='msnportalaucatpartners,msnportalaucatglobal';
var JS_OMNTR_CATEGORY='partners';
var JS_OMNTR_LINKINTERNALFILTERS='optuszoo.ninemsn.com.au';
var JS_OMNTR_COMPANY='optus';
var JS_OMNITURE_COMPANY = 'Optus';
var JS_PAGE_COBRAND = 'Optus';
var JS_SITE='Optus';
var JS_OMNITURE_CATEGORY='msnportalaumisc';

// sets the default category to slide to
var defaultSliderPos = 1;

// Used to prevent omni tracking on initial load of page

var FIRST_LOAD = true;
// Stores current slider position
var CURR_POS;

//Slide Interval Positions
var NEWS_POS = 2;
var POP_POS = 13;
var SPORT_POS = 24;
var ENT_POS = 35;

// Used by slider animation functions
var SLIDE_STEP = 1;
var SLIDE_END;
var SLIDE_POS;
var SLIDE_FUNC_ID;
var lock = false;
// Limits the amount of character that can be rendered within a headline
var FEED_MAX_CHAR = 53;
// Stores the vid and spotlight background color class so alternate bg colors 
// can be set when user clicks different tabs
var SL_CLASS = "";
var CTXS_CLASS_WIN = false;
//RSS feed content urls for each slider position
//Each url merges 2 highlights, 1 for the main image and 1 for the headline links
var MAIN_FEED = [];
MAIN_FEED[0] = "/share/com/highlights/rss.aspx?hltid=71,71&hlgroup=72784,72783&siteid=2327&media=img";
MAIN_FEED[1] = "/share/com/highlights/rss.aspx?hltid=71,71&hlgroup=72787,72813&siteid=2327&media=img";
MAIN_FEED[2] = "/share/com/highlights/rss.aspx?hltid=71,71&hlgroup=72789,72796&siteid=2327&media=img";
MAIN_FEED[3] = "/share/com/highlights/rss.aspx?hltid=71,71&hlgroup=72794,72790&siteid=2327&media=img";
var CTX_SEARCH_FEED = [];
CTX_SEARCH_FEED[0] = "/share/com/highlights/rss.aspx?hltid=71&hlgroup=75322&siteid=2327&media=img";
CTX_SEARCH_FEED[1] = "/share/com/highlights/rss.aspx?hltid=71&hlgroup=75323&siteid=2327&media=img";
CTX_SEARCH_FEED[2] = "/share/com/highlights/rss.aspx?hltid=71&hlgroup=75324&siteid=2327&media=img";
CTX_SEARCH_FEED[3] = "/share/com/highlights/rss.aspx?hltid=71&hlgroup=75325&siteid=2327&media=img";

// ===================================================================================
//  PAGE INITIALIZATION
// ===================================================================================

    
// ===================================================================================
// FuncName    : $(document).ready
// FuncType    : Event Handler
// Event Obj   : document 
// Event Type  : onLoad 
// Args        : None
// Summary     : Binds slider click/drag events, Tab click events, Search click
//               events, a Logo click event, Tracking events, displays the breaking 
//               news headline if available, and checks a cookie for the default 
//               slider position.                            
// ===================================================================================

$(document).ready(function()
{
    $("#slideLeft").bind("click", moveMarker);
    $("#slideRight").bind("click", moveMarker);
    
    $("#slideBar1").bind("click", moveMarker);
    $("#slideBar2").bind("click", moveMarker);
    $("#slideBar3").bind("click", moveMarker);
    $("#slideBar4").bind("click", moveMarker);

    $("#newsImg").bind("click", moveMarker);
    $("#gossImg").bind("click", moveMarker);
    
    $("#slideMarker").bind("mousedown", startDrag);
    $("#slideMarker").bind("mouseup", stopDrag);

    $("#todayTab").bind("click", RenderToday);

    $("#logo_wrapper").bind("click", TrackNineLogo);
  
    //cat_hl_88833 = WN_Breaking_News
    if($("#cat_hl_88833").length)
    {
        $("#breakingNews").show();
        $("#breakingNews").addClass("break_news_bg");
        $("#breakingNews a").bind("click", TrackBreakNews);
    } 
    
    // the switch-case tracks the viewer/default category
    switch (parseInt($.cookie('ninemsn_window_slider_pos') ? $.cookie('ninemsn_window_slider_pos') : defaultSliderPos))
    {
        case 1: TrackSliderDef("NEWS"); 
        break;
        case 2: TrackSliderDef("POP"); 
        break;
        case 3: TrackSliderDef("SPORTS"); 
        break;
        case 4: TrackSliderDef("ENT");
        break;
        default: break;
    }
    //checks for the position of the slider and then gets the slider move to the right position
    checkCookie();
    eraseOptusBrandCookie();
});


function eraseOptusBrandCookie() {
    if(document.cookie && (document.cookie.indexOf("brand=optus") != -1) || (document.cookie.indexOf("brand=Optus") != -1)) {
    document.cookie = "brand=nobrand;expires=Thu, 2 Aug 2000 20:00:00 UTC;domain =.ninemsn.com.au;path=/";
    }
};



// ===================================================================================
//  SLIDER CONTROL FUNCTIONS
// ===================================================================================

// ===================================================================================
// FuncName    : setCookie(val)
// FuncType    : Helper function   
// Args        : 1) val - the current position of the slide marker
// Summary     : Stores a cookie containing the current position of the slide marker. 
//               The next time the user visits the page, the cookie is read and the  
//               slide marker moves to the stored position.                           
// =================================================================================== 

function setCookie(val)
{
    $.cookie('ninemsn_window_slider_pos', val, {expires:3650});
}


// ===================================================================================
// FuncName    : checkCookie()
// FuncType    : Helper function   
// Args        : None
// Summary     : Reads the stored cookie and moves the slide marker to the respective 
//               position.                 
// ===================================================================================
     
function checkCookie()
{
    var cookiePos =  parseInt($.cookie('ninemsn_window_slider_pos') ? $.cookie('ninemsn_window_slider_pos') : defaultSliderPos);
    CURR_POS = cookiePos;
    // reads the cookie for the slider position and executes the function (moveMarker) as if the div was 'clicked' on
    switch (cookiePos){
        case 1: $("#slideBar1").click(); break;
        case 2: $("#slideBar2").click(); break;
        case 3: $("#slideBar3").click(); break;
        case 4: $("#slideBar4").click(); break;
        default: break;
    }       
}


// ===================================================================================
// FuncName    : startDrag()
// FuncType    : Event Handler
// Event Obj   : #slideMarker
// Event Type  : onMouseDown      
// Args        : None
// Summary     : Fires when user mouses down on the slide marker.  The lock is used to 
//               make sure the slide marker is not currrently animating (ie. user clicks
//               on slide marker several times at once).  While the mouse is pressed and within
//               the slideBar area, the slide marker will follow the mouse horizontally.                        
// ===================================================================================
    
function startDrag()
{
    if (!lock)
    {
        $('#slideBar').bind("mousemove", getMouseX);
    }
}

// ===================================================================================
// FuncName    : stopDrag()
// FuncType    : Event Handler
// Event Obj   : #slideMarker
// Event Type  : onMouseUp     
// Args        : None
// Summary     : Fires when the user mouses up after sliding the slide marker.  
//               The getMouseX() function is deactivated and the slide marker is set 
//               to one of the interval positions.  The specific content for a given 
//               position is then rendered.
// ===================================================================================
   
function stopDrag()
{
    $('#slideBar').unbind("mousemove", getMouseX);
    var slidePos = parseInt($('#slideMarker').css("left").match(/\d+/));
    
    if (slidePos >= 0 && slidePos <= 7 )
    {
        $('#slideMarker').css("left", NEWS_POS + "px");
        loadNews();        
    } 
    else if (slidePos > 7 && slidePos <= 18)
    {
        $('#slideMarker').css("left", POP_POS + "px");
        loadHot(); 
    }
    else if (slidePos > 18 && slidePos <= 29)
    {
        $('#slideMarker').css("left", SPORT_POS + "px");
        loadSports(); 
    }
    else 
    {
        $('#slideMarker').css("left", ENT_POS + "px");
        loadGoss(); 
    }
}


// ===================================================================================
// FuncName    : getMouseX() 
// FuncType    : Event Handler
// Event Obj   : #slideBar 
// Event Type  : onMouseMove      
// Args        : None
// Summary     : Fires when the user drags the slide marker within slider control.  
//               The slide marker's "left" position changes according to the user's 
//               mouse position within the slider control.  
// ===================================================================================

function getMouseX()
{       
    var tempX = event.clientX;       
              
    if (tempX > 125 && tempX < 164)
    {
        $('#slideMarker').css("left", (tempX - 125) + "px");
    }
   event.returnValue = false;                   
}


// ===================================================================================
// FuncName    : moveMarker()
// FuncType    : Event Handler
// Event Obj   : any element within the slideCtrl
// Event Type  : onClick    
// Args        : None
// Summary     : Fires when the user clicks on any element within the slide control.  
//               The slide marker will "slide" to the element that was clicked.  
//               The specific content for a given position is then rendered.  A lock
//               is used to prevent subsequent clicks before animation is done.
// ===================================================================================
   
function moveMarker()
{
    var curPos;    
    if (!lock)
    {
        switch(this.id)
        {
            case 'slideBar1':
            default:
            case 'newsImg':
                slideInit(NEWS_POS);
                loadNews();
                break;
            case 'slideBar2':
                slideInit(POP_POS);
                loadHot();
                break;
            case 'slideBar3':
                slideInit(SPORT_POS);
                loadSports(); 
                break;
            case 'slideBar4':
            case 'gossImg':
                slideInit(ENT_POS);
                loadGoss(); 
                break;
            case 'slideLeft': 
                curPos = parseInt($('#slideMarker').css("left").match(/\d+/));
                if (curPos == NEWS_POS)
                {
                    break;
                } 
                else if (curPos == POP_POS)
                {
                    slideInit(NEWS_POS);
                    loadNews();
                    break;
                }
                else if (curPos == SPORT_POS)
                {
                    slideInit(POP_POS);
                    loadHot();
                    break;
                } 
                else if (curPos == ENT_POS)
                {
                    slideInit(SPORT_POS);
                    loadSports();
                    break;
                }
                break;
            case 'slideRight':
                curPos = parseInt($('#slideMarker').css("left").match(/\d+/));
                  
                if (curPos == ENT_POS)
                {
                    break;
                } 
                else if (curPos == SPORT_POS)
                {
                    slideInit(ENT_POS);
                    loadGoss();
                    break;
                } 
                else if (curPos == POP_POS)
                {
                    slideInit(SPORT_POS);
                    loadSports();
                    break;
                } 
                else if (curPos == NEWS_POS)
                {
                    slideInit(POP_POS);
                    loadHot();
                    break;
                }
                break;            
        }
    }
}


// ===================================================================================
// FuncName    : slideInit(endPos)
// FuncType    : Helper function   
// Args        : 1) endPos - the final position the slide marker will stop at
// Summary     : Sets up global vars for the slide animation.  Using the lock, 
//               it ensures the slide marker has finished animating before another 
//               animation can be called (ie. user clicks on silde control rapidly).  
//               Calls the slide animation function over a given interval time period.
// ===================================================================================

function slideInit(endPos)
{
    if (!lock)
    {
        SLIDE_POS = parseInt($('#slideMarker').css("left").match(/\d+/));
        SLIDE_END = endPos;
      
        if (SLIDE_POS < SLIDE_END)
        {
            lock = true;
            SLIDE_FUNC_ID = setInterval(slideRight, 1);
        }
        else if (SLIDE_POS > SLIDE_END)
        {
            lock = true;
            SLIDE_FUNC_ID = setInterval(slideLeft, 1);
        }
        else
        {
            //"Twiddle" the slide marker to make it appear in IE
            SLIDE_POS++;
            $('#slideMarker').css("left", SLIDE_POS + "px");
            SLIDE_POS--;
            $('#slideMarker').css("left", SLIDE_POS + "px");
        }    
    }     
}
  
// ===================================================================================
// FuncName    : slideRight()
// FuncType    : Helper function   
// Args        : None
// Summary     : Animates the slide marker moving right                
// =================================================================================== 
     
function slideRight()
{
    SLIDE_POS += SLIDE_STEP; 
    if (SLIDE_POS < SLIDE_END)
    {      
        $('#slideMarker').css("left", SLIDE_POS + "px");
    }
    else 
    {
        $('#slideMarker').css("left", SLIDE_END + "px");
        lock = false;
        clearInterval(SLIDE_FUNC_ID);       
    }
}

// ===================================================================================
// FuncName    : slideLeft()
// FuncType    : Helper function   
// Args        : None
// Summary     : Animates the slide marker moving left                 
// ===================================================================================

function slideLeft()
{
    SLIDE_POS -= SLIDE_STEP;
    if (SLIDE_POS > SLIDE_END)
    {
        $('#slideMarker').css("left", SLIDE_POS + "px");
    }
    else 
    {
        $('#slideMarker').css("left", SLIDE_END + "px");
        lock = false;
        clearInterval(SLIDE_FUNC_ID);        
    }
}

// ===================================================================================
//  LOADING FEED CONTENT FUNCTIONS
// ===================================================================================


// ===================================================================================
// FuncName    : loadNews()
// FuncType    : Helper function   
// Args        : None
// Summary     : Makes an AJAX and JSON call to get the news feed headlines and video 
//               feed content, respectively.  On success, the data is wrapped in HTML    
//               and added to the page.  The HTML is dynamically styled, tracking 
//               functions are added, and the cookie for the slider position is set.                                 
// ===================================================================================

function loadNews()
{
    CURR_POS = 1;
    $('#main_replace').css("display", "none");
    jQuery.ajax(
        {
            url: MAIN_FEED[0],
            datatype: 'xml',
            type: 'GET',
            error: function()
            {
                $('#main_replace').html("");
            },
            success: function(result) 
            {
                dataAcquired(result);
                $("#imgaImg").bind("click", TrackNewsImg);
                $("#imgaText").bind("click", TrackNewsCap);
                $("#feedLinkGrp a").bind("click", TrackNewsHL);
                $("#imgaText").removeClass();
                $("#imgaText").addClass("news_hl_bg");
                // **** $("#srchBox").focus();
                
                if (FIRST_LOAD) 
                {
                    FIRST_LOAD = false;
                }
                else
                {
                    setCookie(1);
                    TrackSlider("NEWS");
                }
                
            }
        }
    );
}


// ===================================================================================
// FuncName    : loadHot()
// FuncType    : Helper function   
// Args        : None
// Summary     : Makes an AJAX and JSON call to get the most pop feed headlines and video 
//               feed content, respectively.  On success, the data is wrapped in HTML    
//               and added to the page.  The HTML is dynamically styled, tracking 
//               functions are added, and the cookie for the slider position is set.      
// ===================================================================================
     
function loadHot()
{
    CURR_POS = 2;
    $('#main_replace').css("display", "none");
    jQuery.ajax(
        {
            url: MAIN_FEED[1],
            datatype: 'xml',
            type: 'GET',
            error: function()
            {
                $('#main_replace').html("");
            },
            success: function(result) 
            {
                dataAcquired(result);
                $("#imgaImg").bind("click", TrackPopImg);
                $("#imgaText").bind("click", TrackPopCap);
                $("#feedLinkGrp a").bind("click", TrackPopHL);
                $("#imgaText").removeClass();
                $("#imgaText").addClass("pop_hl_bg");
                 // **** $("#srchBox").focus();
                if (FIRST_LOAD) 
                {
                    FIRST_LOAD = false;
                }
                else
                {
                    setCookie(2);
                    TrackSlider("POP");
                }
                
            }
        }
    );
}

// ===================================================================================
// FuncName    : loadSports()
// FuncType    : Helper function   
// Args        : None
// Summary     : Makes an AJAX and JSON call to get the sport feed headlines and video 
//               feed content, respectively.  On success, the data is wrapped in HTML    
//               and added to the page.  The HTML is dynamically styled, tracking 
//               functions are added, and the cookie for the slider position is set.               
// =================================================================================== 

function loadSports()
{
    CURR_POS = 3;
    $('#main_replace').css("display", "none");
    jQuery.ajax(
        {
            url: MAIN_FEED[2],
            datatype: 'xml',
            type: 'GET',
            error: function()
            {
                $('#main_replace').html("");
            },
            success: function(result) 
            {
                dataAcquired(result);
                $("#imgaImg").bind("click", TrackSportsImg);
                $("#imgaText").bind("click", TrackSportsCap);
                $("#feedLinkGrp a").bind("click", TrackSportsHL);
                
                $("#imgaText").removeClass();
                $("#imgaText").addClass("sport_hl_bg");
                // ****  $("#srchBox").focus();
                if (FIRST_LOAD) 
                {
                    FIRST_LOAD = false;
                }
                else
                {
                    setCookie(3);
                    TrackSlider("SPORTS");
                } 
                
            }
        }
    );
}

// ===================================================================================
// FuncName    : loadGoss()
// FuncType    : Helper function   
// Args        : None
// Summary     : Makes an AJAX and JSON call to get the goss feed headlines and video 
//               feed content, respectively.  On success, the data is wrapped in HTML    
//               and added to the page.  The HTML is dynamically styled, tracking 
//               functions are added, and the cookie for the slider position is set.                   
// ===================================================================================
     
function loadGoss()
{
    CURR_POS = 4;
    $('#main_replace').css("display", "none");
    jQuery.ajax(
        {
            url: MAIN_FEED[3],
            datatype: 'xml',
            type: 'GET',
            error: function()
            {
                $('#main_replace').html("");
            },
            success: function(result) 
            {
                dataAcquired(result);
                $("#imgaImg").bind("click", TrackEntImg);
                $("#imgaText").bind("click", TrackEntCap);
                $("#feedLinkGrp a").bind("click", TrackEntHL);
                
                $("#imgaText").removeClass();         
                $("#imgaText").addClass("goss_hl_bg");
                // ****  $("#srchBox").focus();
                if (FIRST_LOAD) 
                {
                    FIRST_LOAD = false;
                }
                else
                {
                    setCookie(4);
                    TrackSlider("ENT");
                }
                
            }
        }
    );
}


// ===================================================================================
// FuncName    : dataAcquired(result)
// FuncType    : Helper function   
// Args        : 1) result - result of the AJAX call
// Summary     : Wraps feed headline content in html and adds it to the page                   
// ===================================================================================
     
function dataAcquired(rss) 
{
    var rssData = rss.getElementsByTagName('rss')[0];
    var itemData = rss.getElementsByTagName('item');
    var imageCaption = itemData[0].getElementsByTagName("title")[0].firstChild.nodeValue;
    var imageUrl = itemData[0].getElementsByTagName("enclosure")[0].getAttribute("url");    
    imageUrl = imageUrl.replace("http://optusfeed.","http://msntoday.");
    var imageLink = itemData[0].getElementsByTagName("link")[0].firstChild.nodeValue;
    var htmlFragment = "";
    htmlFragment += '<div id="main_feeds">';
    htmlFragment += '<a href="' + imageLink +'" target="_parent" style="display:block;width:365px;height:144px;margin:0;padding:0;">';
    htmlFragment += '<div id="mainImg" style="background-image: url(' + imageUrl + '); width: 365px; height: 144px;">';
    htmlFragment += '</div></a>';
    htmlFragment += '<a id="imgaText" href="' + imageLink +'" target="_parent">'
    htmlFragment += '<div id="imgCap">' + imageCaption;
    htmlFragment += '</div></a><div id="feedLinkGrp">';   
    for(i=1; i < itemData.length; i++) 
    {
        htmlFragment += getListItemHTML(itemData[i].getElementsByTagName("title")[0].firstChild.nodeValue, itemData[i].getElementsByTagName("link")[0].firstChild.nodeValue,i);           
    }
    htmlFragment += "</div>";
    htmlFragment += "</div>";        
    $('#main_replace').html(htmlFragment).slideDown("normal");
}


// ===================================================================================
// FuncName    : getListItemHTML(title,link,i)
// FuncType    : Helper function   
// Args        : 1) title - headline title
//               2) link - headline link
//               3) i - headline humber
// Summary     : Wraps headline content in html                   
// ===================================================================================    
    
function getListItemHTML(title,link,i)
{
    var htmlFragment="";
    var subtitle=title;
    
    htmlFragment+='<div class="feedLink">';
    if(title.length>FEED_MAX_CHAR) 
        subtitle=title.substring(0,FEED_MAX_CHAR)+" ...";
    htmlFragment+='<img src="/img/wl_layout/arrow_bullet.gif" alt="" />';
    htmlFragment+='<a class="blkText" id="feedLink'+i+'" title="'+title+'" href="'+link+'" target="_parent">'+subtitle+'</a>';
    htmlFragment+='</div>';
    return htmlFragment;
}
// ===================================================================================
// FuncName    : jsonCallback(json, cd, error)
// FuncType    : Helper function   
// Args        : 1) json - json object returned
//               2) cd - 
//               3) error - error, if any
// Summary     : Wraps JSON data in HTML and adds to teh page.                       
// ===================================================================================

function jsonCallback(json, cd, error) 
{	
    if(!error){
        videoHTML = '<div class="contentHeading smblkText">VIDEO</div>';
        videoHTML += '<div class="contentGroupContainer">';
        var i = 1;
        for (key in json.video) 
        {
            videoHTML += getItemHTML(json.video[key].files.file[0].uri.$, json.video[key].title.$, json.video[key].uuid.$,i);
            i++;
        }
        videoHTML += '</div>';

        $('#vid_replace').html(videoHTML);
        $("#vidImageYo1").bind("error", function(){ $(this).attr("src", "/img/wl_layout/video_70x50.jpg"); });
        
        switch (CURR_POS)
        {
            case 1: $("#vid_replace a").bind("click", TrackNewsVid); break;
            case 2: $("#vid_replace a").bind("click", TrackPopVid); break;
            case 3: $("#vid_replace a").bind("click", TrackSportsVid);  break;
            case 4: $("#vid_replace a").bind("click", TrackEntVid);  break;
            default: break;
        }
        // ****  $("#srchBox").focus(); 
    }
}


// ===================================================================================
// FuncName    : getItemHTML(url, title, uuid, i)
// FuncType    : Helper function   
// Args        : 1) url -
//               2) title -
//               3) uuid -
// Summary     : Wraps JSON data in HTML                  
// ===================================================================================
    
function getItemHTML(url, title, uuid, i) 
{	
    var htmlFragment = "";
    htmlFragment +=  	'<div class="contentItem" >';
    htmlFragment += 		'<div class="contentImg">';
    htmlFragment += 			'<a  class="vidItem'+i+'" href="http://ninemsn.video.msn.com/v/en-au/v.htm?g=' + uuid + '" title="' + title + '">';
    htmlFragment += 				'<img id="vidImageYo'+i+'" src="' + url + '" width="70" height="50" title="' + title + '" />';
    htmlFragment += 			"</a>";
    htmlFragment += 		"</div>";
    htmlFragment += 		'<div class="contentText">';
    htmlFragment += 			'<a class="vidItem'+i+'" href="http://ninemsn.video.msn.com/v/en-au/v.htm?g=' + uuid + '" title="' + title + '">';
    htmlFragment += 			title;	
    htmlFragment += 			"</a>";	
    htmlFragment += 		"</div>";
    htmlFragment +=    "</div>";
    return htmlFragment;
}


// ===================================================================================
// FuncName    : getContextualSearchHTML(rss)
// FuncType    : Helper function   
// Args        : 1) rss - result of the AJAX call
// Summary     : Wraps feed headline content in html and adds it to the page  
// ===================================================================================
function getContextualSearchHTML(rss) 
{	
    var itemData = rss.getElementsByTagName('item');
    var imageTitle = itemData[0].getElementsByTagName("title")[0].firstChild.nodeValue;
    var imageUrl = itemData[0].getElementsByTagName("enclosure")[0].getAttribute("url");    
    var imageLink = itemData[0].getElementsByTagName("link")[0].firstChild.nodeValue;
    var htmlFragment = "";
    htmlFragment += '<div class="contentHeading smblkText contxSearch">SEARCH</div>';
    htmlFragment +=     '<div class="contentGroupContainer">';    
    htmlFragment +=  	    '<div class="contentItem contxSearchItem" >';
    htmlFragment += 		    '<div class="contentImg">';
    htmlFragment += 			    '<a class="searchItem" href="' + imageLink +'" title="' + imageTitle + '">';
    htmlFragment += 				    '<img id="searchImage" src="' + imageUrl + '" width="70" height="50" title="' + imageTitle + '" />';
    htmlFragment += 			    '</a>';
    htmlFragment += 		    '</div>';
    htmlFragment += 		    '<div class="contentText">';
    htmlFragment += 			    '<a class="searchItem" href="' + imageLink +'" title="' + imageTitle + '">';
    htmlFragment += 			        imageTitle;	
    htmlFragment += 			    '</a>';	
    htmlFragment += 		    '</div>';
    htmlFragment +=         '</div>';
    htmlFragment +=     '</div>';
    htmlFragment += '</div>';
    $('#contextual_search_replace').html(htmlFragment).show();   
}


// ===================================================================================
//  TAB FUNCTIONS
// ===================================================================================


// ===================================================================================
// FuncName    : RenderWin()
// FuncType    : Event Handler
// Event Obj   : #winTab
// Event Type  : onClick
// Args        : None 
// Summary     : When WIN Tab clicked, on first time, an AJAX call is made to get 
//               the WIN Tab content.  The content is wrapped in HTML and appended to 
//               the page, event handlers are added for tracking purposes, and CSS is 
//               dynamically updated.                                
// ===================================================================================

function RenderWin() 
{        
    CURR_POS = 4;
    if (this.className != "winSel")
    {
        if ($("#winContent").html() == "")
        {
            jQuery.ajax(
                {
                    url: "/tab/win.aspx",
                    datatype: 'xml',
                    type: 'GET',
                    success: function(result)
                    {
                        var p = result.getElementsByTagName("content");  
                        $("#winContent").html(p[0].childNodes[0].nodeValue);
                        //cat_hl_84850 = Win > Current competitions
                        $("#cat_hl_84850 span").eq(0).addClass("comp_gray_bg");
                        $("#cat_hl_84850 span").eq(9).addClass("comp_gray_bg");
                        $("#cat_hl_84850 span").eq(12).addClass("comp_gray_bg");
                        $("#cat_hl_84850 span").eq(0).attr("id", "COMPLINK1");
                        $("#cat_hl_84850 span").eq(0).bind("click", TrackComp);
                        $("#cat_hl_84850 span").eq(3).attr("id", "COMPLINK2");
                        $("#cat_hl_84850 span").eq(3).bind("click", TrackComp);
                        $("#cat_hl_84850 span").eq(6).attr("id", "COMPLINK3");
                        $("#cat_hl_84850 span").eq(6).bind("click", TrackComp);
                        $("#cat_hl_84850 span").eq(9).attr("id", "COMPLINK4");
                        $("#cat_hl_84850 span").eq(9).bind("click", TrackComp);
                        $("#cat_hl_84850 span").eq(12).attr("id", "COMPLINK5");
                        $("#cat_hl_84850 span").eq(12).bind("click", TrackComp);
                        $("#cat_hl_84850 span").eq(15).attr("id", "COMPLINK6");
                        $("#cat_hl_84850 span").eq(15).bind("click", TrackComp);
                        $("#cat_hl_84850 .footer").find("a").bind("click", TrackMoreComp);  
                
                    }
                }
            );
        }
        CTXS_CLASS_WIN = true;        
        document.getElementById('feedContent').style.display = 'none';
        document.getElementById('winContent').style.display = 'block';
        document.getElementById('winTab').className = "winSel";
        document.getElementById('winText').className = "tabText smTabSelText";
        document.getElementById('slideCtrl').style.display = "none";
        document.getElementById('todayInnerTab').className = "";
        document.getElementById('todayTab').className = "todayDef";
        document.getElementById('todayText').className = "tabText bigTabDefText";
      
        TrackTab("WIN");
        // ****  $("#srchBox").focus();
       
    }
}


// ===================================================================================
// FuncName    : RenderToday()
// FuncType    : Event Handler
// Event Obj   : #todayTab
// Event Type  : onClick
// Args        : None 
// Summary     : When Today Tab clicked, CSS styling is dynamically updated and tracking
//               information is added.           
// ===================================================================================

function RenderToday() 
{
    CURR_POS =  parseInt($.cookie('msntoday_slider_pos') ? $.cookie('msntoday_slider_pos') : defaultSliderPos);
    if (this.className != "")
    {
        FIRST_LOAD = true; 
        checkCookie();
        document.getElementById('feedContent').style.display = 'block';
        document.getElementById('winContent').style.display = 'none';
        document.getElementById('winTab').className = "winDef";
        document.getElementById('winText').className = "tabText smTabDefText";
        document.getElementById('slideCtrl').style.display = "block";
        document.getElementById('todayInnerTab').className = "todaySel";
        document.getElementById('todayTab').className = "";
        document.getElementById('todayText').className = "tabText bigTabSelText";
        TrackTab("TODAY");
    }
    
      
}


// ===================================================================================
//  SEARCH FUNCTIONS
// ===================================================================================


// ===================================================================================
// FuncName    : enterSearch()
// FuncType    : Event Handler
// Event Obj   : #srchBox
// Event Type  : onKeyPress
// Args        : None 
// Summary     : Checks for the Enter key pressed on the search box.             
// ===================================================================================

function enterSearch() 
{
    var keycode;
    if(window.event) keycode = window.event.keyCode;
    else if(e) keycode = e.which;
    else return true;
    if(keycode == 13) 
    {
        // **** submitSearch();
        return false;
    }
    return true;
}

// ===================================================================================
// FuncName    : submitSearch()
// FuncType    : Event Handler
// Event Obj   : #srchBtn
// Event Type  : onClick
// Args        : None 
// Summary     : Submits the search with the value typed in the Search Box.             
// ===================================================================================

// **** function submitSearch() 
// **** {
 // ****    var query = $('#srchBox').attr('value');
// ****     if (!query)
// ****         query = 'ninemsn';
// ****         window.open("http://search.ninemsn.com.au/results.aspx?FORM=MSNTOD&v=1&CY=en-au&RS=CHECKED&GO=GO&q=" + query);
// ****     TrackSearch();
// ****     return false;
// **** }


// ===================================================================================
//  OMNITURE TRACKING FUNCTIONS
// ===================================================================================


// ===================================================================================
// FuncName    : TrackInt(uname)
// FuncType    : Helper function   
// Args        : 1) uname - unique identifier for a given link (or group of links)
// Summary     : Calls the OCL tracking function with a given unique identifier of 
//               what was clicked.  Other tracking function below create the unique ID.                    
// ===================================================================================

function TrackInt(uname)
{
    try 
    { 
        var trackThis = LINKTRACKINGPREFIX + uname;
        SBKCustomLinkTracking(true,trackThis)
    }
    catch (err) 
    {
    }
}

function TrackNineLogo() 
{
    var uname = "NINEMSN_FRAME_LOGO";
    TrackInt(uname);
}

function TrackSearch() 
{
    var uname = "NINEMSN_FRAME_SEARCH";
    TrackInt(uname);
}


function TrackTab(val) 
{
    var uname = "NINEMSN_FRAME_TAB_" + val + "_INT";
    TrackInt(uname);
}

function TrackSlider(val) 
{
    var uname = "NINEMSN_FRAME_SLIDER_" + val + "_INT";
    TrackInt(uname);
}

function TrackBreakNews() 
{
    var uname = "NINEMSN_FRAME_BREAK_NEWS";
    TrackInt(uname);
}

function TrackNewsImg() 
{
    var uname = "NINEMSN_FRAME_NEWS_MAINIMG";
    TrackInt(uname);
}

function TrackNewsCap() 
{
    var uname = "NINEMSN_FRAME_NEWS_MAINCAP";
    TrackInt(uname);
}

function TrackNewsHL() 
{
    var uname = "NINEMSN_FRAME_NEWS_" + this.id.toUpperCase();
    TrackInt(uname);
}

function TrackNewsVid()
{
    
    var uname = "NINEMSN_FRAME_NEWS_" + this.className.toUpperCase();
    TrackInt(uname);
}

function TrackPopImg() 
{
    var uname = "NINEMSN_FRAME_POP_MAINIMG";
    TrackInt(uname);
}

function TrackPopCap() 
{
    var uname = "NINEMSN_FRAME_POP_MAINCAP";
    TrackInt(uname);
}

function TrackPopHL() 
{
    var uname = "NINEMSN_FRAME_POP_" + this.id.toUpperCase();
    TrackInt(uname);
}

function TrackPopVid()
{
    var uname = "NINEMSN_FRAME_POP_" + this.className.toUpperCase();
    TrackInt(uname);
}

function TrackSportsImg() 
{
    var uname = "NINEMSN_FRAME_SPORTS_MAINIMG";
    TrackInt(uname);
}

function TrackSportsCap() 
{
    var uname = "NINEMSN_FRAME_SPORTS_MAINCAP";
    TrackInt(uname);
}

function TrackSportsHL() 
{
    var uname = "NINEMSN_FRAME_SPORTS_" + this.id.toUpperCase();
    TrackInt(uname);
}

function TrackSportsVid()
{
    var uname = "NINEMSN_FRAME_SPORTS_" + this.className.toUpperCase();
    TrackInt(uname);
}

function TrackEntImg() 
{
    var uname = "NINEMSN_FRAME_ENT_MAINIMG";
    TrackInt(uname);
}

function TrackEntCap() 
{
    var uname = "NINEMSN_FRAME_ENT_MAINCAP";
    TrackInt(uname);
}

function TrackEntHL() 
{
    var uname = "NINEMSN_FRAME_ENT_" + this.id.toUpperCase();
    TrackInt(uname);
}

function TrackEntVid()
{
    var uname = "NINEMSN_FRAME_ENT_" + this.className.toUpperCase();
    TrackInt(uname);
}

function TrackComp() 
{
    var uname = "NINEMSN_FRAME_COMP_" + this.id;
    TrackInt(uname);
}

function TrackMoreComp() 
{
    var uname = "NINEMSN_FRAME_COMP_MORE";
    TrackInt(uname);
}

function TrackSliderDef(val){ 
    var uname = "NINEMSN_FRAME_SLIDER_" + val + "_DEFAULT";
    TrackInt(uname);
}

function TrackCtxs(val){ 
    var uname = "NINEMSN_FRAME_CTXS_" + val.data.ctxsArea;
    TrackInt(uname);
}











