var key = 0; 
var active_video = "";
var video_list = new Array();
var r_video_list = "";  
var shuffle = 0;
var list_i = 50;
var playing = false;
function Show_user_playlists()
{
	 $.ajax({
	type: 'GET',
	url: SITE+'engine.php?r='+$.random(1000),
	data: 'proc=show_user_playlists',
	success: function(message) {
		$('#m1').html("<br>"+message); 
	}
    }); 
}
function show_loading(id){
 $('#'+id).html("<br><br><br><center><img src='"+SITE+"/images/s2.gif'></center>");
}

function show_loading2(id){
 $('#'+id).html("<img src='"+SITE+"/images/s2.gif' width='16' height='16'>");
}
function winopen(url,winName,width,height)
{
	xpos = (screen.width-width)/2;
	ypos = (screen.height-height)/2;
	options = "scrollbars=1,width=500"+",height="+height+",left="+xpos+",top="+ypos+"position:absolute";
	newwindow=window.open(url, winName, options);
	if (window.focus) {newwindow.focus()}
	return false;

}

function refresh(){
        $.ajax({
        type: 'GET',
        url: SITE+'engine.php?r='+$.random(1000)+'&k='+key,
        data: '',
        success: function(message) {
            $('#plist').html(message); 
        }
    });  
}
function YeshowSavelistbox()
{
	show_dialog('list_manager', 'Add / Save Your Play List', 400);
}
function NoshowSavelistbox()
{
   setTimeout('window.location.reload();', 500);
}
function add_song(song){
    show_loading('plist');
    playlist = $("#list_items").val();
	if ( playlist=== undefined ){
	$.ajax({
        type: 'GET',
        url: SITE+'engine.php?r='+$.random(1000),
        data: 'proc=add_song_unregistered&song='+song,
        success: function(message) {
            $('#plist').html(message);
        }
    });
	} else {
    $.ajax({
        type: 'GET',
        url: SITE+'engine.php?r='+$.random(1000),
        data: 'proc=add_song&song='+song+'&playlist='+playlist,
        success: function(message) {
            $('#plist').html(message);
        }
    });
	}
}

function add_all(ids, start, end){
    show_loading('plist');
    $.ajax({
        type: 'GET',
        url: SITE+'engine.php?r='+$.random(1000),
        data: 'proc=add_all&keyword='+ids+'&start=' + start + '&end=' + end + '&k='+key,
        success: function(message) {
            $('#plist').html(message); 
        }
    });         
}

function clear_all(){
	pl = $("#list_items").val();
    show_loading('plist');

    if ( typeof pl !== "undefined" ) {
	    $.ajax({
	        type: 'GET',
	        url: SITE+'engine.php?r='+$.random(1000),
	        data: 'proc=clear_list&pl='+pl+'&k='+key,
	        success: function(message) {
	            $('#plist').html(message);
	        }
	    });
	} else {
		//in case there are no actuall db playlist involved just clean the output
		  $.ajax({
	        type: 'GET',
	        url: SITE+'engine.php?r='+$.random(1000),
	        data: 'proc=clear_listcookie',
	        success: function(message) {
	           alert("Your playlist is now empty.");
	        }
	    });
		$('#plist').html('<ul id="sortable"></ul><script type="text/javascript">$(function(){$("#sortable").sortable();});</script>')
		$('ul#sortable').html("<br><font class='msg2' style='padding-left:45px;'>Your playlist is empty. Add songs!</font>");
	}
}

function delete_list() {
	pl = $("#list_items").val();
    show_loading('plist');
    
	if (pl === undefined) { clear_all(); }
	else{
    $.ajax({
        type: 'GET',
        url: SITE+'engine.php?r='+$.random(1000),
        data: 'proc=delete_list&pl='+pl+'&k='+key,
        success: function(message) {
            $('#plist').html(message);
        }
    });
	$("#list_items option[value='"+pl+"']").remove();
	
	if ( $("#list_items").val() === undefined) {$("#selectbox").html("No playlists. Create your first!")}
	}
}

function add_list(){
        $.ajax({
            type: 'POST',
            url: SITE+'engine.php?r='+$.random(1000)+'&k='+key,
            data: 'proc=add_list&list='+list,
            success: function(message) {
                $('#uyeol').html(message);             
                refresh(); 
                create_select();                  
            }
        });  
}

