var playListOperationPage = FLIX_URL + "/playList_AddRemove.asp";
var playListXmlStr = "";
var addToPlayListImg = FLIX_URL + "/images/Icons/plus_icon_blue.gif";
var addToPlayListImg_Over = FLIX_URL + "/images/Icons/plus_icon_orange.gif";
var addToPlayListImg_Click = FLIX_URL + "/images/Icons/plus_icon_grey.gif";
var quickList = new Array();
var quickList_count = 0;
var playingMediaId = 0;

function getIndexOf(quickList, mediaId) {
    for (i = 0; i < quickList.length; i++) {
        if (quickList[i] == mediaId)
            return i;
    }
    return -1;
}

function addToQuickList_onMouseOver(mediaId, relId) {
    var addToQuickList_Id = 'addToQuickList_' + mediaId;
    if (relId && relId != "") addToQuickList_Id += '_' + relId
    if (document.getElementById(addToQuickList_Id).src.match('grey'))
        document.getElementById(addToQuickList_Id).src = addToPlayListImg_Click;
    else
        document.getElementById(addToQuickList_Id).src = addToPlayListImg_Over;
}

function addToQuickList_onMouseOut(mediaId, relId) {
    var addToQuickList_Id = 'addToQuickList_' + mediaId;
    if (relId && relId != "") addToQuickList_Id += '_' + relId
    if (document.getElementById(addToQuickList_Id).src.match('grey'))
        document.getElementById(addToQuickList_Id).src = addToPlayListImg_Click;
    else
        document.getElementById(addToQuickList_Id).src = addToPlayListImg;
}

function addToQuickList_changeIconEnd(mediaId, relId) {
    var addToQuickList_Id = 'addToQuickList_' + mediaId;
    if (relId && relId != "") addToQuickList_Id += '_' + relId
    if (document.getElementById(addToQuickList_Id)) {
        document.getElementById(addToQuickList_Id).src = addToPlayListImg_Click;
        document.getElementById(addToQuickList_Id).blur();
    }
}

function addToQuickList_onClick(mediaId, relId) {
    if (addToQuickList(mediaId)) {
        if (quickList.length == 1) {
            document.getElementById('myQuickList').style.display = '';
            document.getElementById('how_many_flixs').innerHTML = 'סרטון';
        }
        else {
            document.getElementById('how_many_flixs').innerHTML = 'סרטונים';
        }

        document.getElementById('play_all_numb').innerHTML = quickList_count + '&nbsp;';
        if (document.getElementById('show_all_video_number'))
            document.getElementById('show_all_video_number').innerHTML = quickList_count

        if (!relId) relId = ""
        addToQuickList_changeIconEnd(mediaId, relId)

        return true;
    }
    return false;
}

function removeFromQuickList_onClick(mediaId) {
    if (removeFromQuickList(mediaId)) {
        var row_id = "vid_row_" + mediaId;
        var row_space_id = "vid_space_row_" + mediaId;
        //keep track of video numbers that display in the left column and reorder the numbers
        var tbody = document.getElementById("playList_table").tBodies[0];
        var total_rows = tbody.rows.length - 1;
        var current_row = tbody.rows[row_id].cells[0].innerHTML;
        var current_row_in_table = tbody.rows[row_id].rowIndex - 1;
        for (i = total_rows - 1; i > current_row_in_table; i = i - 2) {
            tbody.rows[i].cells[0].innerHTML = tbody.rows[i].cells[0].innerHTML - 1;
        }
        tbody.removeChild(document.getElementById(row_id));
        tbody.removeChild(document.getElementById(row_space_id));

        document.getElementById('play_all_numb').innerHTML = quickList_count + " ";
        if (document.getElementById('show_all_video_number'))
            document.getElementById('show_all_video_number').innerHTML = quickList_count

        var addToQuickList_Id = 'addToQuickList_' + mediaId;
        if (document.getElementById(addToQuickList_Id))
            document.getElementById(addToQuickList_Id).src = addToPlayListImg;

        if (quickList.length == 0)
            document.getElementById('myQuickList').style.display = 'none';
        else if (quickList.length == 1)
            document.getElementById('how_many_flixs').innerHTML = 'סרטון';
        else
            document.getElementById('how_many_flixs').innerHTML = 'סרטונים';

        return true;
    }
    return false;
}

function addToQuickList(mediaId) {
    var mediaIndex = getIndexOf(quickList, mediaId);
    if (mediaIndex < 0) {
        quickList.push(mediaId);
        quickList_count++;
        updatePlayListCk(1, mediaId);
        return true;
    }
    return false;
}

