function ajaxSearchPagination() {
	$('.ajaxresults-sort').children('th').children('a').click(function() {
		var url = $(this).attr('href');
		$(this).attr('href', 'javascript:void(0);')
		jQuery.post(url, null, function(data){
			if(data.length > 0) {
				$('#jsrplsr').html(data);
				ajaxSearchPagination();
				favUnfav();
			}
		});
		$(this).attr('href', url);
		return false;
	});
	
	$('.ajaxresults-num').children('span').children('a').click(function() {
		var url = $(this).attr('href');
		$(this).attr('href', 'javascript:void(0);')
		jQuery.post(url, null, function(data){
			if(data.length > 0) {
				$('#jsrplsr').html(data);
				ajaxSearchPagination();
				favUnfav();
			}
			return false;
		});
		$(this).attr('href', url);
		return false;
	});
	
	$('#ajaxresults-nav').children('.paginatenext').children('.nextbtn').click(function() {
		var url = $(this).attr('href');
		$(this).attr('href', 'javascript:void(0);')
		jQuery.post(url, null, function(data){
			if(data.length > 0) {
				$('#jsrplsr').html(data);
				ajaxSearchPagination();
				favUnfav();
			}
		});
		$(this).attr('href', url);
		return false;
	});
	
	$('#ajaxresults-nav').children('.paginateprevious').children('.prevbtn').click(function() {
		var url = $(this).attr('href');
		$(this).attr('href', 'javascript:void(0);')
		jQuery.post(url, null, function(data){
			if(data.length > 0) {
				$('#jsrplsr').html(data);
				ajaxSearchPagination();
				favUnfav();
			}
		});
		$(this).attr('href', url);
		return false;
	});
}

function uploadVideoChanging() {
	$('.linkvideoform').hide();
	$('.uploadvideoform').show();
	$('#linkvideonav').removeClass('selected');
	$('#uploadvideonav').addClass('selected');
	$('.instructions').html($('.hiddenulinstructions').html());
}

function favUnfav() {
	// Fav-unfav an item
	$(".fav-rating").children("li").children("a").click(function(data)
	{
		if ($(this).attr("href"))
		{
			var url = $(this).attr('href');
			var thelink = $(this);
			thelink.attr('href', 'javascript:void(0);');
			jQuery.post(url, null, function(data)
			{
				if (data == "error")
				{
					thelink.attr('href', url);
					window.alert("Sorry, there was a problem saving your favorite. Please try again.")
				}
				else if (data == "cantfav")
				{
					thelink.attr('href', url);
					return true;
				}
				else if (parseInt(data) == 10 || parseInt(data) == 0)
				{	
					//console.log(thelink.parent().html());
					
					theid = url.split('/');
					if (parseInt(data) == 10)
					{
						thelink.attr("title", "Add to favorites");
						thelink.attr("class", "fav");
						thelink.attr('href', '/favorites/remove/'+theid[3]);
					} else if(parseInt(data) == 0) {
						thelink.attr("title", "Remove from favorites");
						thelink.attr("class", "not-fav");
						thelink.attr('href', '/favorites/add/'+theid[3]);
					}
				}
			});
			return false;
		}
	});
}