function search_song(keyword){
	if($("#search_results").length == 0){
		$("#tabs").tabs("add", "#search_results", "Search Results");
		$("#tabs").tabs("select", 4);
	}
	else { $("#tabs").tabs("select", 4);}
	$('#loading').hide();
	$('.pagination_songs').empty();
	$('#keyword').val( keyword );
	show_loading('list');
	ytvbp.listVideos('all', keyword, 1);
	get_similar(keyword);
	$('#list_bt').show();
	//$('#all_link').attr('onclick', "add_all( '"+keyword+"', "+start+", "+end+" )");
}  

function search_playlist(keyword){
		show_loading("m1");
    $.ajax({
        type: 'GET',
        url: SITE+'engine.php?r='+$.random(1000)+'&keyword='+keyword,
        data: 'proc=search_playlist',
        success: function(message) {
        $('#m1').html(message);
        }
    });
		$('#similar').hide();
}  

function adm_search_song(keyword, start, end){
	if($("#list").length == 0){
		$("#tabs").append("<div id='list'></div>");
	}
    $('#loading').hide();
    $('#keyword').val( keyword );
    
    show_loading('list');
    $.ajax({
        type: 'GET',
        url: SITE+'engine.php?r='+$.random(1000),
        data: 'proc=adm_search_song&keyword='+keyword+'&start='+start+'&last='+end+'&k='+key,
        success: function(message) {
            $('#list').html(message); 
            $('#list_bt').show();      
            $('#all_link').attr('onclick', "add_all( '"+keyword+"', "+start+", "+end+" )");
        }
    });      
}

function remove_from_top_list(processing_id){
	$.ajax({
        type: 'GET',
        url: SITE+'engine.php?r='+$.random(1000),
        data: 'proc=remove_from_top_list&id='+processing_id
    }); 
	
	$('.rem_'+processing_id).hide();
}

function song_delete(list_id, song_id){
    $('#listItem_'+song_id).hide("slow").delay(1000).queue(function() { $(this).remove(); });

	//deleting song from cookie of unregistered user
    if (list_id=="c"){
	$.ajax({
        type: 'GET',
        url: SITE+'engine.php?r='+$.random(1000),
        data: 'proc=delete_from_cookie&song_id='+song_id+'&k='+key,
        success: function(message) {
           $('#plist').append(message);            
       }
    });
	}
	
	//if user doesn't have right to modify that playlist, just hides the song
	else if (list_id=="tmp"){}
	
	//default
	else{
    $.ajax({
        type: 'GET',
        url: SITE+'engine.php?r='+$.random(1000),
        data: 'proc=song_delete&list_id='+list_id+'&song_id='+song_id+'&k='+key
       // success: function(message) {
           // $('#plist').html(message);            
       // }
    });
	}
  //  stop();   
}

function list_delete(id){
    $.ajax({
        type: 'GET',
        url: SITE+'engine.php?r='+$.random(1000),
        data: 'proc=list_delete&list_id='+id+'&k='+key,
        success: function(message) {
            $('#uyeol').html(message);            
            refresh(); 
            create_select();             
        }
    });  
}

function get_list(list_id){
	pl = $("#list_items").val();
	if (pl === undefined) {}  //setting null playlist option, action skips is intentional
	else { $("#list_items").find('option.markedrmv').remove()}

	
    show_loading('plist');
    
    $.ajax({
        type: 'GET',
        url: SITE+'engine.php?r='+$.random(1000),
        data: 'proc=get_list&list_id='+list_id+'&k='+key,
        success: function(message) {
            $('#plist').html(message);
        }
    });
}

function add_to_select(list_id, list_name){
	new_option = '<option value="'+list_id+'">'+list_name+'</option>'
	$('#list_items').append(new_option);
	$('#list_items').val(list_id);
}

function create_select(){
    $.ajax({
        type: 'GET',
        url: SITE+'engine.php?r='+$.random(1000)+'&k='+key,
        data: 'proc=create_list',
        success: function(message) {
            $('#selectbox').html(message);
        }
    });      
}

function get_similar(key_text){   
    $.ajax({
        type: 'GET',
        url: SITE+'engine.php?r='+$.random(1000),
        data: 'proc=show_similar&keyword='+key_text+'&k='+key,
        success: function(message) {
            $('#similar').html(message); 
            $('#similar').show();
            $('#mycarousel').jcarousel({
            	
            }); 
        }
    });     
}

