﻿//On load page, init the timer which check if the there are anchor changes each 300 ms
$().ready(function(){
	setInterval("checkAnchor()", 300);
});
var currentAnchor = null;
//Function which chek if there are anchor changes, if there are, sends the ajax petition
function checkAnchor(){
	//Check if it has changes
	if(currentAnchor != document.location.hash){
		currentAnchor = document.location.hash;
		//alert('currentAnchor is: ' + currentAnchor);
		//if there is not anchor, the loads the default section
		if(!currentAnchor){
			var currentUrl = document.location.href.split("?")[1];
			if(currentUrl == undefined){
				var section = "home";
				var query = "section=" + section;
				//var queryaddthis = section;
			}
			else{
				if(currentUrl.indexOf('&') == -1 && currentUrl.indexOf('/') == -1){
					var splits = currentUrl.split('.html');
					var section = splits[0];
					//section = section.replace("=", "");
					var query = "section=" + section;
					//alert('query is: ' + query);
				}
				else
				{
					if(currentUrl.indexOf('&') == -1){
						var splits = currentUrl.split('/');
						var section = splits[0];
						//section = section.replace("=", "");
						delete splits[0];
						if(section == 'play'){
							var params = '&id=' + splits[1] + '&song=' + splits[2];
						}
						else if(section == 'listen' || section == 'news' || section == 'hot' || section == 'favor'){
							var params = '&page=' + splits[1] + '&list=' + splits[2];
						}
						else if(section == 'theme' && splits[3] != undefined){
							var params = '&in=' + section + '&cat=' + splits[1] + '&page=' + splits[2] + '&keyword=' + splits[3];
							section = 'search';
						}
						else if(section == 'singer' && splits[3] != undefined){
							var params = '&in=' + section + '&sid=' + splits[1] + '&page=' + splits[2] + '&keyword=' + splits[3];
							section = 'search';
						}
						else{
							var params = '&in=' + section + '&page=' + splits[1] + '&keyword=' + splits[2];
							if(splits[2] != 'ca-si.html'){
								section = 'search';
							}
						}
					}
					else{
						var splits = currentUrl.split('&');
						var section = splits[0];
						//section = section.replace("=", "");
						delete splits[0];
						var params = splits.join('&');
					}
					var query = "section=" + section + params;
					//alert('query is: ' + query);
				}
			}
		}
		else
		{
			//Creates the  string callback. This converts the url URL/#main&id=2 in URL/?section=main&id=2
			if(currentAnchor.substring(1).indexOf('/') == -1){
				var splits = currentAnchor.substring(1).split('.html');
				var section = splits[0];
				//section = section.replace("=", "");
				var query = "section=" + section;
			}
			else
			{
				var splits = currentAnchor.substring(1).split('/');
				//Get the section
				var section = splits[0];
				//section = section.replace("=", "");
				delete splits[0];
				//Create the params string
				//alert('section is: ' + section);
				if(section == 'play'){
					var params = '&id=' + splits[1] + '&song=' + splits[2];
				}
				else if(section == 'listen' || section == 'news' || section == 'hot' || section == 'favor'){
					var params = '&page=' + splits[1] + '&list=' + splits[2];
				}
				else if(section == 'theme' && splits[3] != undefined){
					var params = '&in=' + section + '&cat=' + splits[1] + '&page=' + splits[2] + '&keyword=' + splits[3];
					section = 'search';
				}
				else if(section == 'singer' && splits[3] != undefined){
					var params = '&in=' + section + '&sid=' + splits[1] + '&page=' + splits[2] + '&keyword=' + splits[3];
					section = 'search';
				}
				else{
					var params = '&in=' + section + '&page=' + splits[1] + '&keyword=' + splits[2];
					if(splits[2] != 'ca-si.html'){
						section = 'search';
					}
				}
				var query = "section=" + section + encodeURI(params);
				//alert('query is: ' + query);
			}
		}
		switch(section)
		{
		case 'listen':
			$("#loading-image").show();
			$.get("listenssong.asp",query, function(data){
				$("#listenlist").html(data);
				$("#loading-image").fadeOut('slow', function() {
					//$("#loading-image").hide();
				});
			});
			if(query.indexOf('&id=') == -1){
				document.getElementById('listenlist').scrollIntoView(true);
			}
			break;
		case 'news':
			$("#loading-image").show();
			$.get("newssong.asp",query, function(data){
				$("#newslist").html(data);
				$("#loading-image").fadeOut('slow', function() {
					//$("#loading-image").hide();
				});
			});
			if(query.indexOf('&id=') == -1){
				document.getElementById('newslist').scrollIntoView(true);
			}
			break;
		case 'hot':
			$("#loading-image").show();
			$.get("hotssong.asp",query, function(data){
				$("#hotlist").html(data);
				$("#loading-image").fadeOut('slow', function() {
					//$("#loading-image").hide();
				});
			});
			if(query.indexOf('&id=') == -1){
				document.getElementById('hotlist').scrollIntoView(true);
			}
			break;
		case 'favor':
			$("#loading-image").show();
			$.get("favorsong.asp",query, function(data){
				$("#favorlist").html(data);
				$("#loading-image").fadeOut('slow', function() {
					//$("#loading-image").hide();
				});
			});
			if(query.indexOf('&id=') == -1){
				document.getElementById('favorlist').scrollIntoView(true);
			}
			break;
		case 'search':
			$("#loading-image").show();
			$.get("searchresults.asp",query, function(data){
				$("#searchresult").html(data);
				$("#loading-image").fadeOut('slow', function() {
					//$("#loading-image").hide();
				});
			});
			if(query.indexOf('&id=') == -1){
				document.getElementById('searchresult').scrollIntoView(true);
			}
			break;
		case 'singer':
			$("#loading-image").show();
			$.get("singerslist.asp",query, function(data){
				$("#singers").html(data);
				$("#loading-image").fadeOut('slow', function() {
					//$("#loading-image").hide();
				});
			});
			document.getElementById('singers').scrollIntoView(true);
			break;
		case 'theme':
			$("#loading-image").show();
			$.get("themeslist.asp",query, function(data){
				$("#categories").html(data);
				$("#loading-image").fadeOut('slow', function() {
					//$("#loading-image").hide();
				});
			});
			document.getElementById('categories').scrollIntoView(true);
			break;
		case 'intro':
			$("#loading-image").show();
			$.get("introduction.asp",query, function(data){
				$("#searchresult").html(data);
				$("#loading-image").fadeOut('slow', function() {
					//$("#loading-image").hide();
				});
			});
			document.getElementById('searchresult').scrollIntoView(true);
			break;
		case 'guide':
			$("#loading-image").show();
			$.get("guideservice.asp",query, function(data){
				$("#searchresult").html(data);
				$("#loading-image").fadeOut('slow', function() {
					//$("#loading-image").hide();
				});
			});
			document.getElementById('searchresult').scrollIntoView(true);
			break;
		case 'register':
			$("#loading-image").show();
			$.get("nameregister.asp",query, function(data){
				$("#searchresult").html(data);
				$("#loading-image").fadeOut('slow', function() {
					//$("#loading-image").hide();
				});
			});
			document.getElementById('searchresult').scrollIntoView(true);
			break;
		case 'charges':
			$("#loading-image").show();
			$.get("charges.asp",query, function(data){
				$("#searchresult").html(data);
				$("#loading-image").fadeOut('slow', function() {
					//$("#loading-image").hide();
				});
			});
			document.getElementById('searchresult').scrollIntoView(true);
			break;
		case 'agree':
			$("#loading-image").show();
			$.get("agreements.asp",query, function(data){
				$("#searchresult").html(data);
				$("#loading-image").fadeOut('slow', function() {
					//$("#loading-image").hide();
				});
			});
			document.getElementById('searchresult').scrollIntoView(true);
			break;
		case 'contact':
			$("#loading-image").show();
			$.get("contactus.asp",query, function(data){
				$("#searchresult").html(data);
				$("#loading-image").fadeOut('slow', function() {
					//$("#loading-image").hide();
				});
			});
			document.getElementById('searchresult').scrollIntoView(true);
			break;
		case 'play':
			$("#loading-image").show();
			$.get("showplayer.asp",query, function(data){
				$("#divPlayer").html(data);
				$("#loading-image").fadeOut('slow', function() {
					//$("#loading-image").hide();
				});
			});
			//document.getElementById('divPlayer').scrollIntoView(true);
			break;
		default:
			$("#loading-image").fadeOut('slow', function() {
				//$("#loading-image").hide();
			});
			/*
			$("#loading-image").show();
			$.get("includes/inc_singers.asp",query, function(data){
				$("#singers").html(data);
				$("#loading-image").hide();
			});
			$("#loading-image").show();
			$.get("newssong.asp",query, function(data){
				$("#news").html(data);
				$("#loading-image").hide();
			});
			*/
		}
	}
}

$(function() {
    function updateUserCount() {
        $("#userCount").load("countingpage.asp");
    }
    setInterval(updateUserCount, 15000);
});
$(function() {
    function updateUserOnline() {
        $("#userSupport").load("useronline.asp");
        //alert('Update OK');
    }
    setInterval(updateUserOnline, 20000);
});

function centerThis(div) { 
	var winH = $(window).height(); 
	var winW = $(window).width(); 
	var centerDiv = $('#' + div); 
	centerDiv.css('top', winH/2-centerDiv.height()/2); 
	centerDiv.css('left', winW/2-centerDiv.width()/2); 
} 
//centerThis('loading-image'); 
function changeLang(lang)
{
	currentUrl = document.location.href;
	if(!currentAnchor)
		document.location.href = currentUrl + '#home&lang=' + lang;
	else
	{
		if(currentUrl.indexOf('&lang=')==-1)
			document.location.href = currentUrl + '&lang=' + lang;
		else
		{
			var newUrl = currentUrl.split('&lang=');
			document.location.href = newUrl[0] + '&lang=' + lang;
		}
	}
}