$(document).ready(function(){
	$('img[@src$=.png]').ifixpng(); 
	$('#search-submit').attr('value', "");
	$('#nonjs-search-submit').attr('value', "");
	$("#userprofiletab").children('a').attr('href','javascript:void(0);');
	$(".loginpulink").attr('href','javascript:void(0);');
	$("#usermediatab").children('a').attr('href','javascript:void(0);');
	
	if(!$("#SearchSearchTerms").attr("value")) {
		$("#SearchSearchTerms").attr("value","Location or Keyword");
	}
	
	$("#SearchSearchTerms").focus(function(){
		if ($("#SearchSearchTerms").attr("value") == "Location or Keyword")
		{
			$("#SearchSearchTerms").attr("value","");
		}
	});
	
	$("#SearchSearchTerms").blur(function(){
		if(!$("#SearchSearchTerms").attr("value"))
		{
			$("#SearchSearchTerms").attr("value","Location or Keyword")
		}
	});

	//Login via JS form show
	var videocode = $(".videocontainer").html();
	var vimeocode = $(".vimeocontainer").html();
	
	$(".loginpulink").click(function(){
		$(".videocontainer").html('<img src="/img/qt_temp.gif" width="500px" width"300px" alt="" />');
		$(".vimeocontainer").html('<img src="/img/qt_temp.gif" width="500px" width"300px" alt="" />');
		$("#overlay").show();
		$("#loginBox").show();
	});
	
	$("#closejslb").click(function() {
		$(".videocontainer").html(videocode);
		$(".vimeocontainer").html(vimeocode);
		$("#loginBox").hide();
		$("#overlay").hide();
	});
	
        // User/Media Switch
        $(".medialisttab").attr('href', 'javascript:void(0);')
        $(".medialisttab").click(function(){
                //$("#usermediatab").attr('class', 'menuButton darkButton');
                //$("#userprofiletab").attr('class', 'menuButton lightButton');

                $('.memberprofilecontainer').hide();
                $('#miAbout').removeClass('selected');

                $('.membermediacomments').hide();
                $('#miComments').removeClass('selected');

                $('.memberitemslist').show();
                $('#miMedia').addClass('selected');
        });

        $(".aboutusertab").attr('href', 'javascript:void(0);');
        $(".aboutusertab").click(function(){
                //$("#usermediatab").attr('class', 'menuButton lightButton');
                //$("#userprofiletab").attr('class', 'menuButton darkButton');

                $('.memberitemslist').hide();
                $('#miMedia').removeClass('selected');

                $('.membermediacomments').hide();
                $('#miComments').removeClass('selected');

                $('.memberprofilecontainer').show();
                $('#miAbout').addClass('selected');

        });

        $(".linkmediacomments").attr('href', 'javascript:void(0);');
        $(".linkmediacomments").click(function(){
                //$("#usermediatab").attr('class', 'menuButton lightButton');
                //$("#userprofiletab").attr('class', 'menuButton darkButton');

                $('.memberitemslist').hide();
                $('#miMedia').removeClass('selected');

                $('.memberprofilecontainer').hide();
                $('#miAbout').removeClass('selected');

                $('.membermediacomments').show();
                $('#miComments').addClass('selected');

        });
	
	// Upload Page JS
	//Only show the tab if link to a video is already set
	if($('.uploadmethodnav').html()!='')
	{
		$('.uploadmethodnav').html('<li id="linkvideonav" class="selected"><a id="linkvideo">Link to a Movie</a></li>');
	} else if($('.uploadmethodnav').html()=='') {
		//if it's not, just show the video uploaded tab
		uploadVideoChanging();
		$('.uploadmethodnav').html('<li id="uploadvideonav" class="selected"><a id="uploadvideo">Your Uploaded Movie</a></li>');
	}
	
	$('.instructions').css('padding', '10px');
	
	$('#uploadvideo').click(function(){
		uploadVideoChanging();
	});
	
	$('#linkvideo').click(function(){
		$('.uploadvideoform').hide();
		$('.linkvideoform').show();
		$('#uploadvideonav').removeClass('selected');
		$('#linkvideonav').addClass('selected');
		$('.instructions').html($('.hiddenlinkinstructions').html());
	});
	
	ajaxSearchPagination();
	
	$('#search-submit').click(function() {
		var searchterms = $('#SearchSearchTerms').attr('value');
		var filter = $('#SearchFilter').attr('value');
		jQuery.post('/search/results/page:1', {"search_terms":searchterms,"filter":filter}, function(data){
			if(data.length > 0) {
				$('#jsrplsr').html(data);
				ajaxSearchPagination();
				favUnfav();
			}
		});
		return false;
	});

	// Star rating
	$('.star-rating').children('li').children('a').click(function() {
		var url = $(this).attr('href');
		var thelink = $(this);
		thelink.attr('href', 'javascript:void(0);')
		jQuery.post(url, null, function(data){
			if(data == 'error') {
				window.alert('Sorry, there was a problem saving your rating. Please try again.');
			} else if(data == 'cantrate') {
					return true;
			} else if(parseInt(data)<101 && parseInt(data)>0) {
				$('.star-rating').children('.current-rating').attr('style', 'width:'+data+'%;');
			}
			thelink.attr('href', url);
			return false;
		});
	});
	
	//Admin function to show profile for rating
	$(".clickuprofile").click(function(data){
		$(this).next().toggle();
	});
	
	favUnfav();
	
	$("#flashMsgOutter").animate({opacity: 1.0}, 20000)
	$("#flashMsgOutter").fadeOut("slow");
	
	 // hides the slickbox as soon as the DOM is ready
	 // (a little sooner than page load)
	if($.cookie('UseOpenID')=='true') {
	    // hide regular login form and show openid form
		$('.regularlogin').hide();
		$('.openidlogin').show();
	} else {
	    // hide openid form and show regular login form
		$('.openidlogin').hide();
		$('.regularlogin').show();
	}
    
	// Member Reg / Open ID Switch
	$('.regularlogin').ready(function() {
		$('a.openidswitch').click(function() {
    		$.cookie('UseOpenID', 'true', {expires: 300, path: '/'});
    		$('.regularlogin').hide();
			// $('#UserCreatepassword').attr('value', '');
			// $('#UserConfirmassword').attr('value', '');
    		$('.openidlogin').show();
		});
	});

	$('.openidlogin').ready(function() {
		$('a.regswitch').click(function() {
			$.cookie('UseOpenID', 'false', {expires: 300, path: '/'});
    		$('.openidlogin').hide();
			// $('#UserOpenidUrl').attr('value', '');
    		$('.regularlogin').show();
	    });
	});
});

