function getIPInfo() 
{   
    var ip = "127.0.0.1";
    
    try 
    {
        //var sURL = "http://iplocationtools.com/ip_query.php";
        var sURL = "/Player/Helper/GetIPInfo.aspx";
                
        $.ajax({
            type: "GET",
            url: sURL,
            dataType: "xml",
            cache: false,
            async: false,
            success: function(data){

                $(data).find("Response").each(function() {
                    var xmlResponse = $(this);  
                    
                    ip = xmlResponse.find("Ip").text();
                });
            }
        });
    }
    catch(e)
    {
    }    

    return ip;
}

function requestGatewayInfo(stationId, ipAddress)
{
    try
    {       
        var sURL = "/Player/Helper/GetGatewayInfo.aspx?stationId=" + stationId + "&ip=" + ipAddress;

        $.ajax({
            type: "GET",
            url: sURL,
            dataType: "xml",  
            cache: false,
            async: false,
            success: function(data){
                $(data).find("GatewayAd").each(function() {                    
                    var xmlAd = $(this);  
                    
                    var gwId = xmlAd.find("GatewayId").text();
                    
                    g_GWMedia = xmlAd.find("Media").text();
                    g_GWRuntime = parseInt(xmlAd.find("Duration").text()) * 1000;
                    g_GWBanner = xmlAd.find("Banner").text();    
                    g_GWMediaImage = xmlAd.find("MediaImage").text();  
                    g_GWLink = xmlAd.find("Link").text();  
                    g_GWFolder = xmlAd.find("Folder").text();  

                    g_GWLink = generateGWClickThruURL(g_GWLink, gwId, stationId);
                    
                    var regExp = /^(http|https|www)/i ;

                    var isUrl = false;
                    if(g_GWMedia != "") 
                    {
                        isUrl = regExp.test(g_GWMedia);
                    }				
                    if(isUrl == false )
                    {
                        if(parseInt(g_GWFolder) < 0) 
                        {
                            g_GWMedia = "http://content.streamaudio.com/GatewayAds/owner/" + Math.abs(parseInt(g_GWFolder)) + "/" + g_GWMedia;					
                        }
                        else 
                        {
                            g_GWMedia = "http://content.streamaudio.com/GatewayAds/" + g_GWFolder + "/" + g_GWMedia;					
                        }
                    }
                    
                    if(isUrlWinVideo(g_GWMedia) || isUrlWinAudio(g_GWMedia)) 
                    {
                        g_GWMedia = g_GWMedia.replace("content.streamaudio.com", "archive.streamaudio.com");
                    }
                    
                    //debug("testMsg", "Gateway media " + g_GWMedia, true); 
                                    
                });            
            }
        });        
    }
    catch (e)
    {
    }
    
    displayGateway();  
}

function loadGateway(bannerDivName, bannerUrl, clickUrl, bannerWidth, bannerHeight, bannerImageUrl) {
    
    try {
        var html = "";
        
        var divBanner = $("#" + bannerDivName);
        
        if(isUrlFlash(bannerUrl)) {
            
            //debug("testMsg", "gateway is flash", true); 
            
            loadBanner(bannerDivName, bannerUrl, clickUrl, bannerWidth, bannerHeight);	
            
            startGatewayTimeout(g_GWRuntime + 10000);
        }
        else if(isUrlWinAudio(bannerUrl)) {
            //debug("testMsg", "gateway is win audio", true); 
            
            loadBanner(bannerDivName, bannerImageUrl, clickUrl, bannerWidth, bannerHeight);	
            
            startGatewayWM("myxamlGatewayAudio.xaml", 1, 1);	
            
            startGatewayTimeout(g_GWRuntime + 30000);	
        }
        else if(isUrlWinVideo(bannerUrl)) {
            //debug("testMsg", "gateway is win video", true); 
            
            startGatewayWM("myxamlGateway" + bannerWidth + "x" + bannerHeight + ".xaml", bannerWidth, bannerHeight);	
            
            startGatewayTimeout(g_GWRuntime + 30000);
        }
        else {
            //debug("testMsg", "gateway is unknown type " + bannerUrl, true); 
            
            startGatewayTimeout(3000);
        }
    }
    catch(e) {
        //debug("testMsg", "ERR:loadGateway: " + e, true); 
    }
}

function startGatewayTimeout(runtime) {
    
    //debug("testMsg", "gateway timeout in " + runtime, true); 
    
    setTimeout("gatewayTimeout()", runtime);
}

function gatewayTimeout() {
    //alert("gatewayTimeout");
    onGatewayMediaEnded();
}

function startGatewayWM(xamlGateway, width, height) {
    try {
        var parentElementGateway = document.getElementById(g_SilverlightPluginHostGateway);

        createMySilverlightPluginVideo(parentElementGateway, xamlGateway, width, height)
        
        setGatewayMediaSource();
    }
    catch(e) {

    }
}

function setGatewayMediaSource() {

    try {
        var plugin = document.getElementById(g_SilverlightPluginCtrlGateway);     
                
        var media = plugin.content.findName("media");
        
        media.Source = g_GWMedia;
        
        //alert(media.Source);
    }
    catch(e)
    {
        //alert("setGatewayMediaSource error " + e);
        setTimeout("setGatewayMediaSource()", 1000);
    }
} 


function onGatewayMediaEnded() {
    try {
        if(!g_GWIsEnded) {
            g_GWIsEnded = true;
            
            //debug("testMsg", "gateway media ended.", true); 
            
            hideGateway();
    	
            startRadio();
        }
        else {
            //debug("testMsg", "gateway media already ended.", true); 
        }
    }
    catch(e)
    {
        //debug("testMsg", "ERR:onGatewayMediaEnded: " + e, true); 
    }
}

function hideGateway() {
    try {
    	$("#" + g_SilverlightPluginHostGateway).hide();	
        $("#" + g_dvBanner2).hide();
        $("#" + g_dvBanner1).hide();
    }
    catch(e)
    {
        //debug("testMsg", "ERR:hideGateway: " + e, true); 
    }
}


function displayGateway() {

    //alert(g_GWMedia);
    //alert(g_GWLink);
    //alert(g_GWMediaImage);
    //debug("testMsg", "to display " + g_GWMedia, true); 
    
    try {
        loadGateway(g_dvBanner2, g_GWMedia, g_GWLink, g_Banner2Width, g_Banner2Height, g_GWMediaImage);
        loadBanner(g_dvBanner1, g_GWBanner, g_GWLink, g_Banner1Width, g_Banner1Height);
    }
    catch(e)
    {}
}