var sVideoType = "flash";
var sContentPlayerURL;
var aCookie = null;	
var bOpened = true ;
//var sCurrentGuid ;
//var iCurrentContentId ;


$(document).ready(function(){
	$('.header').pngFix();
	$('#headerBottom').pngFix();	
	$("#btnMediaPlayer").show() ;
	$("#btnFlashPlayer").hide() ;
		lait.initHome() ;
		//lait.oIntCounter = setInterval("lait.getCurrentBroadcast()", 10000) 
});

function setCurrentItem()
{
	var url = "/site/GetAjaxContent.html?Template=WebTV\\ContentListBroadcast" ;
	$.get(url, function(Xml) {
		var Control_node = Xml.getElementsByTagName('Channel')[0] ;
		var ContentList_node = Xml.getElementsByTagName('ContentList')[0] ;
		var iCurrentItem = parseInt(Control_node.getAttribute('currentplaylistentry')) ;
		var sGuid = Control_node.getAttribute('currentguid') ;
		var iContentId = parseInt(Control_node.getAttribute('currentid')) ;
		lait.getExtraInfo(sGuid);
		lait.getRelatedContent(iContentId) ;
	} );
}

function MakeRequest(template, objDom, callback) 
{
	var url = "/site/GetAjaxContent.html?template=" + template ;
	
	$(objDom).html("") ;
	$(objDom).html("<div class='loading'><img src='/img/loadingAnimation.gif' /></div>") ;
	$.ajax({
		type: "GET",
		url: url,
		dataType: "xml",
		success: function(Xml) 
		{
		
			if (objDom == "#mediaplayer")
			{
				var oPlayer = document.getElementById('mediaplayer') ;
				oPlayer.innerHTML = $("ContentHTML",Xml).text() ;
				 
			}
			else 
			{
				$(objDom).html($("ContentHTML",Xml).text()) ;
				 
			}
				
		}
	});

	/*try{ if(callback) callback() ; }
	catch(e){}*/
	 if(callback) callback ;
}

function SetVideoType(sType)
{
	sVideoType = sType;
	var url = sContentPlayerURL;
	//url += sContentPlayerURL;
	
	url += "&videotype=" + sVideoType;
	if (sType == "flash")
		$("#divFullscreen").css('visibility', 'hidden');
		
	else
		$("#divFullscreen").css('visibility', 'visible');
	
	try 
	{
		var oPlayer = document.getElementById('mediaplayer') ;
		oPlayer.innerHTML = "" ;
	}
	catch (e) {}
	MakeRequest(url,'#mediaplayer', null) ;
	//alert(url) ;
	document.cookie = "WebTV:VideoType=" + sType;
	//document.location.href=url
}