function write_wlkqt(video, width, height, controller, scale, qttype, autoplay, bgcolor, mvname, node, pan, tilt, fov, kiosk, loop, pmtype, postermovie)
{
	str = '<OBJECT CLASSID="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B"  CODEBASE="http://www.apple.com/qtactivex/qtplugin.cab#version=7,0,0,0" WIDTH="'+width+'" HEIGHT="'+height+'"><PARAM NAME="scale" VALUE="'+scale+'"><PARAM NAME="type" VALUE="'+qttype+'"><PARAM NAME="target" VALUE="myself"><PARAM NAME="pluginspage" VALUE="http://www.apple.com/quicktime/download/index.html">';
	if(postermovie!=null) {
		str = str+'<PARAM NAME="src" VALUE="'+postermovie+'"><PARAM NAME="href" VALUE="'+video+'"><PARAM NAME="autoplay" VALUE="'+autoplay+'"><PARAM NAME="controller" VALUE="'+controller+'"><PARAM NAME="loop" VALUE="true">';
	} else {
		str = str+'<PARAM NAME="src" VALUE="'+video+'">'+'<PARAM NAME="autoplay" VALUE="'+autoplay+'">'+'<PARAM NAME="controller" VALUE="'+controller+'">';
		if(loop!=null) { str = str+'<PARAM NAME="loop" VALUE="'+loop+'">'; }
	}
	// if(bgcolor!=null) { 
	str = str+'<PARAM NAME="bgcolor" VALUE="#141414">';
	// }
	if(mvname!=null) { str = str+'<PARAM NAME="moviename" VALUE="'+mvname+'">'; }
	if(node!=null) { str = str+'<PARAM NAME="node" VALUE="'+node+'">'; }
	if(pan!=null) { str = str+'<PARAM NAME="pan" VALUE="'+pan+'">'; }
    if(tilt!=null) { str = str+'<PARAM NAME="tilt" VALUE="'+tilt+'">'; }
	if(fov!=null) { str = str+'<PARAM NAME="fov" VALUE="'+fov+'">'; }
	// str = str+'<PARAM NAME="wmode" VALUE="transparent">';
	if(kiosk=='true') { str = str+'<PARAM NAME="kioskmode" VALUE="true">'; }
	str = str+'<EMBED WIDTH="'+width+'" HEIGHT="'+height+'" SCALE="'+scale+'" TARGET="myself" type="'+qttype+'" bgcolor="#141414"';
	if(postermovie!=null) {
		str = str+' SRC="'+postermovie+'" HREF="'+video+'" CONTROLLER="false" LOOP="true" AUTOPLAY="true"';
	} else {
		str = str+' SRC="'+video+'" CONTROLLER="'+controller+'" AUTOPLAY="'+autoplay+'"';
		if(loop!=null) { str = str+' LOOP="'+loop+'"'; }
	}
	if(bgcolor!=null) { str = str+' BGCOLOR="'+bgcolor+'"'; }
	if(mvname!=null) { str = str+' MOVIENAME="'+mvname+'"'; }
	if(node!=null)
	{
		str = str+' NODE="'+node+'"';	
	}
	if(pan!=null) { str = str+' PAN="'+pan+'"'; }
	if(tilt!=null) { str = str+' TILT="'+tilt+'"'; }
	if(fov!=null) { str = str+' FOV="'+fov+'"'; }
	if(kiosk=='true') { str = str+' KIOSKMODE="true"'; }
	str = str+' PLUGINSPAGE="http://www.apple.com/quicktime/download/index.html"></EMBED></OBJECT>';
	document.write(str);
}
