var player = null;

function init_menu() 
{
    // Выбор таба по дефолту
    var $tabs = $(".menu").tabs({
    	select : function(event, ui) {
	        var url = $.data(ui.tab, 'load.tabs');
	        if( url ) {
	            location.href = url;
	            return false;
	        }
	        return true;
	    }
    });
    $(".menu").bind('select', function(event, ui) {
        var url = $.data(ui.tab, 'load.tabs');
        if( url ) {
            location.href = url;
            return false;
        }
        return false;
    });
    
    
}

function start_ajax_updates()
{
    //check_program_on_air ();
    //check_program_banner ();
    //check_program_online ();
}

function init_player(src, type, autoupdate, driver) 
{
    var source_video = $('#bndw').val();
    // Убирать или показывать панель кнопок wmp
    var uAgent = navigator.userAgent.toLowerCase();
    var g_IsIE = (uAgent.indexOf("msie") >= 0);
    var g_IsGecko = (uAgent.indexOf("gecko") >= 0);
    var g_IsIE7Plus = false;
    var t_uAgent = uAgent.match('msie [0-9]+');
    if(t_uAgent)
    {
        g_IsIE7Plus = (parseInt(t_uAgent[0].substring(5,t_uAgent[0].length)) >= 7);
    }

    // For IE < 7
    if (g_IsIE) 
    {
        $("#telek").css("height", "325px");
        $("#but_pan").css("display", "none");
    // For Opera & FireFox
    } else {
        $("#telek").css("height", "324px");
        $("#but_pan").css("display", "none");
    }

    // For IE 7-8
    if (g_IsIE7Plus) {
        $("#telek").css("height", "353px");
    }
    $("#but_pan").css("display", "none");

    // Смена потока
    $("#bndw").change(function () 
    {
        if ($(this).val() == $("#opt1").val()) 
        {
            window.location.href = "/?qu=low";
        } else if ($(this).val() == $("#opt2").val())
        {
            window.location.href = "/?qu=high";
        }
    });
    var state = 'init'
  
    var success_function = function (text)
            {       
                if (text != "" && state != 'warning') 
		//if (text != "") 
                {
		    state = 'warning';
                	
                    $('#tv_set').html('<table width=432 height=325 style="padding-left:10px"><tr><td valign="center">' + text + '</td></tr></table>');    
		    $('#fragment-0').html('');
                } else if (text == '' && state !='content') {
		//} else if (text == '') {
                	state = 'content'
                    box = $('#tv_set');
                    if ($('#tv_set > embed').attr("src") == undefined) 
                    {
                    	if (driver == 'flash') {
	                		var html = "<object width='432' height='324' id='MediaPlayer' name='MediaPlayer' type='application/x-shockwave-flash' classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' >" +
	                                "<param name='movie' value='/dynplayer/videoPlayer.swf' /> <param name='quality' value='high' />" +
	                                "<param name='bgcolor' value='#000000' />" + 
	                                "<param name='allowfullscreen' value='true' />" +
	                                "<param name='flashvars' value= '&videoWidth=0&videoHeight=0&dsControl=manual&dsSensitivity=100&serverURL=" + src + "&DS_Status=true&streamType=" + type + "&autoStart=true'/>" +
	                                "<embed src='/dynplayer/videoPlayer.swf' width='432' height='324' id='videoPlayer' quality='high' bgcolor='#000000' name='videoPlayer' allowfullscreen='true' pluginspage='http://www.adobe.com/go/getflashplayer' flashvars='&videoWidth=0&videoHeight=0&dsControl=manual&dsSensitivity=100&serverURL=" + src + "&DS_Status=true&streamType=" + type + "&autoStart=true' type='application/x-shockwave-flash'>" + 
	                                "</embed>" +
	                                "</object>"
                    	} else {
                    		 if (type == 'vod') {
                        		var path = src;
                	         } else {
                	        	var path = 'mms://90.156.228.8/' + src;
                	         }
                    		 var html = '<object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="432" height="324" id="slp">'+
                    			'<param name="source" value="/ovp/OVP.xap"/><param name="minRuntimeVersion" value="2.0.30923.0" />'+
                    			'<param name="onerror" value="onSilverlightError" />'+
                    			'<param name="background" value="black" />'+
                    			'<param name="MaxFrameRate" value="30" />'+
                    			'<div style="padding-top: 20px;text-align:center; width: 432px;"><a href="http://go.microsoft.com/fwlink/?LinkID=124807" style="text-decoration: none;">'+
                    				'<img src="/images/install_silverlight.gif" alt="Get Microsoft Silverlight" style="border-style: none;"/>'+
                    			'</a></div>'+
                    		    '<param name="initparams" value=\'autoplay=true, muted=false, playlistoverlay=true, theme=/ovp/themes/Default.xaml, stretchmode=Fit, stretchmodefullscreen=Fit, mediasource=' + path +  '\' />'+
                    			'</object>';
                    	}
                        box.html(html);
                		detect_flash();
                    }
                }
                init = false
            }  
  
    var f = function() {
    	$.ajax({
            type: "POST",
            url: "/ajax/comments",
            cache: false,
            dataType: "html",
            success: success_function,
            complete: function() {
            	if (autoupdate) {
            		setTimeout(f, 20000);
            	}
            }
        });
    }
    var f_no_autoupdate = function() {
    	$.ajax({
            type: "POST",
            url: "/ajax/comments",
            cache: false,
            dataType: "html",
            success: success_function,
        });
    }    
    f();
    return f_no_autoupdate;
}
                

function load_by_ajax(url, id, timeout, callback) 
{
	var f = function() {
        $.ajax({
            cache: false,
            dataType: "html",
        	type: 'POST',
            url: url, 
            success : function(msg) {
            	$(id).html(msg)
            },
            complete: function() {
                if (timeout) setTimeout(f, timeout);
                if (callback) callback()
            }
        })
	}
	f()
}

function check_program_online (){
    load_by_ajax("/ajax/panama_program", "#program_text", 10000, scroll_list);
}

function check_program_on_air (){
	load_by_ajax("/ajax/panama_on_air", "#now_on_air", 10000);
}

function check_program_banner (){
	load_by_ajax("/ajax/panama_on_banner", "#banner", 10000);
}

function check_channel (){
	load_by_ajax("/ajax/channel", "#quality", 50000);
}

function Toggle(id1,id2) {
    if (document.getElementById(id1).style.display == 'none') {
        document.getElementById(id1).style.display = 'block';
        document.getElementById(id2).innerHTML = '[-]';
          
    } else { 
        document.getElementById(id1).style.display = 'none';
        document.getElementById(id2).innerHTML = '[+]';
    }
}

function scroll_list () {
    if (document.getElementById("down") != null) 
    {
        jQuery(function($) {
            $("dl.program_text").scrollTo("dt.curnav", 800)
        });
        return;
    }
    setTimeout("scroll_list()", 3000);
}