var lait = {
    channelRequest: 0,
    contentRequest: "",
    contentGuidRequest: "",
    channelNameRequest: "",
    sCurrentGuid: null,
    iCurrentContentId: 0,
    oIntCounter: 0,
    icounterComment: 0,
    sCurrentCulture: "",
    sLabelCloseComment: "",
    sLabelAlertLogin: "",

    initHome: function() {
        //		lait.initScroller();
        lait.getOnAir();
        lait.getPrimoPiano();
        lait.getChannels();

        var sFindGuid = window.location.search.split("guid=");
        var sFindContentID = window.location.search.split("&contentid=");
        var sGuidFinded = "";
        sGuidFinded = sFindGuid[1];
        var sContentIDFinded = sFindContentID[1];

        lait.setCultureCode();
        lait.getVideoDelGiorno(sGuidFinded);
        if (sFindContentID != "") {
            lait.getVideoOnDemand(sGuidFinded, sContentIDFinded)
            if (sContentIDFinded != null) $(".webTV .videoContent h2").text("Video degli utenti");
            else $(".webTV .videoContent h2").text("Video del giorno");


        }
        else {

            if (sGuidFinded != null) {
                //alert(sGuidFinded);
                lait.getVideoDelGiorno(sGuidFinded);

            }
        }
    },

    deletePost: function(ipostid, icontentid) {
        if (confirm('Sei sicuro ?')) {
            $.post("/site/GetAjaxContent.html?Template=WebTV\\BlogActions", { postid: ipostid, contentid: icontentid });
            lait.LoadBlog('infoVideo', lait.iCurrentContentId, 0);
            //alert("ContentId: " + contentid + " PostId: " + postid);
        }
        else return false;
    },

    setCultureCode: function() {
        var sSplitString = window.location.pathname.split("/");
        lait.sCurrentCulture = sSplitString[2];
        switch (lait.sCurrentCulture) {
            case "it-IT":
                lait.sLabelCloseComment = "> Visualizza / nascondi commenti";
                $(".commenti .close a").text(lait.sLabelCloseComment);
                lait.sLabelAlertLogin = "Devi effettuare il login per inserire un post";
                break;
            case "en-GB":
                lait.sLabelCloseComment = "> View / hide Comment";
                $(".commenti .close a").text(lait.sLabelCloseComment);
                lait.sLabelAlertLogin = "You have to login to add a post";
            default:
                break;

        }

    },
    getOnAir: function() {

        var url = "WebTV\\ContentPlayer&WebTVSelectedChannelID=306";
        sContentPlayerURL = url;
        url += "&videotype=" + sVideoType;
        MakeRequest(url, "#mediaplayer");
    },
    getPrimoPiano: function(objDom) {

        MakeRequest("WebTV\\ContentList&WebTVSelectedChannelID=307", ".primopiano .bodyChannel");
        $("#primopiano_id").addClass("active");
        $("#visti_id").removeClass();
    },
    getContentChannel: function(channelid, dom) {
        var objDom = dom;
        $(".contentChannelList").attr("style", "height:0px;dispay:none;line-height:0; font-size:0;").html("");
        $("#" + channelid + ".contentChannelList").attr("style", "height:120px;").html("");

        MakeRequest("WebTV\\ChannelContentList&WebTVSelectedChannelID=" + channelid, $('#' + channelid), $("#" + channelid + ".contentChannelList").fadeIn(500));


    },
    getMostVisited: function() {
        MakeRequest("WebTV\\MostVisited", ".primopiano .bodyChannel");
        $("#visti_id").addClass("active");
        $("#primopiano_id").removeClass();

    },
    getChannels: function() {
        MakeRequest("WebTV\\ChannelList&WebTVParentChannelID=308", ".canali");

    },
    getVideoOnDemand: function(guid, contentid) {
        window.clearInterval(lait.oIntCounter);
        var url = "WebTV\\ContentPlayer&WebTVContentID=" + guid + "&WebTVOnDemandChannelID=0"; //+ contentid ;
        lait.iCurrentContentId = contentid;
        lait.LoadBlog('pane', lait.iCurrentContentId, 0);
        sContentPlayerURL = url;
        url += "&videotype=" + sVideoType;

        MakeRequest(url, "#mediaplayer");
    },
    getVideoDelGiorno: function(guid) {
        //window.clearInterval(lait.oIntCounter);

        var sFind = window.location.search.split("=");
        var sGuid = sFind[1];

        var url = "WebTV\\ContentPlayer&WebTVContentID=" + guid + "&WebTVOnDemandChannelID=" + 319;

        sContentPlayerURL = url;
        url += "&videotype=" + sVideoType;
        MakeRequest(url, "#mediaplayer");
        $(".webTV .videoContent h2").text("Video del giorno");
        if (guid != null) {
            lait.getExtraInfo(guid)
        }
    },
    getExtraInfo: function(guid) {
        var url = "WebTV\\ExtraInfo&WebTVContentID=" + guid;
        MakeRequest(url, ".descrizioneVideo");
    },
    getRelatedContent: function(contentid) {
        var url = "WebTV\\RelatedContent&WebTVContentID=" + contentid;
        MakeRequest(url, ".videoCorrelati .bodyChannel", lait.initScroller);
    },
    initScroller: function() {
        //$('.scrollpane').jScrollPane({showArrows:true,scrollbarWidth: 12});

    },
    getCurrentBroadcast: function() {

        var url = "/site/GetAjaxContent.html?template=WebTV\\ContentListBroadcast&WebTVBroadcastAction=currentplaylistentry";

        $.get(url, function(Xml) {
            var Control_node = Xml.getElementsByTagName('Channel')[0];
            var ContentList_node = Xml.getElementsByTagName('ContentList')[0];
            var iCurrentItem = parseInt(Control_node.getAttribute('currentplaylistentry'));
            var sGuid = Control_node.getAttribute('currentguid');
            var iContentId = parseInt(Control_node.getAttribute('currentid'));
            if (iContentId != lait.iCurrentContentId) {

                lait.getExtraInfo(sGuid);
                lait.getRelatedContent(iContentId);
                lait.sCurrentGuid = sGuid;
                lait.iCurrentContentId = iContentId;
                //lait.iCurrentContentId =  1108;
                //lait.sCurrentGuid = "8eb22534e9e34922b1b96df17525906f" ;
                //alert(lait.iCurrentContentId) ;
                //lait.LoadBlog('contentPane',lait.iCurrentContentId,0);
            }
        });
    },

    slideElement: function(elemetToSlide) {

        if (bOpened) {
            $(elemetToSlide).fadeOut("fast", function() {
                bOpened = false;
            });
        }
        else {
            $(elemetToSlide).fadeIn("fast", function() {
                bOpened = true;
            });
        }
    },
    LoadBlog: function(objdom, contentid, parentid) {
        //alert("") ;
        lait.icounterComment = 0;
        if (contentid == undefined)
            contentid = lait.iCurrentContentId;
        //alert(lait.iCurrentContentId);
        MakeRequest("WebTV\\Blog&contentid=" + contentid + "&parentid=" + parentid, ".contentPane");

        $(".contentPane").fadeIn();
        bOpened = true;
        $(".commenti .close").show();

    },
    GetForm: function(objdom) {
        $("div.postComment").hide();

        $(objdom).parent().parent().find("div.postComment").show("fast", function() {
            $('#pane').jScrollPane({ showArrows: true, scrollbarWidth: 12 });
        });

        return false;

    },
    CloseForm: function(objdom) {
        $('#pane').jScrollPane({ showArrows: true, scrollbarWidth: 12 });
        $(objdom).parent().parent().find("div.postComment").hide("fast", function() {
            $('#pane').jScrollPane({ showArrows: true, scrollbarWidth: 12 });
        });
        return false;

    },
    PostComment: function(ipostid, iauthuserid, icontentid) {
        var sText = $('#text_' + ipostid).val();
        if (iauthuserid != 0) {
            if (icontentid == 0) {
                icontentid = lait.iCurrentContentId;
                //alert(lait.iCurrentContentId);
            }
            //alert(ipostid + " " + iauthuserid + " " + icontentid);	
            if (sText != "") {
                var url = "/site/GetAjaxContent.html?Template=WebTV\\Blog";
                $.post(url, { message: sText, userid: iauthuserid, contentid: icontentid, parentid: ipostid }, function(Xml) {
                    lait.LoadBlog('infoVideo', lait.iCurrentContentId, 0);
                }, "xml");

            }
        }
        else {
            alert("Ti devi loggare per inserire un commento !");
        }
    },
    countComment: function() {

        $(".counterClass").each(function(e) {
            lait.icounterComment = e + 1;
        });
        if (lait.icounterComment != 0) $(".addComment span").html(lait.icounterComment);


    }
}
