stopping="0";
stopoff = "0";
tool = "";
tool_active = "";
reached = "";
news_loaded = "0";
frame_path_default = "/livetool_sptv/?automatic=1";
frame_path = frame_path_default;

$.fn.OnAirPatchMini = function () {
	dt = new Date();
    if (news_loaded=="0"){
        // 1. Get JSON from server
    $.getJSON("/onair.php?changed=" + dt,
              function(data){
              
              $.each(data, function()
                     {
                     
                     if (data.moderator.name != "Standardmoderator" && data.moderator.name != data.sendung.name){
                     var sendung = '<li class="st1 nowactive">Jetzt auf Sendung: <b>'+ data.sendung.name + '</b> mit <b>'+ data.moderator.name +'</b></li>';}
                     else {var sendung = '';}
                     
                     if(data.sendungsthemen.sendungsthema1){
                     if(data.sendungsthemen.url_sendungsthema1 != "")
                     {var sendungsthema1 = '<li class="st2"><a href="'+ data.sendungsthemen.url_sendungsthema1 +'">'+ data.sendungsthemen.sendungsthema1 +'</a></li>';}
                     else {var sendungsthema1 = '<li class="st2">'+ data.sendungsthemen.sendungsthema1 +'</li>';}
                     }
                     else {var sendungsthema1 = '';}
                     if(data.sendungsthemen.sendungsthema2){
                     if(data.sendungsthemen.url_sendungsthema2 != "")
                     {var sendungsthema2 = '<li class="st3"><a href="'+ data.sendungsthemen.url_sendungsthema2 +'">'+ data.sendungsthemen.sendungsthema2 +'</a></li>';}
                     else {var sendungsthema2 = '<li class="st3">'+ data.sendungsthemen.sendungsthema2 +'</li>';}
                     }
                     else {var sendungsthema2 = '';}
                     if(data.sendungsthemen.sendungsthema3){
                     if(data.sendungsthemen.url_sendungsthema3 != "")
                     {var sendungsthema3 = '<li class="st4"><a href="'+ data.sendungsthemen.url_sendungsthema3 +'">'+ data.sendungsthemen.sendungsthema3 +'</a></li>';}
                     else {var sendungsthema3 = '<li class="st4">'+ data.sendungsthemen.sendungsthema3 +'</li>';}
                     }
                     else {var sendungsthema3 = '';}
                     if(sendungsthema1 == "" && sendungsthema2 == "" && sendungsthema3 == "") {var head = '<li>Zur laufenden Sendung liegen keine Themen vor.</li>';}
                     else {var head = ''}
                     // $('#themes_tool').html('<ul id="tt_ul">' + head + sendung + sendungsthema1 + sendungsthema2 + sendungsthema3 + '</ul>');
					 $('#themes_tool').html('<ul id="tt_ul">' + sendung + '</ul>');
                     });	    	
              }
              );
			  news_loaded = "1"
		  }

}

