/**
 * Video Utilities JS 
 * Various functions for rendering video on pages.  Also contains specific video channel functions.
 * @package agdistis
 */
 
var vpjs_feed_api_url = 'video_feed.php'; 
var vpjs_promotional_column_api_url = 'video_promotional.php'; 
var vpjs_video_splash_url = 'video_splash.php'; 
var vpjs_3rdparty_api_url = '3rdparty_api.php';
 
// Contains a mapping of vendor names to ids
var VPL_VENDOR_ID_MAP = new Object();

// Contains a mapping of vendor ids to names
var VPL_VENDOR_NAME_MAP = new Object();

// Contains a mapping of vendor ids to names
var VPL_VENDOR_DISPLAY_NAME_MAP = new Object();

// Contains a mapping of vendor ids to sort type
var VPL_VENDOR_SORT_TYPE = new Object();

// Contains a mapping of vendor ids to sort dir
var VPL_VENDOR_SORT_DIR = new Object();

// Indexed by vtp_container ids, contains information for rendering partner logo divs.  Must be an array.
var VPL_VENDOR_LOGOS = new Array();

var vpjs_visible_channels = 5;
var channel_offset = 0;

var vpjs_selected_class = "vich_channel vich_channel_hover";
var vpjs_hover_class = "vich_channel vich_channel_hover";
var vpjs_off_class = "vich_channel";

var vpjs_max_per_page = 10;
var vpjs_page_jump_max = 10;

var vpjs_videosplash_override = true;

var vpjs_loaded_channel_images = new Array();

var load_handler = 'vpjs_vich_listings_loading';




// .o000 Generic Functions 000o. \\
 
/**
 * Grabs the text from the search input field and informs the Shepherd.
 *
 * @param {String} target
 */
function vpjs_search_helper(target)
{
	if (target)
	{
		var id = document.getElementById(target);
		if (id && id.value)
		{
			value = id.value;
			value = value.replace(/[^\w\s-]/g, '');
		}
	}

	if (value)
	{
		VPJS_SHEPHERD.set_state('vpjs_browse_ids', null);
		VPJS_SHEPHERD.set_state('vpjs_search_terms', value);
		VPJS_SHEPHERD.set_state('vpjs_user_sort', null);
		vpjs_update_tabs();
	}
}



/**
 * Informs the Shepherd of new vendor_id and category_id values.
 *
 * @param {int} vendor_id
 * @param {int} category_id
 */
function vpjs_browse_helper(vendor_id, category_id)
{
	VPJS_SHEPHERD.set_state('vpjs_search_terms', null);
	VPJS_SHEPHERD.set_state('vpjs_browse_ids', vendor_id + '|' + category_id);
	if (!VPJS_SHEPHERD.get_state('vpjs_user_sort'))
	{
		VPJS_SHEPHERD.set_state('vpjs_user_sort', 'DATE|DESC');
	}
}



/**
 * Informs the Shepherd of the new sorting type.
 * 
 * @param {String} sort_type
 */
function vpjs_sort_helper(sort_type)
{
	var direction = '';
	var old_user_sort_type = '';
	var old_user_sort_dir = '';

	if (VPJS_SHEPHERD.get_state('vpjs_user_sort'))
	{
		var old_user_sort = VPJS_SHEPHERD.get_state('vpjs_user_sort').split('|');
		old_user_sort_type = old_user_sort[0];
		old_user_sort_dir = old_user_sort[1];
	}
	else
	{
		old_user_sort_type = 'DATE';
		old_user_sort_dir = 'ASC';
	}

	if (old_user_sort_type.toUpperCase() == sort_type.toUpperCase())
	{
		direction = old_user_sort_dir.toUpperCase() == 'ASC' ? 'DESC' : 'ASC';
	}
	else
	{
		direction = sort_type.toUpperCase() == 'DATE' ? 'DESC' : 'ASC';	
	}
	VPJS_SHEPHERD.set_state('vpjs_user_sort', sort_type + '|' + direction);
	if (VPJS_SHEPHERD.get_state('vpjs_search_terms'))
	{
		vpjs_render_video_feed("vich_listings", "search", 1);
	}
	else
	{
		vpjs_render_video_feed("vich_listings", "browse", 1);
	}
}