function removeFromQuickList(mediaId) {
    var mediaIndex = getIndexOf(quickList, mediaId);
    if (mediaIndex >= 0) {
        quickList.splice(mediaIndex, 1)
        quickList_count--;
        updatePlayListCk(2, mediaId);
        return true;
    }
    return false;
}

function drawPlayList_Video(pVideo_MediaId, pVideo_Title, pVideo_Link, pVideo_UserId, pVideo_UserName, pVideo_Pic, pVideo_Runtime, pVideo_ViewTimes) {
    var nextVideoNumber = quickList_count;
    var temp_pVideo_Title = unescape(pVideo_Title);
    var div_id_1 = "1_" + pVideo_MediaId;
    var div_id_2 = "2_" + pVideo_MediaId;
    var div_id_3 = "3_" + pVideo_MediaId;
    var div_id_4 = "4_" + pVideo_MediaId;
    var div_id_5 = "5_" + pVideo_MediaId;

    var div_content_1 = nextVideoNumber;
    var div_content_2 = '<a href="' + pVideo_Link + '"><img src="' + pVideo_Pic + '" style="padding:2px;width:89px;height:50px;border:0px;vertical-align:middle;"></a>';
    var div_content_3 = '<div dir="rtl"><a href="' + pVideo_Link + '" class="blueFlixText"><b>' + temp_pVideo_Title + '</b></a></div>' +
	                    '<div dir="rtl"><img align="baseline" src="/images/IconDarkBlue_User.gif">  <a href="/myPage.asp?id=' + pVideo_UserId + '" class="blueFlixText">' + pVideo_UserName + '</a></div>' +
	                    '<div dir="rtl"><span class="grayText"><b>צפיות:</b> ' + pVideo_ViewTimes + '</span></div>';
    var div_content_4 = '<span class="grayText"><b>זמן: </b>' + pVideo_Runtime + '</span>';
    var div_content_5 = '<a onClick="removeFromQuickList_onClick(\'' + pVideo_MediaId + '\');return false;" style="cursor:pointer;" title="הסר מהפלייליסט"><img src="/images/Icons/playList_delete.gif" border="0" /></a>';

    var tbody = document.getElementById("playList_table").tBodies[0];
    var row = document.createElement("TR");
    row.setAttribute("id", "vid_row_" + pVideo_MediaId);
    row.setAttribute("width", "434");
    row.setAttribute("bgcolor", "#F9F9F9");
    row.setAttribute("bgColor", "#F9F9F9");
    row.setAttribute("className", "grayText");

    var cell1 = document.createElement("TD");
    cell1.setAttribute("id", "1_" + pVideo_MediaId);
    cell1.setAttribute("align", "center");
    cell1.setAttribute("valign", "middle");
    cell1.setAttribute("width", "16");
    cell1.setAttribute("height", "49");
    cell1.setAttribute("class", "grayText");

    var cell2 = document.createElement("TD");
    var tempDiv_2 = document.createElement("DIV");
    tempDiv_2.setAttribute("id", "2_" + pVideo_MediaId);
    tempDiv_2.setAttribute("align", "right");
    tempDiv_2.setAttribute("valign", "middle");
    tempDiv_2.setAttribute("width", "64");

    var cell3 = document.createElement("TD");
    var tempDiv_3 = document.createElement("DIV");
    tempDiv_3.setAttribute("id", "3_" + pVideo_MediaId);
    tempDiv_3.setAttribute("align", "right");
    tempDiv_3.setAttribute("width", "254");

    var cell4 = document.createElement("TD");
    cell4.setAttribute("align", "center");
    var tempDiv_4 = document.createElement("DIV");
    tempDiv_4.setAttribute("id", "4_" + pVideo_MediaId);
    tempDiv_4.setAttribute("width", "70");

    var cell5 = document.createElement("TD");
    cell5.setAttribute("align", "center");
    var tempDiv_5 = document.createElement("DIV");
    tempDiv_5.setAttribute("id", "5_" + pVideo_MediaId);
    tempDiv_5.setAttribute("width", "40");

    cell2.appendChild(tempDiv_2);
    cell3.appendChild(tempDiv_3);
    cell4.appendChild(tempDiv_4);
    cell5.appendChild(tempDiv_5);

    row.appendChild(cell1);
    row.appendChild(cell2);
    row.appendChild(cell3);
    row.appendChild(cell4);
    row.appendChild(cell5);

    tbody.appendChild(row);

    var row_space = document.createElement("TR");
    row_space.setAttribute("id", "vid_space_row_" + pVideo_MediaId);
    row_space.setAttribute("width", "434");
    row_space.setAttribute("height", "4");

    var cell_space = document.createElement("TD");
    cell_space.setAttribute("width", "16");
    cell_space.setAttribute("height", "4");

    row_space.appendChild(cell_space);
    tbody.appendChild(row_space);

    if (document.getElementById('now_playing_end')) {
        if (document.getElementById('playList_table').rows.length > 10) {
            setTimeout("jumpToNowPlaying(1);", 200);
        }
        else {
            if (quickList_count > 5)
                jumpToNowPlaying(1);
        }
    }

    document.getElementById(div_id_1).innerHTML = div_content_1;
    document.getElementById(div_id_2).innerHTML = div_content_2;
    document.getElementById(div_id_3).innerHTML = div_content_3;
    document.getElementById(div_id_4).innerHTML = div_content_4;
    document.getElementById(div_id_5).innerHTML = div_content_5;

}