/* $.fn.OnAirPatchMini = function ()
{
	dt = new Date();
    if (news_loaded=="0"){
        // 1. Get JSON from server
    $.getJSON("/onair.php?changed=" + dt,
              function(data){
              
              $.each(data, function()
                     {
                     
                     if (data.moderator.name != "Standardmoderator" && data.moderator.name != data.sendung.name){
                     var sendung = '<li class="st1 nowactive">Heute mit '+ data.moderator.name +'</li>';}
                     else {var sendung = '';}
                     
                     if(data.sendungsthemen.sendungsthema1){
                     if(data.sendungsthemen.url_sendungsthema1 != "")
                     {var sendungsthema1 = '<li class="st2"><a href="'+ data.sendungsthemen.url_sendungsthema1 +'">'+ data.sendungsthemen.sendungsthema1 +'</a></li>';}
                     else {var sendungsthema1 = '<li class="st2">'+ data.sendungsthemen.sendungsthema1 +'</li>';}
                     }
                     else {var sendungsthema1 = '';}
                     if(data.sendungsthemen.sendungsthema2){
                     if(data.sendungsthemen.url_sendungsthema2 != "")
                     {var sendungsthema2 = '<li class="st3"><a href="'+ data.sendungsthemen.url_sendungsthema2 +'">'+ data.sendungsthemen.sendungsthema2 +'</a></li>';}
                     else {var sendungsthema2 = '<li class="st3">'+ data.sendungsthemen.sendungsthema2 +'</li>';}
                     }
                     else {var sendungsthema2 = '';}
                     if(data.sendungsthemen.sendungsthema3){
                     if(data.sendungsthemen.url_sendungsthema3 != "")
                     {var sendungsthema3 = '<li class="st4"><a href="'+ data.sendungsthemen.url_sendungsthema3 +'">'+ data.sendungsthemen.sendungsthema3 +'</a></li>';}
                     else {var sendungsthema3 = '<li class="st4">'+ data.sendungsthemen.sendungsthema3 +'</li>';}
                     }
                     else {var sendungsthema3 = '';}
                     if(sendungsthema1 == "" && sendungsthema2 == "" && sendungsthema3 == "") {var head = '<li>Zur laufenden Sendung liegen keine Themen vor.</li>';}
                     else {var head = ''}
                     $('#themes_tool').html('<ul id="tt_ul">' + head + sendung + sendungsthema1 + sendungsthema2 + sendungsthema3 + '</ul>');
                     });	    	
              }
              );
    borschtsch = 1;
    newsfadeMini = function ()  {
        var newsli = $("#tt_ul li").length;
        if (borschtsch < newsli) {
            $("#tt_ul li.nextactive").removeClass("nextactive").addClass("nowactive");
            $("#tt_ul li.nowactive + li").addClass("nextactive");
            $("#tt_ul li.nowactive").fadeOut(300, function() {
                                             $(this).css({left: 500 + "px", display: "block"});
                                                      }).removeClass("nowactive");
            $("#tt_ul li.nextactive").animate({"left": 0}, 300);
            borschtsch++;
        } else if (borschtsch == newsli) {
            $("#tt_ul li.nextactive").removeClass("nextactive").addClass("nowactive");
            $("#tt_ul li.st1").addClass("nextactive");
            $("#tt_ul li.nowactive").fadeOut(300, function() {
                                                      $(this).css({left: 500 + "px", display: "block"});
                                                      }).removeClass("nowactive");
            $("#tt_ul li.nextactive").animate({"left": 0}, 300);
            borschtsch = 1;
        }
    };  
        
        news_loaded = "1";
        
        onairfadeMini = setInterval(newsfadeMini, 10000);
        
        $("#themes_tool").hoverIntent(function () {clearInterval(onairfadeMini);
                                    }, function () {
                                    onairfadeMini = setInterval(newsfadeMini, 10000);
                                    });
        
            // setInterval ( "$('#info_live h3').newsfade()", 4000 );
    }
}; */

$.fn.toolShowBar = function() {
    if($('#sputtv_bar_clone').length > 0){$('#sputtv_bar_clone').remove();}
    if($('#sputtv_bar').length > 0){$('#sputtv_bar').remove();}
    if(tool_active != "no"){
    if( $.cookie('toolActive') == null ) {$('#new_header').append('<div id="sputtv_bar"><img id="img_onair" src="/img/skins/facelift2011/sptv_onair.png" alt="" /><div id="tooloff"></div><div id="toolpopup"></div></div>');$('#sputtv_bar').fadeIn(300);}
    else {
        if ($.cookie('toolPopup') != null) {$('#new_header').append('<div id="sputtv_bar"><img id="img_onair" src="/img/skins/facelift2011/sptv_onair.png" alt="" /><div id="toolpopupinact"></div></div>');$('#sputtv_bar').css({"display": "block"});}
        else {
            if($.cookie('toolActive') == "on"){$('#new_header').append('<div id="sputtv_bar"><img id="img_onair" src="/img/skins/facelift2011/sptv_onair.png" alt="" /><div id="tooloff"></div><div id="toolpopup"></div></div>');$('#sputtv_bar').css({"display": "block"});}
            else {
            if ( $.browser.msie )
   				 {
					$('#new_header').append('<div id="sputtv_bar"><img id="img_onair" src="/img/skins/facelift2011/sptv_onair.png" alt="" /></div>');$('#sputtv_bar').css({"display": "block"});
					$('#new_header').append('<div id="sputtv_bar_clone"><div id="toolon"></div></div>');$('#sputtv_bar_clone').css({"display": "block"});
    			} else {
            $('#new_header').append('<div id="sputtv_bar"><img id="img_onair" src="/img/skins/facelift2011/sptv_onair.png" alt="" /><div id="toolon"></div></div>');$('#sputtv_bar').css({"display": "block"});  
            	}
            	  
            }
            }
    }
    }

};