function set_song_info(id, title, img){
    var url = AMAZON_URI;
	//alert(url);
    var buy_url = url.replace("%title%", title)
    var shr = "winopen('http://www.facebook.com/sharer.php?s=100&p[title]="+escape(title)+"&p[summary]=Play this and thousands of other playlists and songs on CollegePlaylists.com&p[url]=http://collegeplaylists.com/?vid_id="+id+"', 'cleaning',500, 500);";
    
    $('#buy_btn').attr('href', buy_url);
    $('#shr_btn').attr('href', shr);
    $('#artist_pic').attr('src', img );
    $('#player_text').html(title);
	$("#player_song_id").html(id);
}
function opennewWin()
{
	var newtitle = $('#player_text').html();
	var songid = $('#player_song_id').html();
	winopen('http://www.facebook.com/sharer.php?s=100&p[title]='+escape(newtitle)+'&p[summary]=Play ' +escape(newtitle)+' on CollegePlaylists.com&p[url]=http://collegeplaylists.com/?vid_id='+songid, 'cleaning',500, 500);
}
function get_video_list(){
	var k = 0;
	while ( $('div#plist ul.player_list li').eq(k).attr('id') != undefined) {
		tmp=$('div#plist ul.player_list li').eq(k).attr('id');
		video_list[k]=tmp.replace("listItem_","");
		k++;
	}
	return video_list;
}
function Mark_Favorites(pl_id)
{
	$.ajax({
			type: 'GET',
			url: SITE+'engine.php?r='+$.random(1000),
			data: 'proc=Favorites&playlist_id='+pl_id,
			success: function(message) {
				if(message==1)
				{
					$('#fav_btn'+pl_id).attr('src', SITE+'images/Favorited.png');
					alert("Added In Your Favorites List");
				}
				else if (message==0)
				{
					$('#fav_btn'+pl_id).attr('src', SITE+'images/Unfavorited2.png');
					alert("Removed In Your Favorites List");
				}
				else
				{
					alert(message);
				}
				
			}
 	    }); 
}
function next_video(){
    var next = 0;
	var proc_array = new Array();
	
	proc_array = get_video_list(); //get current playlist
	
    if( shuffle ){
       next = Math.floor( Math.random() * proc_array.length );
	   loadNewVideo( proc_array[next] );
	   return;
    }
    
    for ( var i in proc_array ) { 
        if( next ){
			loadNewVideo(  proc_array[i] );  
            return;
        }
        
        if( active_video == proc_array[i]){
          next = 1;  
        }
        else {
          next = 0;  
        }
    }
    return;
}

function prior_video(){
    var next = 0;
	var proc_array = new Array();
	
	proc_array = (get_video_list()).reverse(); //get current playlist
	
    if( shuffle ){
       next = Math.floor( Math.random() * proc_array.length ); 
       loadNewVideo( proc_array[next] );
	   return;
    }
    
    for ( var i in proc_array )
    { 
        if( next ){
			loadNewVideo(  proc_array[i] );  
            return;
        }
        
        if( active_video == proc_array[i] ){
          next = 1;  
        }
        else {
          next = 0;  
        }       
    }
    return; 
}

function add_for_play(list_id){
	pl = $("#list_items").val();
	if (pl === undefined) {}  //setting null playlist option, action skips is intentional
	else if (pl == "chunk") {}
	else { 
		$("#list_items").prepend("<option value='chunk' class='markedrmv'></option>"); $("#list_items").val("chunk"); 
	}
	
	var tmp_var = $("#plist ul#sortable font").html()
	var result_message = "";
	if (tmp_var == "Your playlist is empty. Add songs!"){
    $.ajax({
        type: 'GET',
        url: SITE+'engine.php?r='+$.random(1000)+'&list_id='+list_id,
        data: 'proc=add_for_play_null',
        success: function(message) {
			$('#plist').html(message);       
        }
    });
	}else{
		$.ajax({
        type: 'GET',
        url: SITE+'engine.php?r='+$.random(1000)+'&list_id='+list_id,
        data: 'proc=add_for_play',
        success: function(message) {
			$('#plist ul#sortable').append(message); 
		}
    });	
	}
	
}

