var currentPage = 'movie';

/************** Main Javascript Call**************/
			$(document).ready(function() { 
								
				
				 $(window).hashchange( function(){
				    // Alerts every time the hash changes!
				    ilovehash.init(page);
				    if(ilovehash.get("test")) { 
				    	
				    }
				  });
				ilovehash.init(page);
							
				$('#header').click(function() { 
					document.location.href = 'http://pirateindex.com';
				});
			
			}); // End of Document Ready
			
			
			
//*******************************************
// Site Page Controller using iLOVEhash.js
//*******************************************
var page = {
	
	devnotes:function() {
		$('#nav a').removeClass('active');
			$('#nav a.devnotes').addClass('active');
		$.ajax({
    		url:'/devnotes.php',
    		method:'GET',
    		success:function(data) {
    			$('.index ul').html(data);
    		}
    	});
    	
    	currentPage="devnotes";
    	clicky.log('#devnotes','Developer Notes');
			
	
	},
	
	//*******************************************
	// #tv controller
	//*******************************************
	tv:function() {

		if(currentPage!="tv") {
			$('#nav a').removeClass('active');
			$('#nav a.tv').addClass('active');
			$('.index ul').html('<li class="loading" style="height:300px; margin-top:50px; text-align:center; font-weight:bold;">Loading TV Top List...</li>');
			$.ajax({
				url:'/services.php?action=index&type=tv',
				success:function(data) {
					$('.index ul').html(data);
				}
			});
			
			currentPage = "tv";
						
		}
		
		if(ilovehash.get("a")=="details") {
			page.details('tv');
			clicky.log('#tv/a:detailsname:'+ilovehash.get("name"),'TV Details');
			document.title = ilovehash.get("name")+" is on the top 50 TV Shows pirated @PirateIndex";
		} else {
			clicky.log('#tv','TV Home');
			document.title = "Top Pirated Television Shows. @PirateIndex";
		};
	},
	
	//*******************************************
	// #music Controller
	//*******************************************
	music:function() {
		if(currentPage!="music") {
			$('#nav a').removeClass('active');
			$('#nav a.music').addClass('active');
			$('.index ul').html('<li class="loading">Loading Music Top List...</li>');
			$.ajax({
				url:'/services.php?action=index&type=music',
				success:function(data) {
					$('.index ul').html(data);
				}
			});
			currentPage="music";
		}
		
		
		if(ilovehash.get("a")=="details") {
			page.details('music');
			clicky.log('#music/a:details/name:'+ilovehash.get("name"),'Music Details');
			document.title = ilovehash.get("name")+" is on the top 50 music albums pirated @PirateIndex";
		} else {
			clicky.log('#music','Music Home');
			document.title = "Top Pirated Music. @PirateIndex";
		};
	},
	
	//*******************************************
	// #movie controller
	//*******************************************
	movie:function() {
		if(currentPage!="movie") {
		//	alert("CurrentPage is not Movie");
			$('#nav a').removeClass('active');
			$('#nav a.movies').addClass('active');
			$('#index ul').html('<li class="loading">Loading Movies Top List...</li>');
			$.ajax({
				url:'/services.php?action=index&type=movie',
				success:function(data) {
					$('.index ul').html(data);
				}
			});
			currentPage="movie";
		} // End if not the Movies Page already
		
		if(ilovehash.get("a")=="details") {
			page.details('movie');
			clicky.log('#movie/a:details/name:'+ilovehash.get("name"),'Movie Details');
			document.title = ilovehash.get("name")+" is on the top 50 Movies Pirated @PirateIndex";
		} else {
			clicky.log('#movie','movie Home');
			document.title = "Top Pirated Movies. @PirateIndex";
		};
	},
	
	//*******************************************
	// #action/a:details Function 
	// where action (tv|movie|music)
	//*******************************************
	details:function(action) {
		
		$('li').removeClass('open');
		
		name = (ilovehash.get('name'));
		key = ilovehash.get("key");
		
		$('li .movie-details').hide();
		var obj = $('li#'+key+' .movie-details');
		
		$('li#'+key).addClass('open');
		
		obj.show(function() { 
			
			

			
		}).addClass('open');
		$('html, body').animate({
			    scrollTop: $('li#'+key).offset().top
			}, 30);
		
		$('.trailer').html("");
		
		$.ajax({
  			dataType:'json',
	  		url:'/services.php?action='+action+'&q='+name,
	  		success:function(data) { 
	  			//alert(data[0].artwork[0]);
	  			
	  		
	  			$('.loading',obj).hide();
	  			
				if(data.trailer==null) {
					data.trailer = {
						video: {
							files : {
								file : []
							}
						}
					} 
				}
				
	  			if(data.trailer.video.files.file.length>0) {
	  				var videourl = data.trailer.video.files.file[0].content;
	  				
	  				//*******************************************
	  				// Add the Trailer
	  				//*******************************************
	  				
	  				if(videourl.substr(0,4)=="http") {
			  				// Its a Flash Video
			  				if(browser_type=="mobile") {
			  					
			  					$('.trailer',obj).html('<object width="290" height="180"><param name="movie" value="'+data.trailer.video.files.file[0].content+'"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="'+data.trailer.video.files.file[0].content+'" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="290" height="180"></embed></object>' );
			  				
			  				} else {
			  				
			  					$('.trailer',obj).html('<object width="475" height="320"><param name="movie" value="'+data.trailer.video.files.file[0].content+'"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="'+data.trailer.video.files.file[0].content+'" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="475" height="320"></embed></object>' );
			  					
			  				}
			  			} else {
			  				// Its a Stream
			  				
			  					$('.trailer',obj).html('<video src="'+videourl+'">Your browser does not support the VIDEO tag and/or RTP streams.</video>');
			  			}

	  				
	  				//*******************************************
	  				// End Adding The Trailer
	  				//*******************************************
	  				
	  			} else {
	  				var videourl = false;
	  			
	  				$('.container',obj).prepend("<div class='description'><p class='overview' style='font-weight:bold; font-size:10px; text-align:center'>Hrm, no additional info found. We'll keep digging.</p></div>");
	  				$('.trailer').hide();
	  			}
	  			
	  			if(action=="movie") {
	  				if(data.details.certification) {
	  					rating=data.details.certification;
	  				} else {
	  					rating="??";
	  				}
	  				$('.container .description',obj).remove();
	  				$('.container',obj).prepend("<div class='description'><div class='status'><span class='rating'>"+rating+"</span></div><p class='overview'>"+data.details.overview+"</p></div>");
	  			
	  			} 			
	  				  			
	  		}
  		});
		
	}
} // End Page Controller



