// Anything in here will be triggered when the page is ready (so all DIV's are present etc.)

$(document).ready(function(){
	populateElement('#keywords', 'Enter keywords');		
});


function populateElement(selector, defvalue) {
    if($.trim($(selector).val()) == "") {
        $(selector).val(defvalue);
    }
  
    $(selector).focus(function() {
        if($(selector).val() == defvalue) {
            $(selector).val("");
        }
    });
    
    $(selector).blur(function() {
        if($.trim($(selector).val()) == "") {
            $(selector).val(defvalue);
        }
    });
 }

 // Player
function changePlayerLinks(programmeid){
    // Change the player links
    link = document.getElementById('playerLinks');

    if (link != null) {
        $.get('/webservices/player-links.aspx', { id: programmeid }, function(data) {
            if (data != '') {
                $('ul#playerLinks').html(data);
            }
        }
        );
    }
}

function showProgramme(programmeid, src, whiteLabelCode, whiteLabelDir) {
    $.get('/webservices/check-programme.aspx', { id: programmeid }, function(data) {
        if (data == 'login') {
            location = '/login/?logintoview=true&id=' + programmeid;
            return;
        }
        else if (data == 'home') {
            location = '/';
            return;
        }

        displayProgramme(programmeid, src, whiteLabelCode, whiteLabelDir);
    }
    );
}

function displayProgramme(programmeid, src, whiteLabelCode, whiteLabelDir) {
    thePlayer = document.getElementById('player');

    if (thePlayer != null) {
        // Change the now watching information
        $.get('/webservices/now-watching.aspx', { id: programmeid, WhiteLabelCode: whiteLabelCode, WhiteLabelDir: whiteLabelDir }, function(data) {
            if (data != '') {
                $('div#nowWatching').html(data);
            }
        }
        );

        if (whiteLabelDir == '') {
            whiteLabelDir = 'rockhopper';
        }
        
        // The player is visible on the current page, load the specified programme
        thePlayer.innerHTML = '<input type="hidden" id="playerProgrammeID" name="playerProgrammeID" value="' + programmeid + '" />' + '\n' +
            '<object id="myId" height="320" width="500" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" style="visibility: visible;">' + '\n' +
            '<param value="/flash/mxmlVideoPlayer.swf?id=' + programmeid + '&src=' + src + '&site=' + whiteLabelDir + '" name="movie"/>' + '\n' +
            '<param value="#333333" name="BGCOLOR"/>' + '\n' +
            '<param value="true" name="allowFullScreen"/>' + '\n' +
            '<param value="transparent" name="wmode"/>' + '\n' +
            '<param value="id=' + programmeid + '&src=' + src + '&site=' + whiteLabelDir + '" name="FlashVars"/>' + '\n' +
            '<!--[if !IE]>-->' + '\n' +
            '<object height="320" width="500" allowfullscreen="true" wmode="transparent" data="/flash/mxmlVideoPlayer.swf?id=' + programmeid + '&src=' + src + '&site=' + whiteLabelDir + '" type="application/x-shockwave-flash">' + '\n' +
            '<div>' + '\n' +
            '<h1>Adobe Flash</h1>' + '\n' +
            '<p>Please download and install the latest version of Flash to view this content.</p>' + '\n' +
            '</div>' + '\n' +
            '</object>' + '\n' +
            '<!--<![endif]-->' + '\n' +
            '</object>';

        changePlayerLinks(programmeid);
        
        // Change the now playing indicator on the carousel
        nowPlayingLink = document.getElementById('programmeLink' + programmeid);

        if (nowPlayingLink != null) {
            nowPlayingHightlight = document.getElementById('nowPlayingHighlight');
            nowPlayingLink.appendChild(nowPlayingHightlight);
        }

        var downloadBtn;
        
        downloadBtn = document.getElementById('downloadButton');
        downloadBtn.style.display = "none";
        
        if (downloadBtn != null) {
            // Check if a download is available and display the button
            $.get('/webservices/download-available.aspx', { id: programmeid }, function(data) {
                if (data == '1') {
                    $.get('/webservices/download-options.aspx', { id: programmeid }, function(data) {
                        $('div#downloadOptions').html(data);
                        downloadBtn.style.display = "block";
                    }
                    );
                }
            }
            );
        }
    }
    else {
        // The player is not on the current page, go to the default programmes page
        window.location = '/programmes/' + programmeid + '/';
    }
}
 