/**
 * Update the sorting tab styles to reflect how the videos are being sorted.
 */
function vpjs_update_tabs()
{
	var sort = VPJS_SHEPHERD.get_state('vpjs_user_sort')
	if (sort)
	{
		sort = sort.split('|');
		var sort_type = sort[0];
		var sort_dir = sort[1]; 

		document.getElementById('vich_sort_newest').className = sort_type == 'DATE' ? 'current_tab' : 'linked_tab';
		document.getElementById('vich_sort_alpha').className = sort_type == 'ALPHA' ? 'current_tab' : 'linked_tab';

		if (sort_type == 'DATE' && sort_dir == 'ASC')
		{
			document.getElementById('vich_sort_newest').innerHTML = 'Oldest first';
		}
		else
		{
			document.getElementById('vich_sort_newest').innerHTML = 'Newest first';
		}
	}
	else
	{ 
		document.getElementById('vich_sort_newest').className = 'linked_tab';
		document.getElementById('vich_sort_alpha').className = 'linked_tab';
		document.getElementById('vich_sort_newest').innerHTML = 'Newest first';
	}
}



/**
 * Retrieves a list of current vendor_ids and category_ids.
 *
 * @return {Array}
 */
function vpjs_get_browse_ids()
{
	if (!VPJS_SHEPHERD || !VPJS_SHEPHERD.is_a_shepherd)
	{
		return new Array();
	}
	
	var browse_ids = VPJS_SHEPHERD.get_state('vpjs_browse_ids');
	browse_ids = browse_ids ? browse_ids : '|';
	return browse_ids.split('|');	
}



/**
 * Retrieves a list of currently selected vendor_ids.
 *
 * @return {Array}
 */
function vpjs_get_vendor_ids()
{
	var browse_ids = vpjs_get_browse_ids();	
	var ids = browse_ids[0] ? browse_ids[0].split(',') : new Array();
	for (var i in ids)
	{
		ids[i] = isNaN(ids[i]) ? 0 : parseInt(ids[i])
	}
	return ids;
}



/**
 * Retrieves a list of all the vendor ids.
 *
 * @return {Array}
 */
function vpjs_get_all_vendor_ids()
{
	var ids = new Array();
	for (var i in VPL_VENDOR_ID_MAP)
	{
		ids.push(VPL_VENDOR_ID_MAP[i]);
	}

	return ids;
}



/**
 * Retrieves a list of the currently selected categories.
 *
 * @return {Array}
 */
function vpjs_get_category_ids()
{
	var browse_ids = vpjs_get_browse_ids();	
	var ids = browse_ids[1] ? browse_ids[1].split(',') : new Array();
	for (var i in ids)
	{
		ids[i] = isNaN(ids[i]) ? 0 : parseInt(ids[i])
	}
	return ids;
}



/**
 * Retrieves the current sorting type.
 *
 * @return {String}
 */
function vpjs_get_user_sort()
{
	if (!VPJS_SHEPHERD || !VPJS_SHEPHERD.is_a_shepherd())
	{
		return '';
	}

	return VPJS_SHEPHERD.get_state('vpjs_user_sort');
}



/**
 * Returns the current search string.
 *
 * @return {String}
 */
function vpjs_get_user_search()
{
	var search_terms = VPJS_SHEPHERD.get_state('vpjs_search_terms');
	if (search_terms)
	{
		var search_terms = vpjs_trim(search_terms);
		return search_terms ? search_terms.replace(/\s+/g, ',') : '';
	}
	else
	{
		return '';
	}
}



/**
 * Retrieves a vendor name from a vendor id.
 *
 * @param {int} vendor_id
 * @return {String}
 */
function vpjs_get_vendor_name(vendor_id)
{
	vendor_id = isNaN(vendor_id) ? 0 : parseInt(vendor_id)
	if (VPL_VENDOR_NAME_MAP && vendor_id && VPL_VENDOR_NAME_MAP[vendor_id])
	{
		return VPL_VENDOR_NAME_MAP[vendor_id];
	}
	return null;
}



/**
 * Retrieves a vendor id from a vendor name.
 *
 * @param {String} vendor_name
 * @return {int}
 */
