//Return the value of a given query string variable
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] == variable) {
	  return pair[1];
	}
  } 
  //alert('Query Variable ' + variable + ' not found');
}

var domain = 'http://www.341production.com/';


//OK!! make video links ajaxified and pass navigatable link to the page url
function makeLinks(){
	$( ".lavori li a" ).each(function(i){
	 		var url = $(this).attr('rel');
	 		var cleanurl = url.replace(domain, ""); // domain is found in project.js
	 		$(this).attr('href','#!/'+cleanurl);
		}
	);
}


// when a video thumbnail is clicked, load up the video and meta with some ajaxy goodness
 function loadVideo() {

	$('.lavori li a, .previouspostbutton a, .nextpostbutton a').live('click',function(){
/* 		setTimeout("", 1000); */
		$.scrollTo( '0px', 2000 );
		
		var link = $(this).attr('rel');
		//$('#title').hide();
		$('#single_content').fadeOut();
		
		$('.lavori li span').html('loading').fadeOut();  // remove other loading states
		$(this).siblings('span').fadeIn(); // show loading state for selected

		
		/* $('#loading').fadeIn().css({backgroundPosition: "0 0"}).animate({backgroundPosition: '0 4000px'}, {duration:20000}); */
		$('#loading').fadeIn();
		$('#inner_wrap').slideUp(3000);
		
		//console.log(link);
		
		$('#inner_wrap').fadeOut('slow', function(){
			$('#inner_wrap').load(link + ' #single_content', // loads only the <div id='single-content'> from requested url
				function() {
					/* $('.rad #header, .rad #footer').hide(); */
					$('#single_content').css('opacity','0');
					$('#loading').fadeOut();
					
					$('#inner_wrap').slideDown(1000, function(){
						// make next, prev links into ajax links
						 $( ".previouspostbutton a, .nextpostbutton a" ).each(function(i){
								var url = $(this).attr('href');
						 		$(this).attr('rel',url);
						 		var cleanurl = url.replace(domain, ""); 
						 		$(this).attr('href',domain+'#!'+cleanurl); // domain is found in project.js
							}
						);
						// display correct source thumbnail
						/* var source = $('#video-source').attr('href');
						
						var result = source.search(/vimeo/);
						
						if(result != -1){
							$('#video-source').addClass('vimeo'); 
						}else{
							$('#video-source').addClass('youtube');
						}
						 */
						//fade into view
						$('#single_content').animate({
							opacity: '1'
						}, 2000);
						
						
						// fire the analytics for the ajaxed page
						/* var stripped_url = link.replace(domain, "");
						_gaq.push(['_trackPageview', stripped_url]); */
						
					});
					
					$('.lavori li span').html('watching');  //hide loading image after load is complete
					$.scrollTo($('body'), 1500);
				}
			);
		});

	});

}


// OK carico video in ajax per homepage e pagina tipologie
function loadFromUrl(extra){
	
	var url = location.href;
	//console.log(url);
	var cleanurl = url.replace('/'+extra+'#!', '');
	//console.log(cleanurl);
	var cleanurl = cleanurl.replace('/'+extra+'#', '');
	//console.log(extra);


	if (cleanurl != domain+extra){  // dominio salvato in function.js
		// load in video from page url
		$('#inner_wrap').load(cleanurl + ' #single_content' , 
			function() {
				/* $('.rad #header, .rad #footer').hide(); */
				$('#inner_wrap').css('opacity','0');
				$('#loading').fadeOut();
				$('#inner_wrap').slideDown('slow');
				$('#inner_wrap').animate({
					opacity: '1'
				}, 2000);
			}
		);
		
		// fire the analytics for the ajaxed page
/*
		var stripped_url = cleanurl.replace(domain, "");
		_gaq.push(['_trackPageview', stripped_url]);
*/
		
	}else{
		// make the first video in the list load by default
		$('.lavori li:first a').click();
		// some trickery to append the permalink to the url
		var perma = $('.lavori li:first a').attr('href');
		window.location = perma;
	}
}	


//OK!!  loads in more videos dynamically
function loadMore(){
	$('.load-more').click(function(){
		$(this).attr('value', 'Loading...');
		var test = $(this).prev('#lavori').html();

		$.ajaxSetup({cache:false});
		
		var offset = $(this).attr('rel'); // sets up the video offset so we load up the right videos in the right order
		var theid = 'more-results'+offset; //gives the container div a unique id for each set of results returned

		$(this).before('<div id="'+theid+'" style="display:none;"></div>');

		//  loads the new set of results in using some insane wordpress template page trickery
		$("#"+theid+"").load(domain+"ajax-call/",{x:offset}, function(data){
			//check if the div is NOT empty
			if (data.length > 4) {
				$('.load-more').attr('value', 'Even More');
				makeLinks(); //see the function above
				$(this).slideDown(500, function(){}); // once loaded the div slides down
			
				// makes the results fade into view as their parent container slides down
				$(this).children('ul').animate({
					opacity: '1'
				}, 1000);
			} else {
				$('.load-more').attr('value', 'No more stuff!');
			}
		});
		
		//sets up a new offset for the next set of results to be loaded 
		var newOffset = parseInt(offset)+4;
		$(this).attr('rel',newOffset);
		return false;
		
	});
}



