
$(document).ready(function()
{
//	    //Replace Title
//	    var title = $("#mainContentSubPage-Title H1").html();
//	    if(title != null && title.length > 0)
//	    {
//	        document.title = title;
//	    }

        //Button style
        $(":submit").css("cursor", "pointer");
        $(":button").css("cursor", "pointer");
             
	    //

	    
	    //Sports feed
//	    var titleRSS = $("H1[@id*='SportsFeedTicker']");
//        var pRSS = $("p[@id *= 'SportsFeedTicker']");
//	    if(titleRSS.length > 0)
//	    {
//	        $.ajax({
//                type: "GET",
//                url: "/loadsportsfeed.aspx",
//                //url: "http://feeds.news.com.au/public/rss/2.0/heraldsun_sport_213.xml",
//                data: (new Date()).toTimeString(),
//                dataType: "html",
//                timeout: 10000,
//                success: function(html,xml){
//                    
//                    var xmldoc; 
//                    var title;
//                    var description;
//                    var latestDate = new Date(1900,1,1);
//                    if($.browser.msie)
//                    {
//                        xmldoc = new ActiveXObject("Msxml2.DOMDocument.4.0");
//                        xmldoc.loadXML(html);
//                    }
//                    else
//                    {
//                        xmldoc = (new DOMParser()).parseFromString(html, "text/xml");
//                    }
//                    
//                    $(xmldoc).find("item").each(function(n)
//                    {
//                        var tempDate = Date.parse($(this).find("pubDate").text());
//                        
//                        if(latestDate < tempDate)
//                        {
//                            latestDate = tempDate;
//                            title = $(this).find("title").text();
//                            description = $(this).find("description").text();
//                        }
//                        
//                    });
//                    
//                    titleRSS.html(title);
//                    pRSS.html(description);
//                },
//                error: function(xhr)
//                {
//                    pRSS.attr("innerText", "Fail to load Sports Feed.");                      
//                }
//            });
//	    }
        UpdateSportsFeed(0);
	    //Search 
		var searchObj = $("div#serachContainer fieldSet input[@type='text']");
		var btnDoSearch = $("div#serachContainer fieldSet input:button");
		
		searchObj.bind("focus",function(event)
		    {
		        if($(event.target).val() == "Enter Keyword")
		        {
		            $(event.target).val("");
		        }
		    }).bind("blur", function(event)
		    {
		        if($(event.target).val().length == 0)
		        {
		            $(event.target).val("Enter Keyword");
		        }
		    }).bind("keydown", function(event)
		    {
		        if(event.keyCode == 13)
		        {
		            btnDoSearch[0].click();
		        }
		    }
		    );
		
		btnDoSearch.bind("click",function(event)
		    {
		        if(searchObj.val() != "Enter Keyword")
		        {
		            window.document.location.href = "/SearchResult.aspx?SearchStr=" + searchObj.val();
		        }
		    })
		
		//Image auto size
		$(".largeImageAutoSize", this).each(function()
		{
			
			var objTD = $(this);
            //
			objTD.ImageAutoSize(548,0, true);
		})
		
		$(".newsLargeImageAutoSize", this).each(function()
		{
			var objDiv = $(this);

			objDiv.ImageAutoSize(210,250, false);
		})
		
		$("DIV#mainContentSubPage img").css("padding", "10px");
	});

function UpdateSportsFeed(k) 
{ 
    //alert("aa" + k);
     var titleRSS = $("H4[@id*='SportsFeedTicker']");
     var pRSS = $("p[@id *= 'SportsFeedTicker']");
	    if(titleRSS.length > 0)
	    {
	        $.ajax({
                type: "GET",
                url: "/loadsportsfeed.aspx",
                //url: "http://feeds.news.com.au/public/rss/2.0/heraldsun_sport_213.xml",
                data: (new Date()).toTimeString(),
                dataType: "html",
                timeout: 15000,
                success: function(html,xml){
                    
                    var xmldoc; 
                    var title;
                    var description;
                    var href;
                    var latestDate = new Date(1900,1,1);
                    if($.browser.msie)
                    {
                        xmldoc = new ActiveXObject("Msxml2.DOMDocument.4.0");
                        xmldoc.loadXML(html);
                    }
                    else
                    {
                        xmldoc = (new DOMParser()).parseFromString(html, "text/xml");
                    }
                    var tempIndex = -1;
                    $(xmldoc).find("item").each(function(n)
                    {
                        if(n == k)
                        {
                        //var tempDate = Date.parse($(this).find("pubDate").text());
                        
                        //if(latestDate < tempDate)
                        //{
                            //latestDate = tempDate;
                            title = $(this).find("title").text();
                            description = $(this).find("description").text();
                            href = $(this).find("link").text();
                        //}
                            tempIndex = n;
                        }
                        
                    });
                    if(tempIndex == -1)
                    {
                        k = 0;
                    }else
                    {
                        k++;
                    }
                    //alert(href);
                    if(tempIndex != -1)
                    {
                        titleRSS.html("<a href='"+href + "' style='PADDING-LEFT: 0px;PADDING-BOTTOM: 5px;FLOAT: left;TEXT-DECORATION: none;COLOR: #000000;'  target='_blank'>" + title + "</A>");
                        pRSS.html(description);
                    }
                    t=setTimeout('UpdateSportsFeed(' + k + ')',5000) 
                },
                error: function(xhr)
                {
                    pRSS.attr("innerText", "Fail to load Sports Feed.");                      
                }
            });
	    }
	    
} 


//
$.fn.ImageAutoSize = function(width,height, isPadding)
{
    $("img",this).each(function()
    {
        var image = $(this);;

        if(width > 0 && image.width()>width)
        {
            image.width(width);
            image.height(width/image.width()*image.height());
        }
        if(height > 0 && image.height()>height)
        {
            image.height(height);
            image.width(height/image.height()*image.width());
        }
        var widthGap = width - image.width();
        if(widthGap > 0 && isPadding)
        {
            //alert(widthGap);
            var stringValue = widthGap/2 + "px";
            //alert(stringValue);
            image.css("padding-left", stringValue);
        }
        
    });
    
    
} 