$.fn.toolAnimateShow = function() {
    var date = new Date();
    date.setTime(date.getTime() + (60 * 60 * 1000));
    $.cookie( 'toolActive', 'on',  { expires: date, path: '/' } );
    $('#info_live h3').toolShowBar();
    $('#new_header').append('<div id="sputtv_tool"><iframe src="' + frame_path + '" width="100%" height="360" scrolling="no" frameborder="0" marginwidth="0" marginheight="0" allowtransparency="true"></iframe></div>');
    $('#new_header').stop(true, true).animate({height: '600px'},300);
    $('#sputtv_tool').stop(true, true).animate({height: '360px'},300);
    $('#new_header').append('<div id="themes_tool"></div>');
    $('#themes_tool').stop(true, true).fadeIn(500, function(){$('#themes_tool').OnAirPatchMini();});
    if ( $.browser.msie ) {
        $('#onair_mod, #onair_info').css({"display": "none"});
    } else {
        $('#onair_mod, #onair_info').stop(true, true).fadeOut(500, function(){
        	$('#onair_mod').css({"display": "none"});
        });
    }
    frame_path = frame_path_default;
};

$.fn.toolShow = function() {
    $('#info_live h3').toolShowBar();
    $('#new_header').append('<div id="sputtv_tool"><iframe src="' + frame_path + '" width="100%" height="360" scrolling="no" frameborder="0" marginwidth="0" marginheight="0" allowtransparency="true"></iframe></div>');
    $('#new_header').css({'height': '600px'});
    $('#sputtv_tool').css({'height': '360px'});
    $('#new_header').append('<div id="themes_tool"></div>');
    $('#themes_tool').stop(true, true).fadeIn(500, function(){$('#themes_tool').OnAirPatchMini();});
    $('#onair_mod, #onair_info').css({"display": "none"});
    frame_path = frame_path_default;
};

$.fn.toolAnimateClose = function() {
    // clearInterval(onairfadeMini);
    clearInterval(onairfade);
    $('#info_live h3').onAirPatch();
    $('#info_live h3').toolShowBar();
    $('#sputtv_tool, #sputtv_tool iframe').stop(true, true).animate({'height': '0'}, 300, function(){$('#sputtv_tool, #sputtv_tool iframe').remove();});
    $('#new_header').stop(true, true).animate({height: '330px'},300);
    $('#themes_tool').stop(true, true).fadeOut(500, function() {$('#themes_tool').remove()});
    
    news_loaded = "0";
    if( $.browser.msie ){
        $('#onair_mod, #onair_info').css({"display": "block"});
    }
    else {
        $('#onair_mod, #onair_info').stop(true, true).fadeIn(500);
    }
    
}

