// clickable skin
$(document).ready(function() {
	$('.skinClick').attr('href',skinClickUrl); 
	var pos = $('#videosContainer').position();
	$('.skinClick').width(pos.left);
});
// submit ajax query with search checks and tab selection
function submitWithCheck(subcat) {
	// if performing search
	if ( subcat == 'Search' || subcat == '' ) {
		q = $('#qsearch').val();
		if ( q == defaultQ ) {
			q = '';
		}
	}
	// only show tab switch when switching tabs!
	if ( subcat ) {
		tabSwitch(VideoTabs,subcat);
	}
	// only need to refresh if we don't currently have any content
	if ( subcat == 'Search' || subcat == '' || $('#panel' + subcat).html() == '' ) {
		if ( subcat != 'Search' && subcat != '' ) {
			$('#panel' + subcat).html('<b class=\"loading\">Loading Videos...</b>');
		} else {
			var searchLoad = true;
		}
		submitSearch( (subcat == 'Search') ? '' : subcat,'','','', (searchLoad) ? false : true );
	}
	// ensures we do not submit the form
	return false;
}
// element: in which div to display result
function submitSearch(subcat, page, order, direction, firstload) {
	// get params
	if ( subcat != null ) {
		element = 'panel' + subcat;
	} else {
		subcat = '';
		element = '';
	}
	page = (page == null) ? 1 : page;
	order = (order == null) ? '' : order;
	direction = (direction == null) ? '' : direction;
	
	// add filters
	filters = new Array('genre','platform');

	if ( subcat == '' ) {
		filters[2] = 'searchSubcat';
	}

	filterLen = filters.length;

	params = {};
	for ( var i=0; i<filterLen; i++ ) {
		if ($('#'+subcat+filters[i]).length != 0 && $('#'+subcat+filters[i]).val() != '') params[filters[i]] = $('#'+subcat+filters[i]).val();
	}
		
	// adding order and direction as part of filter for simple code
	if (order != '') params['order'] = order;
	if (direction != '') params['direction'] = direction;
		
	// show loading screen
	if ( !firstload ) {
		loading(subcat, true);
	}

	// load search results
	sendSearchRequest( subcat, page, params, element );
		
	return false;
}
	
function sendSearchRequest(subcat, page, filter, element ) {
	jQuery.extend(filter, {'category': 'video', 'subcat': subcat, 'gameId': gameId, 'videoId': videoId, 'playlistId': playlistId, 'q': q, 'page': page });
	$.ajax({
			type: 'GET',
			url: urlSearch,
			data: filter,
			complete: function() { loading(subcat, false); },
			error: function(a,b,c) { alert(b); },
			success: function(msg) {
				$('#' + element).html(msg);
			} });
}

function loading(subcat, bool) {
	if (bool) {
		$('#' + subcat + 'Fields').css('display', 'none');
		$('#' + subcat + 'Loading').css('display', '');
		return;
	}

	$('#' + subcat + 'Fields').css('display', '');
	$('#' + subcat + 'Loading').css('display', 'none');
}

function clickEmbedInput(input) {
	input.focus();
	input.select();
}

function confirmWarning() {
	document.getElementById('hdWarning').style.display = 'none';
	document.getElementById('noFlash').style.display = 'block';
	document.cookie = cookiePrefix+"hdWarningSeen=1"+cookieExpires+"; path=/";
	embedVideo();
}
function player_DoFSCommand( command, args ) {
	arrArgs = String(args).split(';');
	if (command == 'advance') {
		advancePlaylist();
	}
}
// extension for SAFARI because fscommand does not work.
function playerFSObject() {
	this.player_DoFSCommand = player_DoFSCommand; 
}
function continuousPlayToggle(el) {
	if (continPlayEnabled) {
		continPlayEnabled = false;
		el.className = 'continuousOff';
		$(el).html('Continuous Playback Off');
	} else {
		continPlayEnabled = true;
		el.className = 'continuousOn';
		//$(el).html('Continuous Playback On');
		el.innerHTML = 'Continuous Playback On';
	}
	// set cookie
	document.cookie = cookiePrefix+"continPlay="+continPlayEnabled+cookieExpires+"; path=/";
	return false;
}
function setPref(type) {
	document.cookie = cookiePrefix+"vidResPref="+type+cookieExpires+"; path=/";
	//$.get(urlSetPref, {type: type, setpref: 1});
}