function vpjs_get_vendor_id(vendor_name)
{
	if (VPL_VENDOR_ID_MAP && vendor_name && VPL_VENDOR_ID_MAP[vendor_name])
	{
		return isNaN(VPL_VENDOR_ID_MAP[vendor_name]) ? 0 : parseInt(VPL_VENDOR_ID_MAP[vendor_name]);
	}
	return 0;
}



/**
 * Starts the process of rendering a video listing.
 *
 * @param {String} target
 * @param {String} action
 * @param {int} page
 */
function vpjs_render_video_feed(target, action, page)
{
	if (!VPJS_SHEPHERD || !VPJS_SHEPHERD.is_a_shepherd)
	{
		return;
	}
	
	// To clear out feed params, be sure to implicitly send empty ones.
	var params = '';
	params['default_vids'] = '';
	params['vendor_ids'] = '';
	params['category_ids'] = '';
	params['search_terms'] = '';

	switch (action)
	{
		case 'browse':
			params += 'default_vids=';
			params += '&vendor_ids=' + escape(vpjs_get_vendor_ids().join(','));
			params += '&category_ids=' + escape(vpjs_get_category_ids().join(','));
			params += '&search_terms=';
			params += '&user_sort=' + escape(vpjs_get_user_sort());
			break
		
		case 'search':
			var user_sort = vpjs_get_user_sort() ? vpjs_get_user_sort() : '';
			params += 'default_vids=';
			params += '&vendor_ids=';
			params += '&category_ids=';
			params += '&search_terms=' + escape(vpjs_get_user_search());
			params += '&user_sort=' + escape(user_sort);
			break
		
		case 'videos':
			var video_ids = VPJS_SHEPHERD.get_state('vpjs_video_ids');
			params += 'default_vids=' + escape(video_ids ? video_ids : '');
			params += '&vendor_ids=';
			params += '&category_ids=';
			params += '&search_terms=';
			params += '&user_sort=';
			break
		
		default:
			params += 'default_vids=';
			params += '&vendor_ids=';
			params += '&category_ids=';
			params += '&search_terms=';
			params += '&user_sort=';
			break

		// End switch
	}

	
	// Sanitize page parameter
	page = isNaN(page) ? 1 : parseInt(page);
	page = (page < 1) ? 1 : page;

	// The data object is passed between each stage
	var data = new Object;
	data['target'] = target;
	data['action'] = action;
	data['page_jump'] = true; 
	data['page'] = page;
	data['max_per_page'] = vpjs_max_per_page;

	if (load_handler && window[load_handler])
	{
		window[load_handler](target);
		document.getElementById('vich_pagination').innerHTML = '';
	}

	var start = (page - 1) * data['max_per_page'];

	new SynAjax().request(vpjs_feed_api_url, {
			data: data,
			method: 'get',
			onComplete: vpjs_render_video_feed_complete,
			parameters: 'start=' + start + '&limit=' + data['max_per_page'] + '&' + params
		}
	); 
} 



/**
 * Parses the data and populates the video listing.
 *
 * @param {Resource} request
 */
function vpjs_render_video_feed_complete(request)
{
	var data = this.data;
	var response = request.responseText;
	var feed_count = parseFloat(response.substr(0,6));
	var video_listing = response.substr(6);

	var limit = data['max_per_page'];
	var pages = Math.ceil(feed_count / limit);
	
	pages = pages ? pages : 1;
	data['pages'] = pages;

	var page_jump = true;
	
	// Dont display jump if there is only one page
	if (pages < 2)
	{
		page_jump = false;
	}
	// Make sure page is within upper bounds
	if (data['page'] > pages)
	{
		data['page'] = pages;
	}
	
	var id = document.getElementById(data['target']);
	id.innerHTML = video_listing;

	var pagination = document.getElementById('vich_pagination');
	if (page_jump == true)
	{
		pagination.innerHTML = vpjs_render_pagination(data);
	}
	else
	{
		pagination.innerHTML = '';
	} 
}



/**
 * Retrieves a page from the video listings.
 *
 * @param {String} target
 * @return {int}
 */