// Play list
function addToPlaylist(programmeid) {
    $.get('/webservices/check-playlist.aspx', function(data) {
        if (data == "0") {
            createNewPlaylist(programmeid);
        }
        else {
            // Add the new item to the current playlist
            $.get('/webservices/add-to-playlist.aspx', { id: programmeid }, function(data) {
                // Reload the users playlist
                $('div#playlistContainer').html(data);

                changePlayerLinks(programmeid);
            }
            );
        }
    }
    );
}

function removeFromPlaylist(programmeid) {
    $.get('/webservices/remove-from-playlist.aspx', { id: programmeid }, function(data) {
        playList = document.getElementById('playList');

        if ((playList != null) && (data != '')) {
            // Reload the users playlist
            $('div#playlistContainer').html(data);
        }
    }
    );
}

function createNewPlaylist(programmeid) {
	thePage = document.getElementById('pageWrap');
	pageHeight = thePage.offsetHeight;
	//pageWidth = thePage.offsetWidth;
	pageWidth = document.body.clientWidth;

	theOverlay = document.getElementById('overlayHolder');
	
	theOverlay.innerHTML += '<div id="overlay" style="width:' + pageWidth + 'px; height:' + pageHeight + 'px;"></div>';

	formHtml = '<form name="playlist" id="playlist" class="playlist" action="javascript:savePlaylist(' + programmeid + ');">';
	formHtml += '<p><label for="listTitle">Enter a title for this playlist:</label> <input type="text" name="listTitle" id="listTitle" /></p>';
	formHtml += '<p class="floatLeft">You must be signed in to save a playlist</p>';
	formHtml += '<p class="floatRight"><a href="#" onclick="javascript:clearForm();"><img src="/img/btnCancel.gif" class="submit" alt="Cancel" onmouseover="this.src=\'/img/btnCancelOvr.gif\';" onmouseout="this.src=\'/img/btnCancel.gif\';" /></a><a href="#" onclick="javascript:savePlaylist(' + programmeid + ');"><img src="/img/btnSubmit.gif" class="submit" alt="Submit" onmouseover="this.src=\'/img/btnSubmitOvr.gif\';" onmouseout="this.src=\'/img/btnSubmit.gif\';" /></a></p>';
	formHtml += '</form>';

	theOverlay.innerHTML += formHtml;

	//document.getElementById('listTitle').focus();
	document.playlist.listTitle.focus();
}

function savePlaylist(programmeid) {
    // Save the new playlist
    theTitle = document.getElementById("listTitle");

    if (theTitle != null)
    {
        $.getJSON('/webservices/create-playlist.aspx?title=' + encodeURI(theTitle.value), function(data) {
            $.get('/webservices/add-to-playlist.aspx', { id: programmeid }, function(data) {
                // Reload the users playlist
                $('div#playlistContainer').html(data);

                changePlayerLinks(programmeid);
            }
            );
        }
        );
    }
    
    // Clear the form
    clearForm();
}

function clearForm() {
    var holder = document.getElementById("overlayHolder");
	var overlay = document.getElementById('overlay');
	var playlist = document.getElementById('playlist');
	
	holder.removeChild(overlay);
	holder.removeChild(playlist);
}

function deletePlaylist(playlistid) {
    $.get('/webservices/delete-playlist.aspx', { id: playlistid }, function(data) {
        if (data == "1") {
            theChannelContent = document.getElementById('channelContent');
            thePlaylist = document.getElementById('channel' + playlistid);

            if ((theChannelContent != null) && (thePlaylist != null))
                theChannelContent.removeChild(thePlaylist);
        }
    }
    );
}

// IE6 Background Hover Flicker Fix
try {
	document.execCommand('BackgroundImageCache',false,true);
	}catch(ignoreme){
}

/*
$(document).ready(function() {    

    $('#whiteLblSlide').toggle(function() {
        $("#whiteLblSlide #whiteLblSlideContent").slideUp("slow", function() {
            $("#whiteLblSlide #whiteLblSlideImage").attr("src", "/img/whitelabel/plus.gif");
        });
    }, function() {
        $("whiteLblSlide #whiteLblSlideContent").slideDown("slow", function() {
            $("#whiteLblSlide #whiteLblSlideImage").attr("src", "/img/whitelabel/minus.gif");
        });
    });

}
*/

