var store=0;
var the_album_id=0;
function show_page(cur_page_id,hash_search,from){	
	var hash;
	var hash_title;
	$("#navigation ul li").each(function(index) { 
			$(this).removeClass("selec");			
		});

	switch(cur_page_id){
			case 0:
				hash="/playing";
				hash_title="Now Playing";
				$(".now_playing").addClass("selec");
				break;
			case 1:
				hash="/beam_collection";
				hash_title="My Beam Collection";
				$(".collection").addClass("selec");
				break;
			case 2:
				hash="/get_beam";
				hash_title="Use Beam Ticket";
				$(".ticket").addClass("selec");
				break;
			case 3:
				hash="/store";
				hash_title="Store";
				$(".store").addClass("selec");
				break;
			case 5:
				hash="/what_is_beam";
				hash_title="What's Beam ?";
				$(".what_is_beam").addClass("selec");
				break;
			case 6:
				hash="/help";
				hash_title="Help";
				$(".help").addClass("selec");
				break;	
			case 7:
				hash="/home";
				hash_title="Home";
				$(".home").addClass("selec");
				break;
			case 8:
				hash="/mobile";
				hash_title="Mobile";
				break;
			case 9:
				hash="/contact";
				hash_title="Contact Us";
				break;
			case 10:
				hash="/desktop";
				hash_title="Desktop Application";
				break;		
		}
	if(window.location.hash.substring(1)!=hash+"/"){		
		window.location.hash = hash+"/";
		return;
	}
	$.get("/ajax_requests/show_page.php",{page_id:cur_page_id,from_hash_search:hash_search},function(data){
    	
		//window.location.hash = hash+"/";
		document.title="Beam: "+hash_title;
		$('#main_ajax').html(data);		
	});
}

function check_unlock(kk){
  $.post("/ajax_requests/check_code.php", $('#'+kk).serialize(),function(data){
	   if(data==1){
			//goto Albums 			
			 store=0;
			 var f_data={from:2};
			 $.colorbox({href:'/include/login.php',data: f_data});
	   }
	   if(data==0){
			apprise('Invalid Or Expired Code. Please check again your code.');   
	   }
   	   if(data==2){
			store_to_cloud();
	   }
	   if(data==3){
			$.colorbox({href:'/ajax_requests/golden_deal/golden_deal.php'});  
	   }
   }
 );
}


function log_in_to_heedia(){
	$.post("/ajax_requests/validate_heedia_member.php", $('#heedia_member').serialize(),function(data){				
		if(data==1){
			apprise('Wrong username and password. Please try again.'); 
		}		
		if(data==0){			
			if(store==0){
				show_page(1,0);
				//window.location.hash = "/beam_collection/";
			}
			else
			{
			   buy_album(the_album_id);
			   store=0;
			}
	   		$('#fb_connect').load('/ajax_requests/check_facebook.php');
			$.colorbox.close();
		}
	}
 	);
}

function register_from_heedia(){
	$('#continueButton').attr("disabled", true);
	$('.loading').show();	
	$.colorbox.resize();
	$.post("/ajax_requests/register_from_heedia.php", $('#heedia_fields').serialize(),function(data){		
		$('#continueButton').attr("disabled", false);
		$('.loading').hide();
		if(data==1){			
			apprise('The password field must match with the retype password.'); 
		}
		if(data==2){
			apprise('Please enter a valid email.'); 
		}
		if(data==3){
			apprise('You are already member on heedia.com'); 
		}
		if(data==4){
			apprise('Please use only latin characters on password field.'); 
		}
		if(data==0){			
			if(store==0){
				show_page(1,0);
				//window.location.hash = "/beam_collection/";
			}
			else
			{
			   buy_album(the_album_id);
			   store=0;
			}
	   		$('#fb_connect').load('/ajax_requests/check_facebook.php');
			$.colorbox.close();
		}
	}
 	);
}

function store_to_cloud(){
	$.colorbox.close();
	//store sto session
	 $.get("/ajax_requests/store_to_cloud.php",{from_store:0},function(data){
	   show_page(1,0);
   }
 );
	
}


function store_user(x,z){	
	$.colorbox.close();
	
	//store sto session
	 $.get("/ajax_requests/store_user.php", {from:x,remember:z},function(data){	   
	   if(store==0){
		 show_page(1,0);
	   }
	   else
	   {
		   buy_album(the_album_id);
		   store=0;
	   }
	   $('#fb_connect').load('/ajax_requests/check_facebook.php');
   }
 );	
}


function show_login(){
	store=0;
	var f_data={from:0};
	$.colorbox({href:'/include/login.php',data:f_data});
}

function logout(){
	$.get("/ajax_requests/log_out.php",{},
		function(data){	
			window.location='/';
		}
	);	
}