function vpjs_goto_value(target)
{
	var id = document.getElementById(target + '_goto');
	if (!id)
	{
		return 1;
	}
	
	return isNaN(id.value) ? 1 : parseInt(id.value);
}



/**
 * Generate the pagination for the video listings.
 *
 * @param {Object} data
 * @return {String}
 */
function vpjs_render_pagination(data)
{
	// Pagination maths
	var midpoint = Math.floor(vpjs_page_jump_max / 2);
	var end = data['page'] + midpoint;
	var start = data['page'] - midpoint;
	start = (vpjs_page_jump_max % 2) ? start : start + 1;

	if (start < 1)
	{
		end -= start;
		if (data['page'] <= midpoint)
		{
			end++;
		}
		start = 1;
	}

	if (end > data['pages'])
	{
		var edge = end - data['pages'];
		while (edge && (start > 1))
		{
			start--;
			edge--;
		}
		end = data['pages'];
	}

	// Jump Area Wrapper
	var onclick = "vpjs_render_video_feed('" + data['target'] + "', '" + data['action'] + "', '<page>');return false;";
	
	// Jump Links Side
	var output = '<ul>';

	if (data['page'] > 1)
	{
		output += '<li><a href="#" onclick="' + onclick.replace('<page>', (data['page'] - 1)) + '">&laquo;&laquo;</a></li>';
	}

	if (start > 1)
	{
		output += '<li><a href="#" onclick="' + onclick.replace('<page>', 1) + '">1</a></li>';
	}

	if (start > 2)
	{
		output += '<li>&hellip;</li>';
	}

	for (var ptr = start; ptr <= end; ptr++)
	{
		if (ptr != data['page'])
		{
			output += '<li><a href="#" onclick="' + onclick.replace('<page>', ptr) + '">' + ptr + '</a></li>';
		}
		else
		{
			output += '<li>' + ptr + '</li>';
		}
	}

	if (end < (data['pages'] - 1))
	{
		output += '<li>&hellip;</li>';
	}
	
	if (end < data['pages'])
	{
		output += '<li><a href="#" onclick="' + onclick.replace('<page>', data['pages']) + '">' + data['pages'] + '</a></li>';
	}
	
	if (data['page'] < data['pages'])
	{
		output += '<li><a href="#" onclick="' + onclick.replace('<page>', (data['page'] + 1)) + '">&raquo;&raquo;</a></li>';
	}

	output += '</ul>';
	
	// "Go To Page" Side
	output += '<div class="vich_goto">';
	output += '<p class="vich_goto_title">Go To:</p>';

	onclick = "vpjs_render_video_feed('" + data['target'] + "', '" + data['action'] + "', vpjs_goto_value('" + data['target'] + "'));";
	onkeypress = 'if(event&&((event.which==13)||(event.keyCode==13))){' + onclick + '}';
	
	output += '<input id="' + data['target'] + '_goto" onkeypress="' + onkeypress + '" />';
	output += '<div class="vich_goto_button" onclick="' + onclick + '">go</div>';
	
	output += '</div>';

	return output;
}



/**
 * Simply hit our logging URL when playing popup videos.
 *
 * @param {int} vid
 * @param {String} username
 * @param {String} message
 * @param {String} source Optionally specify what is requesting the video
 */
function vpjs_logging_api(vid, username, message, source)
{
	var parameters = {
		op: "log",
		vid: vid,
		username: username,
		message: message
	};

	if (source)
	{
		parameters.source = source;
	}

	$.get(flpl_video_api_url, parameters);
}




// .o0o0o0o0o0o0o000o00o0o0oo0o0o00o0oo0o0o. \\
// .o000 == Scrolling Partner Logos == 000o. \\
// .o0o0o000o000o0o0oo0oo0o00o00o0o0o0oo00o. \\

/**
 * Keep track of the previous selected vendors.
 * @var {String}
 */
var old_selected = '';

/**
 * Changes a partner logo divs class based on a hover event.
 *
 * @global object VPJS_SHEPHERD - The video shepherd storing states for this page.
 * @param {Object} self - Id of the specific partner logo div in question.
 * @param {String} node - Name of the partner logos container component div.
 * @param {int} vendor_id - The vendor id associated with this div.php
 */
