// JavaScript Document
var player;
var instructorBy;
var classUrl = 'http://www.danceplug.com/class?outlet=';
var outletsResults = 1;
var myoutletsResults = 1;

$(document).ready(function(){
	instructorBy = $('#get_instructor_id').val();
	
	printMainTabs();
	printOutletsContent();
	printMyOutletsContent();
	youMayAlsoLike($('#next-id-video').val());
	classesFiveStar();

	$("#classes-features").tabs();
	$("#classes-outlets").tabs();
	
	$('#submit-outlets').click(function(){
		outletsResults = 1;
		printOutletsContent();	
		return false;								
	});
	
	$('#outlets-show-more').click(function(){
		$('#outlets-show-more a').html("loading...");
		outletsResults ++;
		printOutletsContent();	
		return false;
	});
	
	$('#myoutlets-show-more').click(function(){
		$('#myoutlets-show-more a').html("loading...");
		myoutletsResults ++;
		printMyOutletsContent();	
		return false;
	});
	
	$('#get-credit-button').click(function(){
		self.location = "/store";									 
	});
	
	$('#switch-resolution').click(function(){
		$.post(
	   '/danceplug_outlets/save_settings',
		{ 
		   type: 'definition'		   
		},
  		function(data){
			printMainTabs();
			outletsResults = 1;
			myoutletsResults = 1;
			printOutletsContent();
			printMyOutletsContent();
			
			name = $('#next-video').val();
			id = $('#next-id-video').val();
				
			if($('#next-definition-video').val() == "Low")
				name = name.replace("-lo-","-hi-");
			else
				name = name.replace("-hi-","-lo-");
			
			name = name.replace("/outletserver/","");
			
			if($('.playlist-items').length > 0)
				viewMyOutlet(id, name);
			else
				viewOutlet(id, name);
			
			
			$('#switch-resolution')[0].value = data.label;	
			$('#next-definition-video')[0].value = data.label;
 		}, 
		"json");
		
		$('.my-outlets-content').fadeOut();
    });
	
	$('#alow-warning-video').click(function(){
		
		$.post(
	   '/danceplug_outlets/save_settings',
		{ 
		   type: 'warning'		   
		},
  		function(data){
			$('.warning-label').html(data.label);
			if(data.label=="view")
				$('#allow-warning-video')[0].value = 0;
			else
				$('#allow-warning-video')[0].value = 1;
 		}, 
		"json");	
    });
	
	
	if($('#own-outlet')[0].value != "0"){
		var value = "";
		value = $('#own-outlet')[0].value.split(",");
		viewMyOutlet(value[0], value[1]);
	}
});

function playerReady(obj) {	
	player = document.getElementById(obj['id']);
	
	if($('#allow-warning-video')[0].value!="1" && $('#user-id')[0].value > 0){
		player.addModelListener("STATE","loadNextVideo");
	}
	
	player.addModelListener("STATE","stopVideo");
};

function stopVideo(obj){
	if(obj.newstate=="COMPLETED"){
		player.sendEvent("STOP","true");
		console.log('testing nows');
		player.innerHTML('Dp');
	}
	
}

function loadNextVideo(obj) {
	if(obj.newstate=="COMPLETED" || obj.newstate=="PAUSED"){
		player.removeModelListener("STATE","loadNextVideo");
		setTimeout("loaddelayed()",500);
	}
}

function loaddelayed()
{
	filename = $('#next-video').val();
    player.sendEvent("LOAD",{file:filename,provider: 'http','sharing.link':classUrl+$('#outlet-id')[0].value});
    player.sendEvent("PLAY","true");
}


function printOutletsContent(){
	if(outletsResults==1){
		$('.outlets-content').fadeOut();
		$('.loading-outlets').fadeIn();
		$('#outlets-show-more').hide();
	}
	if(instructorBy > 0) instructor = instructorBy; else instructor = $('#instructor-outlet')[0].value;
		
	instructorBy = 0;
		
	style = $('#style-outlet')[0].value;
	type = $('#type-outlet')[0].value;
	level = $('#level-outlet')[0].value;
	words = $('#search-outlet')[0].value;
	
	$.get(
	   '/outletbrowser',
		{ 
		   action: "outlets",
		   page: outletsResults,
		   instructor: instructor,
		   style: style,
		   type: type,
		   level: level,
		   words: words
		},
  		function(data){
			if(outletsResults==1){
				$('.outlets-content').html(data.content);
				$('.loading-outlets').fadeOut();
				$('.outlets-content').fadeIn();
			}
			else
				$('.outlets-content').append(data.content);
			
			$('#outlets-show-more a').html("show more classes");
			
			if(data.more_results == 0)
				$('#outlets-show-more').hide();
			else
				$('#outlets-show-more').show();
			
			$('.outlets-content a, .outlets-content img').tooltip({ 
			   track: true, 
			   delay: 0, 
			   showURL: false,
			   fade: 0 
		    });
 		}, 
		"json");
}