function show_heedia_register(){
	$('.no_facebook').show();	
	$.colorbox.resize();
}

function show_heedia_member(){
	$('.already_member').show();	
	$.colorbox.resize();
}

function show_store_id(store_id,acronym,from_hash){	
	if(from_hash==0){
		$.post("/ajax_requests/pages/include_store/current_store.php", {sub_id:store_id},function(data){
			$('#main_ajax').html(data);
			window.location.hash = "/channel/"+acronym+"/";
			document.title="Beam Store :: Category "+acronym; 		
		 }
		);
	}
	else
	{
		$.post("/ajax_requests/pages/include_store/current_store.php", {seo_category_id:store_id},function(data){
			$('#main_ajax').html(data);
			window.location.hash = "/channel/"+acronym+"/";
			document.title="Beam Store :: Category "+acronym; 		
		 }
		);				
	}
}

function show_details(cur_album_id,acronym,from_hash){
	$.get("/ajax_requests/pages/include_store/show_details.php",{album_id:cur_album_id,_from_hash:from_hash},
	function(data){	
		$('#main_ajax').html(data);
		window.location.hash = "/"+acronym+"/";
		document.title="Beam Store :: Album "+acronym;  
	}
	);
}


function get_it_free(cur_album_id){
	$.post("/ajax_requests/check_state.php",{album_id:cur_album_id},
	function(data){		
		if(data==2){
			apprise('Data violation');
		}
		if(data==0){
			//forma gia login
			 store=0;
			 var f_data={from:3};
			 $.colorbox({href:'/include/login.php',data:f_data});
		}
		if(data==1){
			//store sto cloud
			apprise('Add this to your Beam Spot?', {'verify':true}, 
			function(r)
			{
			if(r)
				{ 
				// user clicked 'Yes'
						$.get("/ajax_requests/store_to_cloud.php",{from_store:1},
						function(data){						   
						   apprise('Beam successfully added to your Beam Spot.');
						  // if($.colorbox){
							   $.colorbox.close();
						   //}
						   show_page(1,0);
						}
						);
				}
			});						
		}	//end if data=1	
   	}
	);
}

function buy_album(cur_album_id){
	$.get("/ajax_requests/check_user_state.php",
	function(data){		
		if(data==0){
			//apprise('You have to be logged in to purchase a Beam Album. Please login using the facebook login button.');
			the_album_id=cur_album_id;
			store=1;
			var f_data={from:0};
			$.colorbox({href:'/include/login.php',data:f_data});
		}
		else
		{
				//window.location.hash = "/purchase/"+cur_album_id+"/";
				$.post("/ajax_requests/pages/include_store/buy_album.php",{album_id:cur_album_id},function(data){
					$('#main_ajax').html(data); 
				});
		}
	});
}

function add_to_playlist(_pointer,_album_id){
	
	var f_data={pointer:_pointer};
	$.colorbox({href:'/ajax_requests/add_to_playlist.php',data:f_data});
}

function create_new_playlist(){
	$('#has_playlist_li').removeClass("active");
	$('#has_playlist').hide();
	$('#new_playlist_li').addClass("active");
	$('#new_playlist').show(); 
	$.colorbox.resize();
}

function add_to_prev_playlist(){
	$('#new_playlist_li').removeClass("active");
	$('#new_playlist').hide(); 
	$('#has_playlist_li').addClass("active");
	$('#has_playlist').show(); 
	$.colorbox.resize();
}

function create_new_playlist_final(){
	$.post("/ajax_requests/add_to_playlist_script.php",$('#new_playlist_form').serialize(),function(data){
			if(data==1){
				apprise("Title cannot be empty.");
			}
			if(data==0){
				$.colorbox.close();
				apprise("Track succesfully added to your playlist.");
			}
	 });
}

function has_playlist_final(){
	$.post("/ajax_requests/add_to_playlist_script.php",$('#has_playlist_form').serialize(),function(data){
			if(data==1){
				apprise("Track is already on this playlist.");
			}
			if(data==0){
				$.colorbox.close();
				apprise("Track succesfully added to your playlist.");
			}
	 });
}

function remove_from_playlist(pointer_id,playlist_id){
	apprise('Are you sure you want to remove this track from your playlist?', {'verify':true}, 
			function(r)
			{
			if(r)
				{
					$.post("/ajax_requests/remove_playlist.php",{pid:pointer_id,playlist:playlist_id},function(data){
							apprise("Track succesfully removed from your playlist.");
							if(data==0){								
								play_album(playlist_id,0,0,1);
								show_page(0,0);
							}
							if(data==1){
								show_page(1,0);
							}
					 });
				}
			});							   	
}

function hide_tpa(){
	$('#tpa_message').hide(); 	
}