function vpjs_logos_onclick(self, node, vendor_id, category_id)
{
	category_id = category_id ? parseFloat(category_id) : 0;
	if (self && self.id && (vendor_id != '0'))
	{
		// Browse for the new vendor id
		if (VPJS_SHEPHERD && VPJS_SHEPHERD.is_a_shepherd)
		{
			vpjs_browse_helper(vendor_id, category_id);
			VPJS_SHEPHERD.set_state('vpjs_user_sort', VPL_VENDOR_SORT_TYPE[vendor_id] + '|' + VPL_VENDOR_SORT_DIR[vendor_id]);
		}
		
		// LLM - set the vendor category title to be the same as the title of the clicked vendor logo.
		var vendcat_title_div_id = "vich_channel_title"
		//var vendcat_title_div = document.getElementById('vendcat_title_text');
		if (vendcat_title_div_id)
		{
			var vendcat_title_div = document.getElementById(vendcat_title_div_id);
			var right_class = "vich_channel_title";
			var child_ps = self.getElementsByTagName('p');
			var this_vendor_text;
			if (child_ps.length > 0)
			{
				this_vendor_text = child_ps[0].innerHTML;
			}
			vendcat_title_div.innerHTML = this_vendor_text;
		}
		
		
		if (old_selected && vpjs_off_class)
		{
			// This may seem pointless, but there is good reason.  We are creating the
			// channels on the fly, so old blocks are replacing new blocks, and while they
			// share the same ID, they are technically not the same block.
			old_selected = document.getElementById(old_selected.id);
			if (old_selected)
			{
				old_selected.className = vpjs_off_class;
			}
		}
		
		// Highlight the new selected vendor.
		if (vpjs_selected_class)
		{
			self.className = vpjs_selected_class;
			old_selected = self;
		}
	}
}



/**
 * Changes a partner logo divs class based on a hover event.
 *
 * @param {Object} self - Id of the specific partner logo div in question.
 * @param {bool} off - When false, we are hovering over the element.  When true, we've moved away.
 */
function vpjs_logos_hover(self, off)
{
	if (off)
	{
		if (old_selected && old_selected.id && old_selected.id == self.id)
		{
			self.className = vpjs_selected_class;
		}
		else
		{
			self.className = vpjs_off_class;
		}
	}
	else
	{
		self.className = vpjs_hover_class;
	}
}



/**
 * Scroll the video channels.
 *
 * @param {String} node
 * @param {int} offset
 */
function vpjs_channels_scroll(node, offset)
{
	channel_offset += offset;
	channel_offset = channel_offset % VPL_CHANNELS.length;
	vpjs_render_channel_logos(node);
}



/**
 * Scroll the video channels.
 *
 * Should be called periodically and automatically.
 */
function vpjs_auto_scroll()
{
	vpjs_channels_scroll('vich_channels_list', -1);
}



/**
 * Stop the automatic scrolling of the video channels.
 */
function vpjs_pause_scroll()
{
	if (typeof(vpjs_auto_scroll_interval) != 'undefined' && vpjs_auto_scroll_interval)
	{
		clearTimeout(vpjs_auto_scroll_interval);
	}
}



/**
 * Resume the automatic scrolling of the video channels.
 */
function vpjs_resume_scroll()
{
	if (typeof(vpjs_auto_scroll_interval) != 'undefined' && vpjs_auto_scroll_interval)
	{
		vpjs_auto_scroll_interval = setInterval(vpjs_auto_scroll, vpjs_auto_scroll_delay);
	}
}



/**
 * Renders the video channel logos.
 *
 * @param {String} div_id
 */