function load_for_play(list_id){
	pl = $("#list_items").val();
	if (pl === undefined) {}  //setting null playlist option, action skips is intentional
	else if (pl == "chunk") {}
	else { $("#list_items").prepend("<option value='chunk' class='markedrmv'></option>"); $("#list_items").val("chunk") }
	
	show_loading('plist');
    $.ajax({
        type: 'GET',
        url: SITE+'engine.php?r='+$.random(1000)+'&list_id='+list_id,
        data: 'proc=load_for_play',
        success: function(message) {
        $('#plist').html(message);
		$('div#plist a.link1').eq(0).addClass('active');
		tmp = $('div#plist ul#sortable').find('li').attr("id");
		
		loadNewVideo( tmp.replace("listItem_","") );  
		//$('#plist').append(resstr[0]);
        //list_config();           
        }
    });
	//$('#list_items').val(list_id);
}

function vote_up(list_id){
	show_loading2("rating_count_"+list_id);
    $.ajax({
        type: 'GET',
        url: SITE+'engine.php?r='+$.random(1000)+'&list_id='+list_id,
        data: 'proc=vote_up',
        success: function(message) {
       $('#rating_count_'+list_id).html(message);             
        }
    });
	$('#up_'+list_id).hide();
	$('#down_'+list_id).hide();
}

function vote_down(list_id){
	show_loading2("rating_count_"+list_id);
    $.ajax({
        type: 'GET',
        url: SITE+'engine.php?r='+$.random(1000)+'&list_id='+list_id,
        data: 'proc=vote_down',
        success: function(message) {
        $('#rating_count_'+list_id).html(message);            
        }
    });
	$('#up_'+list_id).hide();
	$('#down_'+list_id).hide();
}

function select_menu(tab){
	show_loading("m1");
    $.ajax({
        type: 'GET',
        url: SITE+'engine.php?r='+$.random(1000)+'&tab='+tab,
        data: 'proc=menu',
        success: function(message) {
        $('#m1').html(message);
        }
    });
	if (tab=="home"){
		$('#similar').show();
	} else {
		$('#similar').hide();
	}
}

function display_submit_page(){
	show_loading("m1");
	$.ajax({
        type: 'GET',
        url: SITE+'engine.php?r='+$.random(1000),
        data: 'proc=submit_page',
        success: function(message) {
        $('#m1').html(message);
        }
    });
	$('#similar').hide();
}

function get_username_lists(name){
	show_loading("playlist-output");
	$.ajax({
        type: 'GET',
        url: SITE+'engine.php?r='+$.random(1000),
        data: 'proc=sort_by_username&username='+name,
        success: function(message) {
        $('#playlist-output').html(message);
        }
    });    
}

function get_university_lists(proc_id, proc_name){
	tmp_name = proc_name;
	if (proc_name == "n/a"){tmp_name="none"}
	show_loading("playlist-output");
	$.ajax({
        type: 'GET',
        url: SITE+'engine.php?r='+$.random(1000),
        data: 'proc=sort_by_university&id='+proc_id+'&name='+tmp_name,
        success: function(message) {
        $('#playlist-output').html(message);
        }
    });    
}
function get_top_playlists_genere()
{
	var sortd = $('#most_popular').val();
	var genered = $('#genre').val();
	$.ajax({
			type: 'GET',
			url: SITE+'engine.php?r='+$.random(1000),
			data: 'proc=get_top_playlists_genere'+'&genered='+genered+'&sortd='+sortd,
			success: function(message) {
			$('#playlist-output').html(message);
			}
 	    });    
}
function sort_top_playlists(value){
	show_loading("playlist-output");
	if (value=="rated"){
		$.ajax({
			type: 'GET',
			url: SITE+'engine.php?r='+$.random(1000),
			data: 'proc=sort_by_most_rated',
			success: function(message) {
			$('#playlist-output').html(message);
			}
 	    });    
	}
	if (value=="recent"){
		$.ajax({
			type: 'GET',
			url: SITE+'engine.php?r='+$.random(1000),
			data: 'proc=sort_by_most_recent',
			success: function(message) {
			$('#playlist-output').html(message);
			}
 	    }); 
	}
}

function sort_lists_by_genre(value){
	if (value!=0)
	{
	show_loading("playlist-output");
	$.ajax({
			type: 'GET',
			url: SITE+'engine.php?r='+$.random(1000),
			data: 'proc=sort_by_genre&genre='+value,
			success: function(message) {
			$('#playlist-output').html(message);
			}
 	    });
	}
}