$.fn.tool = function() {
    if(stopping=="0"){
        tool_active = "yes";
            // alert('Tool on');
    if( $.cookie('toolActive') == null ) { 
            // First time visit - set cookie
        var date = new Date();
        date.setTime(date.getTime() + (60 * 60 * 1000));
        $.cookie( 'toolActive', 'on',  { expires: date, path: '/' } );
            // alert('TA Cookie set');
        
            // check for scroll position here
        
            // first check if spt is smaller than visible area for tool (195px)
            // call animated function
        if($(window).scrollTop()<195){
            $('#info_live h3').toolAnimateShow();
        }
        
            // second show reminder if scroll position is further to the bottom
        else {
                // add a click function to scroll to top and then call toolAnimateShow()
            $('body').prepend('<div id="sputtv_reminder"><img src="/img/skins/facelift2011/reminder_image.png" alt="" /></div>');
            $('#sputtv_reminder').animate({top: '20px'},300).animate({top: '10px'},50);
            $('#sputtv_reminder').live('click', function() {
                            frame_path = "/livetool_sptv/";
                            $('body').animate({scrollTop: 0}, 300, function(){$('#info_live h3').toolAnimateShow();});
                              $('#sputtv_reminder').animate({top: '20px'},50).animate({top: '-65px'},300);
                                       reached = "yes";
                              // $('#onair_mod, #onair_info').fadeOut(500);
                              });
            $(window).scroll(function () { 
                             if($(window).scrollTop()<195 && reached != "yes"){
                             $('#info_live h3').toolAnimateShow();
                             $('#sputtv_reminder').animate({top: '20px'},50).animate({top: '-65px'},300);
                             reached = "yes";
                             } 
                             });
        }
    }
    
    else {
            // recurring visit - check if and where tool was opened
            // alert('TA Cookie found');
                $('#info_live h3').toolShowBar();
        
        if($.cookie('toolActive')=="on"){
                // show the top bar for tool with buttons as per definition > button off, button popup
                // call toolShow()
                // alert('TA Cookie on');
            
            
            if($.cookie('toolPopup') == null){
                $('#info_live h3').toolShow();
                    // show tool in page
            }
            
            else {
                    // show top bar with inactive button
                $('#onair_mod, #onair_info').fadeIn(500);
            }
        }
        else {
                // show top bar for tool plus show host
                // alert('TA Cookie off');
            $('#onair_mod, #onair_info').fadeIn(500);
            
        }
    } 
        stopping="1";
        stopoff="0"
    }
   
};

$.fn.toolOff = function(){
    if(stopoff == "0"){
    tool_active = "no";
    if($.cookie('toolActive') != null){
    if($.cookie('toolActive') == "on"){
            
            // check if tool opened in popup
        if($.cookie('toolPopup')){
            $.cookie("toolPopup", null);
        }
        else {
        // hide tool in page + show host + delete cookie
            
            $('#info_live h3').toolAnimateClose();
            
            
        }
    }
     
        // hide bar
        $('#sputtv_bar').stop(true, true).fadeOut(500, function(){$('#sputtv_bar').remove();});
        if ( $.browser.msie ) {
            $('#onair_mod, #onair_info').css({"display": "block"});
        } else {
            $('#onair_mod, #onair_info').stop(true, true).fadeIn(500);
        }
        
            // $.cookie("toolActive", null);
        
        
    
    }
    else{
    if($('#sputtv_tool').length > 0){$('#info_live h3').toolAnimateClose();}
    $('#onair_mod, #onair_info').css({"display": "block"});
    }
        stopping = "0";
        stopoff = "1";
}
};

$(document).ready( function () {
                  
                  $('#toolon').live('click', function() {
                                    frame_path = "/livetool_sptv/";
                                    $('#info_live h3').toolAnimateShow();
                                    // $('#onair_mod, #onair_info').fadeOut(500);
                                    });
                  $('#tooloff').live('click', function() {
                                     var date = new Date();
                                     date.setTime(date.getTime() + (60 * 60 * 1000));
                                     $.cookie( 'toolActive', 'off',  { expires: date, path: '/' } );
                                     $('#info_live h3').toolAnimateClose();
                                     
                                     });
                  $('#toolpopup').live('click', function() {
                                       var date = new Date();
                                       date.setTime(date.getTime() + (60 * 60 * 1000));
                                       $.cookie( 'toolPopup', 'yes',  { expires: date, path: '/' } );
                                       $('#info_live h3').toolAnimateClose();
                                       var nwl = (screen.width-977)/2;
                                       var nwh = (screen.height-400)/2;
                                       sptv = window.open('/livetool_sptv/popup.php', 'sptv_live', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,width=977,height=400,left='+nwl+',top='+nwh+''); 
                                       });
                  
                  });