function vpjs_render_channel_logos(div_id)
{
	var div = document.getElementById(div_id);
	if (!div)
	{
		return false;
	}

	if (VPL_CHANNELS.length == 0)
	{
		setTimeout('vpjs_render_channel_logos("' + div_id + '")', 250);
		return false;
	}

	if (channel_offset < 0)
	{
		var start = VPL_CHANNELS.length + channel_offset;
	}
	else
	{
		var start = channel_offset;
	}

	var end = start + vpjs_visible_channels;
	var overlap = 0;

	if (end > VPL_CHANNELS.length)
	{
		overlap = end - VPL_CHANNELS.length;
		end = VPL_CHANNELS.length;
	}

	var channels_to_render = new Array();

	for (var i = start; i < end; i++)
	{
		channels_to_render.push(VPL_CHANNELS[i]);
	}

	if (overlap)
	{
		for (var i = 0; i < overlap; i++)
		{
			channels_to_render.push(VPL_CHANNELS[i]);
		}
	}

	var div_build = '';
	for (var i in channels_to_render)
	{
		var vendor_name = channels_to_render[i]['vendor'];
		var vendor_id = VPL_VENDOR_ID_MAP[vendor_name];

		if (document.getElementById('vich_hidden_vendors_' + vendor_name))
		{
			// window.s is used by Omniture to store Omniture related data.
			// window.omniLink is a helper function for linktracking. If neither
			// are defined, we assume the client is not set up for Omniture and
			// will not include Omniture functionality here.
			var omniture_js = (window.s != undefined && window.omniLink != undefined) ? ' s.prop8=\'' + vendor_name + '\'; omniLink(this,\'' + vendor_name + '\');' : '';

			div_build += '<div id="vich_channel_' + vendor_id + '" class="' + (old_selected && old_selected.id && old_selected.id == 'vich_channel_' + vendor_id ? vpjs_selected_class : vpjs_off_class) + '" onmouseover="vpjs_logos_hover(this, false);" onmouseout="vpjs_logos_hover(this, true);" onclick="vpjs_logos_onclick(this,\'vich_channels\',' + vendor_id + ');' + omniture_js + '">';
			div_build += document.getElementById('vich_hidden_vendors_' + vendor_name).innerHTML;
			div_build += '</div>';
		}
	}

	div.innerHTML = div_build;

	return 1;
}





// .o0o0o0o0o0o0o000o00o0o0oo0oo0oo0o0o. \\
// .o000 == Video Link Handlers == 000o. \\
// .o0o0o000o000o0o0oo0oo0o00o00o0oo00o. \\ 



/**
 * Opens a new window for a URL.
 *
 * @param {int} video_id
 * @param {String} username
 * @param {String} destination
 * @param {int} popup_width
 * @param {int} popup_height
 * @param {int} scrollbars
 */
function vpjs_link_remote_url(video_id, username, destination, popup_width, popup_height, scrollbars)
{
	vpjs_logging_api(video_id, username, 'popupvideo');
	pldv_hide();
	vpjs_3rdparty_close();
	var status = window.open(destination.replace(/&amp;/g, '&'),'VideoPlayer','width=' + popup_width + ',height=' + popup_height + ',scrollbars=' + scrollbars + ',location=0,toolbar=0,directories=0,status=0,menubar=0,resizable=1');
	if (!status)
	{
		vpjs_show_popup_blocked_message();
	}
}



/**
 * Play a local windows media file.
 *
 * @param {int} video_id
 * @param {String} username
 */
function vpjs_link_local_wmp(video_id, username)
{
	pldv_playvideo(video_id, 'wmp');
}



/**
 * Play a local flash video.
 *
 * @param {int} video_id
 * @param {String} username
 * @param {String} source Help determine how the video started playing
 */
function vpjs_link_local_flash(video_id, username, source)
{
	vpjs_3rdparty_close();
	pldv_playvideo(video_id, 'flash', 0, source);
}



/**
 * Play a video in a 3rd party player
 *
 * @param {int} video_id
 * @param {bool} is_popup
 */
function vpjs_link_3rdparty(video_id, is_popup)
{
	vpjs_3rdparty_close();
	vpjs_logging_api(video_id, flpl_username, '3rdparty');

	var url = vpjs_3rdparty_api_url + '?vid=' + video_id;
	if (is_popup)
	{
		window.open(url, 'VideoPlayer', 'scrollbars=1,location=0,toolbar=0,directories=0,status=0,menubar=0,resizable=1');
	}
	else
	{
		$("#vich_3rdparty_player").load(url);
	}
	pldv_hide();
}


/**
 * Displays the 3rd party container and centers it in the screen
 */