function sort_lists_by_period(value){
	show_loading("playlist-output");
	$.ajax({
			type: 'GET',
			url: SITE+'engine.php?r='+$.random(1000),
			data: 'proc=sort_lists_by_period&period='+value,
			success: function(message) {
			$('#playlist-output').html(message);
			}
 	    });
}

function set_list_vars( arr ){
    video_list = arr;
}
function set_shuffle(){

   if( shuffle == 0  ) {
   
      shuffle = 1;
      
      $('#shuffle').attr('src', SITE+'images/shufflesel.png' );        
   }
   else{
      shuffle = 0;
      $('#shuffle').attr('src', SITE+'images/shufflefin.png' );   
   }
}

function scroll_position(){
   var keyword = $('#keyword').val();     
   var end = 20;
   
   if( keyword ){
          
       var dpos = $('#list').scrollTop();
       var hg = $('#list')[0].scrollHeight;                 
       var dif = hg - dpos;  
          
       var start = list_i;    
                   
       if( dif < 350 && start < 220 ){ 
               
        list_i = list_i + end;
        
        $('#loading').show();            
        
        $.ajax({
            type: 'GET',
            url: SITE+'engine.php?r='+$.random(1000),
            data: 'proc=search_append&keyword='+keyword+'&start='+start+'&last='+end+'&k='+key,
            success: function(message) {
                $('#for_append').append(message);   
                $('#loading').hide();                     
            }
        });          
       }
   }
}

function share_list(){
    var uname = $('#user_name').val(); 
    var umail = $('#user_mail').val();   
    var desc = $('#your_msg').val();       
    var data = $('#data').val();       
    
    if( umail == "" || uname == "" || desc == "" ){
        alert("Please, enter all fields .");
        return false;
    }
    else{   
        // send shared list 
        $.ajax({
            type: 'POST',
            url: SITE+'engine.php?r='+$.random(1000),
            data: 'proc=share_list&user_name='+uname+'&user_mail='+umail+'&desc='+desc+'&data='+data,
            success: function(message) {
                if( message == "1" ){
                    alert("Your list successfull sent.")
                }         
                else{
                    alert(message);
                }
            }
        });          
        
    }    
}

function rate( datas ){
    $.ajax({
        type: 'POST',
        url: SITE+'engine.php?r='+$.random(1000),
        data: 'proc=rate_list&data='+datas,
        success: function(message) {
        if( message == 1 )
            alert("Thank you for your rating");                   
        }
    });     
}

function send_info(){
    var uname = $('#user_name').val(); 
    var umail = $('#user_mail').val();   
    var desc = $('#your_msg').val();       
    var datas = uname + ":" + umail + ":" + desc;
    
    if( umail == "" || uname == "" || desc == "" ){
            alert("Please, enter all fields .");
            return false;
    }
    else{                           
        $.ajax({
            type: 'POST',
            url: SITE+'engine.php?r='+$.random(1000),
            data: 'proc=send_info&data='+datas,
            success: function(message) {            
            }
        });   
        alert("Thank you for your feedback !");           
    
    }
}

function reset_scroll(){
	$.data($('.scrollable')[0], 'jScrollPaneImagesToLoad', null);
}

function refresh_widget1(){
	show_loading('widget1_output');
		$.ajax({
			type: 'GET',
			url: SITE+'engine.php?r='+$.random(1000),
			data: 'proc=wid1_update_refresh',
			success: function(message) {
			$('#widget1_output').html(message);
			show_success_message('Update successful');
			}
		});
}

function refresh_widget2(){
	show_success_message('Update successful');
	show_loading('widget2_output');
		$.ajax({
			type: 'GET',
			url: SITE+'engine.php?r='+$.random(1000),
			data: 'proc=wid2_update_refresh',
			success: function(message) {
			$('#widget2_output').html(message);
			show_success_message('Update successful');
			}
		});
}

function refresh_widget3(){
	show_success_message('Update successful');
	show_loading('widget3_output');
		$.ajax({
			type: 'GET',
			url: SITE+'engine.php?r='+$.random(1000),
			data: 'proc=wid3_update_refresh',
			success: function(message) {
			$('#widget3_output').html(message);
			show_success_message('Update successful');
			}
		});
}

