/*
/*
# ------------------------------------------------------------------------
# Gear Youtube Playlist Player
# ------------------------------------------------------------------------
# Copyright (C) 2015 http://joomlagear.org/. All rights reserved.
# @license http://www.gnu.org/licenseses/gpl-3.0.html GNU/GPL
# Author: Joomla Gear
# Websites: http://joomlagear.org
# ------------------------------------------------------------------------
*/
var videoTitle = "";
function initPlayer(id, page, theSearch, width, height, theAutostart, ypage) {
	if(theSearch == 'undefined') return;
		if(page == 'combination') { //ACtion for Droplist ONLY
			var psearch = theSearch.split('|');
			page = psearch[1];
			theSearch = psearch[0];
		}
		var yindex = 1, yresult = 50; 
		theOptions['id'] = id;
		theOptions[id+'page'] = page;
		theOptions[id+'width'] = width;
		theOptions[id+'height'] = height;
		theOptions[id+'theAutostart'] = theAutostart == true?'true':'false';
		var autostart = '';
		if(theAutostart == true) autostart = '&autoplay=1';
		
		if((ypage == 'next') || (ypage == 'previous')) {
					var ytresult = theOptions['max-results'],			
						ytpage = theOptions['start-index'],
						yttotal = theOptions[id+'ytotal'];	
						theOptions[id+'showP'] = false;
						theOptions[id+'showN'] = false;
			switch (ypage) {
				case "next":					
					if(yttotal > ytresult) {
						if(yttotal > (ytpage + ytresult)) {
							theOptions['start-index'] += ytresult;
							showplaylist(id, theOptions[id+'user']);
							}
						}
					else theOptions[id+'showNP'] = 0;
				break;
				case "previous":
						if(yttotal > ytresult) {
							if(ytpage > ytresult){
								theOptions['start-index'] -= ytresult;
								showplaylist(id, theOptions[id+'user']);
							} 
						}
				break;		
			}
			// alert('NP '+ theOptions[id+'showN']+theOptions[id+'showP']+ '  Index: '+theOptions['start-index']);
		}
		if(theSearch == '...') return;		
		switch (page) {
			case "video":
			showPWatchingInfo(id);
			video = "\n<div class=\"yvideo-container\">\n<iframe width=\"" + width +"\" height=\""+height+"\" src=\"http://www.youtube.com/embed/"+theSearch+"?showinfo=0&iv_load_policy=3&modestbranding=1&nologo=1&vq=large" + autostart + "&ps=docs\" frameborder=\"0\" allowfullscreen=\"1\">\n</iframe>\n</div>\n";
			var temp = document.getElementById(id+'-playlist');
			if(temp) {
				temp.innerHTML = '';
				temp.style.display="none";
				}
			break
			case "playlist":
				video = "\n<div class=\"yvideo-container\">\n<iframe width=\"" + width +"\" height=\""+height+"\" src=\"http://www.youtube.com/embed?listType=playlist&list="+theSearch+autostart+"\" frameborder=\"0\" allowfullscreen=\"1\">\n</iframe>\n</div>\n";			
				if(ypage == 'init'){
					showplayList(id,theSearch);
					var temp = document.getElementById(id+'-playlist');
					if(temp) {
						temp.innerHTML = '';
						temp.style.display="none";
						}				
				} else showPWatchingInfo(id);
			break						
		}
	var tmp=document.getElementById(id+'-wrapper');
	if (tmp) { tmp.innerHTML = video; }
}
function showplayList(id, theBlock){
	var playListURL = 'https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=50&key=AIzaSyDgUkAEPr-DkH9tmzJafc_td9S_c5EmT2U&playlistId='+theBlock;
    var iwidth = theOptions[id+'iwidth'], iheight = theOptions[id+'iheight'], width = theOptions[id+'width'], height = theOptions[id+'height'];
	jQuery.getJSON(playListURL, function(data) {
	    var list_data="";
	    var list='<div class="mod_gear_youtube_playlist_player">';
	    jQuery.each(data.items, function(i, item) {
	        var feedTitle = item.snippet.title;
	        var videoID = item.snippet.resourceId.videoId;
	        var thumb = "http://img.youtube.com/vi/"+ videoID +"/default.jpg";
	        var data = {
                                title: escapeHtml(feedTitle),
                                plid: videoID,
                                thumb: thumb 
                            };
	        list += '<a href="javascript:void(0)" onclick="videoTitle=\''+(data.title)+'\';initPlayer(\''+id+'\',\'video\',\''+(data.plid)+'\',\''+width+'\', \''+height+'\',true);">';
            list += '<img style="height: '+iheight+'px; width: '+iwidth+'px;" src="'+(data.thumb)+'" title="'+(data.title)+'" />';
        	list += '</a>';	
	    });
	    list +='</div>';
	    var playlist = document.getElementById(id+'-youtubevideo-playlist');
			if (playlist) {
				playlist.innerHTML = list; playlist.style.display="block";
			}	
	});


    	
}

function showPWatchingInfo(id) {
		var title = videoTitle;
			var bewatching = document.getElementById(id+'-youtubevideo-description');
				if (bewatching) { 
					bewatching.innerHTML = '<b>Watching: </b>' + title;
				}					  
	}
function get_button(id, button) {
	var iwidth = theOptions[id+'iwidth'], iheight = theOptions[id+'iheight'],
		width = theOptions[id+'width'], height = theOptions[id+'height'];
		
	var	btn = '<a href="javascript:void(0)" onclick="initPlayer(\''+id+'\',\'user\',\''+'...'+'\',\''+width+'\', \''+height+'\',true, \''+button+'\');">';
		btn += '<img style="height: '+iheight+'px; width: '+iwidth+'px;" src="'+theOptions['image']+button+'.gif'+'" title="'+button+'" />';
		btn += '</a>';								
	return btn;
}	
function escapeHtml(str) {
	var unsafe = str;
    return unsafe
         .replace(/&/g, "&amp;")
         .replace(/</g, "&lt;")
         .replace(/>/g, "&gt;")
         .replace(/"/g, "&quot;")
         .replace(/'/g, "&#039;");
 }