function vpjs_show_3rdparty_player()
{
	var force_visible = [];
	$("#vich_3rdparty_player iframe").add("#vich_3rdparty_player embed").add("#vich_3rdparty_player object").each(function(i, obj)
	{
		if ($(obj).get(0).style.visibility != "hidden")
		{
			force_visible.push($(obj));
		}
	});
	hide_disruptive_overlay_elements();
	$.each(force_visible, function(i, obj)
	{
		$(obj).css("visibility", "visible");
	});
	vpjs_show_modal();

	$("#vich_3rdparty_container").show();
	var width  = $("#vich_3rdparty_container").width();
	var height = $("#vich_3rdparty_container").height();

	var left = ($(window).width()/2) - (width/2) + $(window).scrollLeft();
	var top  = ($(window).height()/2) - (height/2) + $(window).scrollTop();

	$("#vich_3rdparty_container").css({
		"left": left,
		"top": top
	});
}


/**
 * Closes the 3rd party player
 */
function vpjs_3rdparty_close()
{
	vpjs_hide_modal();
	$("#vich_3rdparty_container").hide();
	$("#vich_3rdparty_player").html("");
	show_disruptive_overlay_elements();
}



/**
 * Play a local flash video and select a vendor.
 *
 * @param {int} video_id
 * @param {int} vendor_id
 */
function vpjs_select_promo_vid(video_id, vendor_id)
{
	pldv_playvideo(video_id, 'flash');
	vpjs_initial_vend_cat_select(vendor_id, '');
}



/**
 * Play a local wmp video and select a vendor.
 *
 * @param {int} video_id
 * @param {int} vendor_id
 */
function vpjs_select_promo_wmpvid(video_id, vendor_id)
{
	pldv_playvideo(video_id, 'wmp');
	vpjs_initial_vend_cat_select(vendor_id, '');
}



// .o0o0o0o0o0o0o000o00o0o0oo0o0o00o0oOoOoOoo0o0o. \\
// .o000 == Comment Tag Template Handlers == 000o. \\
// .o0o0o000o000o0o0oo0oo0o00o00o0oo0OooOOo0oo00o. \\



/**
 * Handles the initial selection of vendors and categories if specified in a URL.
 *
 * @param {int} vendid
 * @param {int} catid
 */
function vpjs_initial_vend_cat_select(vendid, catid)
{
	var partner_logo = document.getElementById('vich_channel_' + vendid);
	if (partner_logo !== null)
	{
		vpjs_logos_onclick(partner_logo, 'vich_channels', vendid, catid);
	}
	else
	{
		vpjs_browse_helper(vendid, catid);
		var vendcat_title_div_id = "vich_channel_title";
		if (vendcat_title_div_id)
		{
			var vendcat_title_div = document.getElementById(vendcat_title_div_id);
			vendcat_title_div.innerHTML = VPL_VENDOR_DISPLAY_NAME_MAP[vendid];
		}
	}
}



/**
 * Populate the VideoSplash.
 *
 * NOTE: You will want to read about the types of video splashes to understand how this function works. 
 * Go to badger/badger/video/classes/videohelper_class.php-->getVideoSplash()
 */
function vpjs_videosplash()
{
	//Don't display a video splash for search
	if (vpjs_get_user_search().length == 0)
	{
		var vendors = vpjs_get_vendor_ids();
		var categories = vpjs_get_category_ids();
		
		var vendor_id = (vendors.length == 1) ? vendors[0] : 'none';
		var category_id = (categories.length == 1)? categories[0] : 'none';
		var cache_key = vendor_id+"_"+category_id;
		
		if (vpjs_video_splash_cache[cache_key])
		{
			$("#vich_vs").html(vpjs_video_splash_cache[cache_key]);
		}
		else
		{
			$.get(vpjs_video_splash_url,{'vendor_id':vendor_id,'category_id' : category_id},
			function (data)
			{
				vpjs_video_splash_cache[cache_key]=data;
				$("#vich_vs").html(data);
			});
		}
	}
	else
	{
		$("#vich_vs").html("");
	}
}

/**
 * Call the promo column api.
 *
 * Attempt to retrieve previously loaded information from vpjs_promo_column_cache.  If 
 * nothing can be found, we will request the information from an AJAX call and then store it.
 */