function refresh_widgets(){
	refresh_widget1();
	refresh_widget2();
	refresh_widget3();
}

function widget_top_playlists(){
	$("#wdg_pl").css("display","block");
	$("#tabs").tabs("select", 2);
} 

function widget_top_colleges(str){	
	$("#wdg_uni").css("display","block");
	$("#tabs").tabs("select", 3);
}
function show_dialog(proc, title, width){
	var url = "engine.php?proc="+proc;
	var dialog = $("<div id='" + proc + "' style='display:hidden'></div>").appendTo("body");
	dialog.load(
		url,
		{},
		function(responseText, textStatus, XMLHttpRequest){
			dialog.dialog({
				width: width,
				title: title
			});
		}
	);
}
function show_registration_form(){
	show_dialog('register_form', 'New User Registration', 400);
}
function show_login_form(){
	show_dialog('login_form', 'Existing User Login', 350);
}

//ex-engine.js comes here

// functions for the api calls
function loadNewVideo(song_id) {
	//	update views counter
	var youtubeid = null;
	var title = null;
	var thumb = null;
    $.ajax({
    	type: 'GET',
    	url: 'engine.php?proc=add_view&r='+$.random(1000),
    	data: 'id='+song_id,
    	success: function(data) {
    		response = jQuery.parseJSON(data);
    		
    		youtubeid = response.youtube_id;
    		title = response.title;
    		thumb = response.thumb;
    		
    		$('#ads').hide('fast');      
    	    $('#tags').hide('fast');   
    	        
    	    $('#ytplayer').css("height", 238);
    	    $('#ytplayer').css("width", 420);         
    	        
    	    playing = true;

    	    ytplayer.loadVideoById(youtubeid);
    	    playVideo();
    	    
    	    $('#player').show('slow');
    		$('div#plist').find('a.link1').removeClass('active'); //removing the active song class from playlist songs
    		$('div#plist li#listItem_'+song_id).find('a.link1').addClass('active'); //adding the active class to the song in playlis, in case it's the song that is currently loading
    	    active_video = song_id;
    	    $('#picon').attr('src', 'images/pause.gif' );

    	    set_song_info(song_id, title, thumb);
    	}
    });
}

function select_list(list){
    show_loading('list'); 
    $.ajax({
        type: 'GET',
        url: 'ajx.php',
        data: 'action=select_list&list_id='+list,
        success: function(ajaxCevap) {
            // Override #result for result.
             $('#list').html(ajaxCevap); 
        }
    });        
}

function get_bar_pos(e, obj){  
	var evt=window.event || e ;
	
	var x = ( evt.pageX ) ? evt.pageX: evt.clientX;
	var o = getXY(obj);  
	
	clickX = x-(o.x-8);
	return clickX;
}

function set_bar( event, obj ){
   var pos = get_bar_pos( event, obj );  
   var focus = (ytplayer.getDuration() / 306) * pos; 
   seekTo( focus -3);
}
function getXY(obj) {
  var curleft = 0;
  var curtop = obj.offsetHeight + 5;
  var border;
  if (obj.offsetParent) {
    do {
      curleft += obj.offsetLeft;
      curtop += obj.offsetTop;
    }
    while (obj = obj.offsetParent);
  }
  else if (obj.x) {
    curleft += obj.x;
    curtop += obj.y;
  }
  return {'x': curleft, 'y': curtop};
}

function show_error_message(text, auto_close){
	auto_close = (auto_close === undefined) ? "false" : true;
	jQuery('body').showMessage({
		'thisMessage':		[text],
		'className':		'fail',
		'opacity':		95,
		'useEsc': false,
		'displayNavigation': true,
		'autoClose':		auto_close

	});
}
function showPopup(obj)
{
	$(obj).find("div").show();
}
function hidePopup(obj)
{
	$(obj).find("div").hide();
}
function show_success_message(text, auto_close){
	auto_close = (auto_close === undefined) ? "false" : true;
	jQuery('body').showMessage({
		'thisMessage':		[text],
		'className':		'success',
		'opacity':		95,
		'useEsc': false,
		'displayNavigation': true,
		'autoClose':		auto_close

	});
}
function logout(){
	$.ajax({
		type: 'POST',
       	url: 'engine.php',
        data: 'proc=logout',
        success: function() {
			window.location.reload();
		}
    });
}