function printMyOutletsContent(page){
	if(myoutletsResults==1){
		$('.my-outlets-content').fadeOut();
		$('.loading-my-outlets').fadeIn();
		$('#myoutlets-show-more').hide();
	}

	
	
	instructor = $('#instructor-myoutlet').val();
	style = $('#style-myoutlet').val();
	type = $('#type-myoutlet').val();
	level = $('#level-myoutlet').val();
	words = $('#search-myoutlet').val();
	
	$.get(
	   '/outletbrowser',
		{ 
		   action: "outlets",
		   sub_action: "myoutlets",
		   page: myoutletsResults,
		   instructor: instructor,
		   style: style,
		   type: type,
		   level: level,
		   words: words
		},
  		function(data){;
			
			if(myoutletsResults==1){
				$('.my-outlets-content').html(data.content);
				$('.loading-my-outlets').fadeOut();
				$('.my-outlets-content').fadeIn();
			}
			else
				$('.my-outlets-content').append(data.content);
			
			$('#myoutlets-show-more a').html("show more classes");
			
			if(data.more_results == 0)
				$('#myoutlets-show-more').hide();
			else
				$('#myoutlets-show-more').show();
			
			$('.my-outlets-content a, .my-outlets-content img').tooltip({ 
			   track: true, 
			   delay: 0, 
			   showURL: false,
			   fade: 0 
		    });
 		}, 
		"json");
	$('.loading-my-outlets').hide();
	
}

function printMainTabs(){
	$.post(
	   '/danceplug_outlets/get_main_tabs',
		{  
		   type: 'main-tabs'
		},
  		function(data){;
			$('#featured-set').html(data.featured);	
			$('#latest-set').html(data.latest);	
			$('#most-viewed-set').html(data.viewed);	
			
			 // initialize scrollable 
			 $.easing.custom = function (x, t, b, c, d) {
				var s = 1.70158; 
				if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b;
				return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;
			 }

			 $("div.scrollable").scrollable({
				   size: 4,
				   items: '#thumbs',  
				   hoverClass: 'hover',
				   easing: 'custom', 
				   speed: 700}).navigator().mousewheel();	

			 
			 
			 $('#featured-set a, #featured-set img').tooltip({ 
			   track: true, 
			   delay: 0, 
			   showURL: false,
			   fade: 0 
		     });
			 $('#latest-set a, #latest-set img').tooltip({ 
			   track: true, 
			   delay: 0, 
			   showURL: false,
			   fade: 0 
		     });
			 $('#most-viewed-set a, #most-viewed-set img').tooltip({ 
			   track: true, 
			   delay: 0, 
			   showURL: false,
			   fade: 0 
		     });
 		}, 
		"json");
	return true;
}

function youMayAlsoLike(id){
	$.post(
	   '/danceplug_outlets/you_may_also_like',
		{ 
		   id: id
		},
  		function(data){;
			$('.you-may-also-content').html(data.content);	
			$('.you-may-also-content a, .you-may-also-content img').tooltip({ 
			   track: true, 
			   delay: 0, 
			   showURL: false,
			   fade: 0 
		    });
 		}, 
		"json");
	return true;
}

function clearOutlets(){
	$('#instructor-outlet')[0].value = "";
	$('#style-outlet')[0].value = "";
	$('#type-outlet')[0].value = "";
	$('#level-outlet')[0].value = "";
	$('#search-outlet')[0].value = "";
	
	outletsResults = 1;
	printOutletsContent();
}

function viewOutlet(id, name){
	$('#outlet-id').val(id);
	name = '/outletserver/'+name;

	$.post(
	   '/danceplug_outlets/most_viewed',
		{ 
		   id: id	   
		},
  		function(data){
			result = data.status;
			printOutletInfo(id);
			youMayAlsoLike(id);
 		}, 
	"json");
	
	if(player != undefined && player != null){
		player.sendEvent("LOAD",{file:name,provider: 'http','sharing.link':classUrl+$('#outlet-id')[0].value});
		player.sendEvent("PLAY","true");
		$('#next-video')[0].value = name;
		$('#next-id-video')[0].value = id;
	}
	
}

function viewMyOutlet(id, name){
	$('#outlet-id').val(id);
	
	name = '/outletserver/'+name;
	printMyOutletInfo(id);
	youMayAlsoLike(id);

	if(player != undefined && player != null){
		player.sendEvent("LOAD",{file:name,provider: 'http','sharing.link':classUrl+$('#outlet-id')[0].value});
		player.sendEvent("PLAY","true");
		$('#next-video')[0].value = name;
		$('#next-id-video')[0].value = id;
	}
}

function printOutletInfo(id){	
	$.get(
	   '/outletbrowser',
		{ 
		   action: "outlets-info",
		   id: id
		},
  		function(data){
			$('.classes-info').html(data.content);	
			$('.playlist-player').html("");
			initOverlays();
			jQuery('div.fivestar-form-item').fivestar();
			classesFiveStar();
          
 		}, 
		"json");
}

