/* Globals */

// Video player
var player;
var playlist;
var flashRequired = "9.0.15";
var homepageVideos = {
    streamer: "rtmp://83.142.31.91/vod/",
    file: "/DesignersVidoePlaylistXML.aspx?DesignerID=" + getQueryVariable("DesignerID") + "&%3F" + Math.random(),
    skin: "/lfwflash/videoskin_small.swf",
    controlbar: "over",
    backcolor: "333333",
    lightcolor: "FC1D82",
    frontcolor: "ffffff",
    displayclick: "none",
    bufferlength: 10,
    stretching: "uniform"
}
homepageVideosParams = { wmode: "opaque", allowScriptAccess: "always", allowFullScreen: "true" }

// Thumbnails scroller settings
var scrollSettings = {
    axis: "x",
    step: 4,
    exclude: 3,
    items: "li",
    cycle: false,
    duration: 500,
    onBefore: disableButtons
}

/* Init */

$(document).ready(function(){
						     
    // Video player

    if (swfobject.hasFlashPlayerVersion(flashRequired)) {
        swfobject.embedSWF("/lfwflash/flvplayer4.2.swf", 'vplayer', "283", "158", "9.0.15", "", homepageVideos, homepageVideosParams, { name: 'vplayer' });
    }
    else {
        $("#vplayer").html('<div class="getflash"><p>You need Adobe Flash Player (' + flashRequired + ') installed to view video on this site.</p><a href="http://get.adobe.com/flashplayer/"><img src="/images/get_flash.jpg" width="160" height="41" alt="Get Adobe Flash Player" title="" /></a>');
    }				   
						   
});

/* Video player functions */

// Callback function when the player is loaded

function playerReady(obj) {
	player = document.getElementById(obj['id']);
	
	// When the playlist is ready, load the thumbnails
    player.addControllerListener("PLAYLIST","playerDrawThumbs");
    player.addControllerListener("PLAYLIST","playetUpdateStatus");
    player.addControllerListener("ITEM","playetUpdateStatus");
}

function playerDrawThumbs(obj) {
   playlist = player.getPlaylist();
   $(".video_scroller").html("<ul></ul>");
   var thumbs = $(".video_scroller ul");
  
    // Add the thumbnails below the player
    for(var i = 0; i < playlist.length; i ++) {
        var eachThumb = $('<li id="vid' + i + '"><a href="#"><img width="84" height="48" src="' + playlist[i].thumb + '" /></a></li>');
        eachThumb
			.bind("click",playerThumbClick)
			.bind("mouseover",playerThumbHover)
			.bind("mouseout",playerThumbHover);
        thumbs.append(eachThumb);
    }
    
    // Initialise the scroller for the thumbs

    $(".video_scroller").serialScroll(scrollSettings);
    if($(".video_scroller ul").length > scrollSettings.step);
        $(scrollSettings.next).removeClass("disabled");

}

function playerThumbClick(e) {
    var item = $(this).attr("id").split("vid")[1];
    player.sendEvent("ITEM",item);
    return false;
}

function playerThumbHover (e) {
	if(e.type == 'mouseover') {
		var index = $("li",$(this).parent()).index(this);
		$(".video_title").text(playlist[index].title);
	} else {
		$(".video_title").text($(".video_title").data('title'));
	}
}

function playetUpdateStatus(obj) {
	if(obj.index) {
		var current = obj.index;
	} else {
		var current = 0;	
	}
	$(".video_title").text(playlist[current].title).data('title',playlist[current].title);
	$(".video_scroller li")
		.removeClass('current')
		.eq(current)
		.addClass('current');
}

// Function to disable the buttons on the thumbnail scroller
function disableButtons(e,elem,$pane,$items,pos) {
    // The object with the scroller's settings in it
    var conf = scrollSettings
	
    // Class the buttons appropriately
    $(conf.prev).removeClass("disabled");
    $(conf.next).removeClass("disabled");

    if( pos == 0 )
        $(conf.prev).addClass("disabled");
    else if( pos == $items.length- conf.step)
        $(conf.next).addClass("disabled");
}

function getQueryVariable(variable) {
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (var i = 0; i < vars.length; i++) {
        var pair = vars[i].split("=");
        if (pair[0].toLowerCase() == variable.toLowerCase()) {
            return pair[1];
        }
    }
} 