/*
 * Chromeless player has no controls.
 */

// Update a particular HTML element with a new value
function updateHTML(elmId, value) {
  document.getElementById(elmId).innerHTML = value;
}

// This function is called when an error is thrown by the player
function onPlayerError(errorCode) {
//  alert("This video is currently unavailable."+active_video);
  document.getElementById('listItem_'+active_video).style.backgroundColor = '#fa7e52';
  next_video();
}

// This function is called when the player changes state
function onPlayerStateChange(newState) {
	if( newState == 0 ){
                
        next_video();
    }
  //updateHTML("playerState", newState);
}

// Display information about the current state of the player
function updatePlayerInfo() {
  // Also check that at least one function exists since when IE unloads the
  // page, it will destroy the SWF before clearing the interval.
  if(document.getElementById("player").style.display =='block')
  {
  if(ytplayer && ytplayer.getDuration) {
	  var gctime =ytplayer.getCurrentTime();
	  var width = ( ytplayer.getCurrentTime() * 306 ) / ytplayer.getDuration() ;
	  	//  alert(width+"=="+ytplayer.getDuration());

	    if( ytplayer.getCurrentTime() > 1 ){
	        $('#nav_sure').css("width", width );
	    }
		if(width > 304 && gctime>0)
		{
			next_video();
		}
//	  updateHTML("bytesloaded", ytplayer.getVideoBytesLoaded());
//	  updateHTML("bytestotal", getBytesTotal());
//	  updateHTML("videoduration", getDuration());
//	  updateHTML("videotime", getCurrentTime());
//	  updateHTML("startbytes", getStartBytes());
//	  updateHTML("volume", getVolume());
	  
	  
//    updateHTML("videoDuration", ytplayer.getDuration());
//    updateHTML("videoCurrentTime", ytplayer.getCurrentTime());
//    updateHTML("bytesTotal", ytplayer.getVideoBytesTotal());
//    updateHTML("startBytes", ytplayer.getVideoStartBytes());
//    updateHTML("bytesLoaded", ytplayer.getVideoBytesLoaded());
//    updateHTML("volume", ytplayer.getVolume());
  }
  }
}
function popitup(url) {
	newwindow=window.open(url,'name','height=320,width=480');
	if (window.focus) {newwindow.focus()}
	return false;
}

function setVolume(newVolume) {
  if (ytplayer) {
    ytplayer.setVolume(newVolume);
  }
}
// Allow the user to set the volume from 0-100
//function setVideoVolume() {
//  var volume = parseInt(document.getElementById("volumeSetting").value);
//  if(isNaN(volume) || volume < 0 || volume > 100) {
//    alert("Please enter a valid volume between 0 and 100.");
//  }
//  else if(ytplayer){
//    ytplayer.setVolume(volume);
//  }
//}

function playVideo() {
  if (ytplayer) {
    
	ytplayer.playVideo();
	
  }
}
function stopVideo() {
	if (ytplayer) {
		ytplayer.stopVideo();
	}
}

function pauseVideo() {
  if (ytplayer) {
    ytplayer.pauseVideo();
  }
}

function muteVideo() {
  if(ytplayer) {
    ytplayer.mute();
  }
}

function unMuteVideo() {
  if(ytplayer) {
    ytplayer.unMute();
  }
}
function seekTo(seconds) {
  if (ytplayer) {
    ytplayer.seekTo(seconds, true);
    playing = true;                
    $('#picon').attr('src', 'images/pause.gif' );    
  }
}
function play() {  
  if (ytplayer) {
    if( playing == false ){
        playing = true;                
        $('#picon').attr('src', 'images/pause.gif' );
        playVideo();
    }    
    else{
        playing = false;
        pauseVideo();
        $('#picon').attr('src', 'images/play_icon_player.gif' );
    }
  }  
}

// This function is automatically called by the player once it loads
function onYouTubePlayerReady(playerId) {
  ytplayer = document.getElementById("ytplayer");
  // This causes the updatePlayerInfo function to be called every 250ms to
  // get fresh data from the player
//  setInterval(updatePlayerInfo, 250);
  updatePlayerInfo();
  ytplayer.addEventListener("onStateChange", "onPlayerStateChange");
  ytplayer.addEventListener("onError", "onPlayerError");
  setVolume(75);
}