function initOverlays(){
	$("#mylaucher").click(function(){ 
			
		var linkapi = $(this).overlay({ 
			fadeInSpeed: 'normal',
			expose: {maskId: 'mask'},
			closeOnClick: false,
			closeOnEsc: false,
			preload: false,
			api: true,
			target: '#overlay-large-white', 
			onBeforeLoad: function(){ 
				$("#myframe-large-white").attr("src",this.getTrigger().attr("href")); 
				$("#myframe-large-white").load(function () { $('.loading-frame').fadeOut(); });
			},
			onBeforeClose: function(){ 
				$('.loading-frame').show();	
			},
			onClose: function(){
				$(".myframe-large-white").attr("src","");		
			}
		});
		
		linkapi.load();
		
		return false;
	}); 
	
	$(".mylauncher-large").overlay({ 
		expose: { color: '#333333', closeOnClick: false },
		preload: false,
		target: '#overlay-large', 
		onBeforeLoad: function(){ 
			$("#myframe-large").attr("src",this.getTrigger().attr("href")); 
			$("#myframe-large").load(function () { $('.loading-frame').fadeOut(); });
		},
		onBeforeClose: function(){ 
			$('.loading-frame').show();	
		},
		onClose: function(){
			$(".myframe-large").attr("src","");		
		}
	});
	
	$(".mylauncher-large-white").overlay({ 
		expose: { color: '#333333', closeOnClick: false },
		preload: false,
		target: '#overlay-large-white', 
		onBeforeLoad: function(){ 
			$("#myframe-large-white").attr("src",this.getTrigger().attr("href")); 
			$("#myframe-large-white").load(function () { $('.loading-frame').fadeOut(); });
		},
		onBeforeClose: function(){ 
			$('.loading-frame').show();	
		},
		onClose: function(){
			$(".myframe-large-white").attr("src","");		
		}
	});
}

function printMyOutletInfo(id){	
	
	$.get(
	   '/outletbrowser',
		{ 
		   action: "outlets-info",
		   sub_action: "myoutlets",
		   id: id
		   
		},
  		function(data){
			$('.classes-info').html(data.content);	
			$('.playlist-player').html(data.playlist);
			initOverlays();
			jQuery('div.fivestar-form-item').fivestar();
			classesFiveStar();
 		}, 
		"json");
}

function getThisOutlet(id){
	$.post(
	   '/danceplug_outlets/get_outlet',
		{ 
		   id: id		   
		},
  		function(data){
			$('.classes-info').fadeOut();
			$('.classes-info').html(data.content);	
			$('.classes-info').fadeIn();
 		}, 
		"json");
}

function currentPlayList(id){
	var current_list;
	current_list = $('#current-list').val();
	$('#outlet-list-'+id).addClass("active");
	$('#outlet-list-'+current_list).removeClass("active");
	$('#current-list')[0].value = id;
}
function viewMyVideo(name){	
	name = '/outletserver/'+name;
	
	if(player != undefined && player != null){
		player.sendEvent("LOAD",{file:name,provider: 'http','sharing.link':classUrl+$('#outlet-id')[0].value});
		player.sendEvent("PLAY","true");
	}
	return true;
}

function purchaseOutlet(id){
	$.post(
	   '/danceplug_outlets/purchase_outlet',
		{ 
		   oid: id		   
		},
  		function(data){
			if(data.status=="ok"){
				$('.classes-info').html(data.confirm_box);
				//alert("you have "+data.credits+" left");
				$('.credits-info').html("you have "+data.credits+" left");
				var credits = data.credits.split(" ");
				$('.my-links #mycredits').html('<a href="/store">get more class credits ('+credits[0]+')</a>');
				setTimeout("completePurchase("+id+", '"+data.video+"', '"+data.credits+"', '"+data.content+"')",5000);
			}else{
				alert("You are already renting this Outlet.");
			}

 		}, 
		"json");
}

function completePurchase(id, video, credits, content){
	outletsResults = 1;
	myoutletsResults = 1;
	printOutletsContent();
	printMyOutletsContent();
	printMyOutletInfo(id);
		
	video = '/outletserver/'+video;
	if(player != undefined && player != null){
		player.sendEvent("LOAD",{file:video,provider: 'http','sharing.link':classUrl+$('#outlet-id')[0].value});
		player.sendEvent("PLAY","true");
	}
			
	$('.my-outlets-content').fadeOut();
}

function classesFiveStar(){
	$('.star').click(function(){
		
		$.get(
	   '/danceplug_outlets/save_rating',
		{ 
		   oid: $('#outlet-id').val(),
		   vote: $('#edit-vote-0').val()
		},
  		function(data){
			$('.fivestar-form-item').addClass("clear-block");
			$('.fivestar-form-item').addClass("fivestar-processed");
			$('.fivestar-form-item').html(data.rating);
 		}, 
		"json");
	});
}