function clearQuickList()
{
    if (confirm('האם את/\ה בטוח/\ה שברצונך להסיר את כל הפליקסים מהרשימה?'))
	{
	    var mytable = document.getElementById('playList_table');
	    myr = mytable.getElementsByTagName('tr');
	    for (var u = 0; u <= myr.length; u++)
	    {

	        document.getElementById('playList_table').deleteRow(u)
	    } 

        var my_QuickList_div = document.getElementById('myQuickList');
        if (my_QuickList_div)
            document.getElementById('myQuickList').style.display = 'none';

        quickList_count = 0;
        quickList.splice(0, quickList.length);
        updatePlayListCk(3, 0)
        document.getElementById('play_all_numb').innerHTML = quickList_count + " ";
        return false;
    }
}

function hideShowPlayList_onClick() {
    if (document.getElementById('hideShowPlayList').src.match('Down')) {
        document.getElementById('hideShowPlayList').src = '/images/Buttons/playList_Up_button.gif';
        document.getElementById('playList_container').style.display = '';
        document.getElementById('playList_control').style.display = '';
    }
    else {
        document.getElementById('hideShowPlayList').src = '/images/Buttons/playList_Down_button.gif';
        document.getElementById('playList_container').style.display = 'none';
        document.getElementById('playList_control').style.display = 'none';
    }
}

function showAllPlayListVideos(mediaId) {
    document.getElementById('playList_showAll').innerHTML = '<span class="blueFlixText">טוען רשימה...</span>';
    var url = FLIX_URL + "/getQuickList.asp?currentMedia=" + mediaId
    //var url = 'http://flix.tapuz.co.il/getQuickList.asp?currentMedia=' + mediaId	
    getUrlXMLResponse(url, postShowAllPlayListVideos);
}
function postShowAllPlayListVideos(xmlHttpReq) {
    var root_node = getRootNode(xmlHttpReq);
    var html_content_code = getNodeValue(root_node, 'html_content');
    document.getElementById('playList_container').innerHTML = html_content_code;
    document.getElementById('playList_showAll').innerHTML = '';
    jumpToNowPlaying();
}

function jumpToNowPlaying(endOfList) {
    if (navigator.appName == 'Microsoft Internet Explorer')
        pixelsFromTop = document.documentElement.scrollTop;
    else
        pixelsFromTop = window.pageYOffset;

    if (navigator.userAgent.indexOf('Safari') == -1) {
        if (endOfList == 1) {
            if (document.getElementById('now_playing_end'))
                location.href = '#now_playing_end';
        }
        else {
            if (document.getElementById('now_playing'))
                location.href = '#now_playing';
        }
        //window.scrollTo(0,pixelsFromTop);
    }
}

function updatePlayListCk(typeId, mediaId) {
    playListXmlStr = "<?xml version='1.0' encoding='utf-8' ?>" +
			"<root>" +
				"<typeId>" + typeId + "</typeId>" +
				"<mediaId>" + mediaId + "</mediaId>" +
			"</root>"
    sendPlayListXML(typeId, mediaId);
}

function sendPlayListXML(typeId, mediaId) {
    var xmlObj;
    if (!document.all) {
        xmlObj = new XMLHttpRequest();
        xmlObj.open("post", playListOperationPage, false);
    }
    else {
        xmlObj = new ActiveXObject("Msxml2.XMLHTTP")
        xmlObj.open("post", playListOperationPage, false);
    }


    xmlObj.onreadystatechange = function() {
        if (xmlObj.readyState == 4) {
            try {
                responseTxt = xmlObj.responseText;
            }
            catch (ex) { }
        }
    }
    xmlObj.send(playListXmlStr);
    playListXmlStr = "";
}