function vpjs_promo_column()
{
	var promo_type = 'default';

	if (vpjs_get_vendor_ids().length == 1)
	{
		promo_type = vpjs_get_vendor_ids()[0];
	}

	if (vpjs_promo_column_cache[promo_type])
	{
		vpjs_populate_promo_column(promo_type);
	}
	else
	{
		new SynAjax().request(
			vpjs_promotional_column_api_url,
			{
				method: 'get',
				parameters: 'promo_type=' + promo_type,
				onComplete: vpjs_promo_column_complete
			}
		);
	}
}



/**
 * Obtain and store the promotional column from the AJAX call response.
 *
 * @param {Resource} req
 */
function vpjs_promo_column_complete(req)
{
	var text = req.responseText;

	var promo_type = parseFloat(text.substr(0,7));
	promo_type = promo_type > 0 ? promo_type : 'default';
	var promo_column = text.substr(7);

	vpjs_promo_column_cache[promo_type] = promo_column;
	vpjs_populate_promo_column(promo_type);
}



/**
 * Populate the promotional column.
 *
 * @param {String} promo_type
 */
function vpjs_populate_promo_column(promo_type)
{
	document.getElementById('vich_promotional').innerHTML = vpjs_promo_column_cache[promo_type];
}



/**
 * Neatly trim the text by removing useless whitespace at the beginning and ending of the string.
 *
 * @param {String} text
 * @return {String}
 */ 
function vpjs_trim(text)
{	
	return text.replace(/^\s*(\b.*\b|)\s*$/, '$1');
}



/**
 * Show/hide a block.
 *
 * @param {String} block
 * @return {bool}
 */
function vpjs_block_swap(block)
{
	var id = document.getElementById(block);
	if (id)
	{
		if (id.style.display != 'none')
		{
			vpjs_block_hide(id.id);
		}
		else
		{
			vpjs_block_show(id.id);
		}
		return true;
	}
	return false;
}



/**
 * Hide a block.
 *
 * @param {String} block
 * @param {bool} whole_section
 * @return {bool}
 */
function vpjs_block_hide(block, whole_section)
{
	whole_section = whole_section == undefined ? false : whole_section;

	var id = document.getElementById(block);
	if (id)
	{ 
		var parent = id.parentNode;

		if (whole_section)
		{
			parent.style.display = 'none';
		}
		else
		{
			id.style.display = 'none';
			var images = parent.getElementsByTagName("span");
			for (var i = 0; i < images.length; i++)
			{
				if (images[i].className == "vich_arrow_open")
				{
					images[i].className = "vich_arrow_closed";
					break;
				}
			} 
		}
		return true;
	}
	return false;
}



/**
 * Show a block.
 *
 * @param {String} block
 * @param {bool} whole_section
 * @return {bool}
 */
function vpjs_block_show(block, whole_section)
{
	whole_section = whole_section == undefined ? false : whole_section;

	var id = document.getElementById(block);
	if (id)
	{ 
		var parent = id.parentNode;

		if (whole_section)
		{
			parent.style.display = 'block';
		}
		else
		{
			id.style.display = 'block';
			var images = parent.getElementsByTagName("span");
			var arrow_image = null;
			for (var i = 0; i < images.length; i++)
			{
				if (images[i].className == "vich_arrow_closed")
				{
					images[i].className = "vich_arrow_open";
					break;
				}
			}
		}
		return true;
	}
	return false;
}

function vpjs_show_modal()
{
	$("#vich_modal").height($(document).height()).width($(document).width()).show();
}

function vpjs_hide_modal()
{
	$("#vich_modal").hide();
}

function vpjs_show_popup_blocked_message()
{
	vpjs_show_modal();
	var top_position = $(window).height()/2 + $(window).scrollTop() - $("#vich_popup_blocked_message").outerHeight()/2;
	$("#vich_popup_blocked_message").css("top", top_position).show();
}

function vpjs_hide_popup_blocked_message()
{
	vpjs_hide_modal();
	$("#vich_popup_blocked_message").hide();
}

$(document).ready(function()
{
	$("#vich_3rdparty_container .close a").click(function()
	{
		vpjs_3rdparty_close();
		return false;
	});

	$("#vich_popup_blocked_message .close a").click(function()
	{
		vpjs_hide_popup_blocked_message();
		return false;
	});
});
