/*
Author: Alien Cheung
Website: http://alien9.com
*/
/*
$('body').mousemove(function(e){
      $("#mask").css("background-position", 2000 + e.pageX+"px " + (-960 + e.pageY)+"px");
 });
*/
//$('.bg,#contentp').corner("10px");
//setInterval("colortext()",4000);
//setInterval("spectrum()",2000);
$(function(){
if($('#loading').css('display') == 'none'){
	$('#loading').css('left',function(){
		return ($(window).width() * 0.5) - 100;
	}).fadeIn('fast');
}
var w ='810px';
var w2 ='0px';
var w3 ='788px';
$('#scrollmain').css({
	left: w2,
	top: '41px'
 });
 $('#scrollcat').css({
	left: w3,
	top: '41px'
 });

 setTimeout(function(){
	$('#shadow,#footbar').fadeIn('slow').fadeTo('slow', 0.7);
	$('#rightpadtouchbg').fadeIn('slow').fadeTo('slow', 0.5);
 },1500);
  setTimeout(function(){
	$('#content').slideDown('slow');
	scrolling();
 },2000);
setInterval("spectrum()",1010);
//var originalc = $('.MenuBlock').find('a').not('.flash').css('color');

	

var hm = $('#content').height()/20;
$('#scrollmain').hide();
var c = ($(window).height() * 0.9) - 50;
var d = c + 20;
var scroll = c -53;
$('#content,#full,#shadow').css('height',c);
$('#pagination').css('top',d);
$('.down').css('top',scroll);
var oy2 = $('#content').height();
var x2 = '0px';
$('#pagination').css('left',x2);
if($(window).width() < 850){
	$('#content,#shadow').css({'left':'0px','margin-left':'5px'})
}else{
	$('#content,#shadow').css({'left':'50%','margin-left':'-400px'})
}

$(".ajax > img").css("width","64px");
updateUserCount();
setInterval("updateUserCount()", 60000);
var str=location.href;
var x= str.split('-')[0];
if(x.indexOf('gotopost')){
	x=str.split('-')[2];
	highlightli(x);
}
$('#menutree1').menuTree();
//=============================================================
$('#rightpadtouch').mouseenter(function(){
	$(this).fadeTo('slow',0.5);
	if ($('#rightpad').css('display') == 'none' ) {
		$('#rightpad').animate({width: 'show',opacity: 'show'},'slow');
	}
})
$('#rightpad').mouseleave(function(){
	$('#rightpadtouch').fadeTo('slow',1);
	$(this).animate({width: 'hide',opacity: 'hide'},'slow');
});

//=============================================================
$(".ajax > img").hover(function(){
	$(this).animate({width: '128px'}, 'slow');
}, function() {
	$(this).animate({width: '64px'}, 'slow');
});
//=============================================================
$(document).ajaxStart(function(){
	if($('#loading').css('display') == 'none'){
		$('#loading').css('left',function(){
			return ($(window).width() * 0.5) - 100;
		}).fadeIn('fast');
	}	
});
//=============================================================
$(document).ajaxComplete(function(){
	if($('#loading').css('display') == 'block'){
		$('#loading').fadeOut('slow');
	}
});
//=============================================================
$(document).ajaxError(function(){
	if($('#loading').css('display') == 'block'){
		$('#loading').html('Error...').fadeIn('slow');
	}	
});
//=============================================================
$('.close').click(function(){	
	$(this).parent().slideUp();
});
//=============================================================
$('.MenuBlock').mouseover(function() {
		$(this).find('a').css({ color: '#FF3399' },100);
		return false;
}).mouseout(function() {
		$(this).find('a').css({ color: '#0099FF'});
		return false;
});	
//=============================================================
$('.MenuBlock').click(function(){	
	if($(this).find('a').attr('rel') == 'x'){
		showdiv($(this).find('a').attr('title'));
	}else if($(this).find('a').attr('rel') == 'logout'){
		logout();
	}else if($(this).find('a').attr('rel') == 'showlogin'){
		showlogin();
	}else{
		location.href='#!'+$(this).find('a').attr('rel');
	}
});
//=============================================================
$('.MenuBlock > a').click(function(){
	return false;
});

//=============================================================
$('.up,.down').mouseover(function(){
	$(this).animate({
			opacity: 1
	});
}).mouseout(function(){
	$(this).animate({
			opacity: 0.1
	});
});
//=============================================================
$('.up').bind({
	mouseover :function(){
	var temp = $(this).parent().attr('id');
		up(temp);
	},
	mouseout :function(){
	var temp = $(this).parent().attr('id');
		clearup(temp);
	 }
});
//=============================================================
$('.down').bind({
	mouseover :function(){
		var temp = $(this).parent().attr('id');
		down(temp);
	},
	mouseout :function(){
		var temp = $(this).parent().attr('id');
		cleardown(temp);
	 }
});
//=============================================================
$(window).resize(function() {
	var ww = ($(window).width() * 0.9) - 150
	$('.out').css('width',ww);
	$('#yting').css('bottom','0px');
	scrolling();
	var c = ($(window).height() * 0.9) - 50;
	var d= c + 20
	var scroll = c -53;
	$('#pagination').css('top',d)
	$('.down').css('top',scroll);
	$('#content,#shadow').css('height',c);
	var x =  '810px';
	var x2 ='0px';
	var w2 ='0px';
	var w3 ='788px';
	var y = 10;
	var y1= 80;
	$('#pagination').css('left',x2);
	$('#scrollmain').css('left',w2);
	$('#scrollcat').css('left',w3);
	var g = $('#wrap').offset().left;
	if($(window).width() < 850){
		$('#content,#shadow').css({'left':'0px','margin-left':'5px'})
	}else{
		$('#content,#shadow').css({'left':'50%','margin-left':'-400px'})
	}
});
//=============================================================
/*
	$('#shadow').fadeTo('slow', 0.7, function() {
	 Animation complete.
	 });
*/
//=============================================================	
$(window).hashchange(function(){
	var hash = location.hash;
	// Iterate over all nav links, setting the "selected" class as-appropriate.

	if(hash != '' && hash != '#'){
		$('#pagination').hide();
	   var str1=location.href.split('#!')[1];
		str2=str1.split('-')[0];
		str3=str1.split('-')[1];
		str4=str1.split('-')[2];
		str5=str1.split('-')[3];
		if(str2=='goto2'){
			goto2(str3+'.php');
		}else if(str2=='goto'){
			if(str3 == 'blog'){
				str4=str4.toString();
				$.cookie('cat',str4);
				$.cookie('id','');
				$.cookie('page',1);
				goto1(str3+'.php');
				pagination();
			}else{
				var match = false;
				var key = ['about','horoscope','main','chatting','loading','addpost','editpost','delete','addcat','webmaster','youtube','privacypolicy'];
				for (var i in key) {
					if (str3 == key[i]){
						match = true;
					}
				}
				if(match == true){
					match = false;
					goto1(str3+'.php');
				}else{
					goto1('error.php');
				}
			}
		}else if(str2=='gotopost'){
			if($('#contentpleft').length <= 0){
				$("#forchange").load('blog_plain.php', function() {
				str4=str4.toString();
				$.cookie('id',str4);
				$.cookie('page',1);
				cat=str5.split('.')[0];
				$.cookie('cat',cat);
				gototitle2(str4);
				gotopost(str4);
				pagination(str4);
				});
			}
			else{
				str4=str4.toString();
				$.cookie('id',str4);
				$.cookie('page',1);
				cat=str5.split('.')[0];
				if($.cookie('cat') != cat){
					$.cookie('cat',cat);
					gototitle(str4);
					pagination(str4);
				}
				gotopost(str4);
				$('#pagination').show();
			}
		}else if(str2=='search'){
			$.cookie('page',1);
			var key= decodeURI(str3);
			$('#search').show();
			$('#searchblog').val(key);
			searchblog(key);
		}else if(str2=='searchdate'){
			$.cookie('page',1);
			var dd= str3+"-"+str4+"-"+str5;
			searchdate(dd);
		}else{
			goto1('error.php');
		}
	}else{
		var str=location.href.split('alien9.com/')[1];
		if(str == ''){
			location.href='#!goto-main-ajax-html';
		}
	}
});
$(window).trigger( 'hashchange' ); //trigger hashchange event in the first time.
refresh();

//=============================================================
 //end of window hashchange

$('#searchblogbtn').click(function() {
	if($('#searchblog').val() != ''){
		var key= encodeURI($('#searchblog').val());
		window.location="#!search-"+key+"-ajax.html";
	}else{
		alert('no keyword detected');
	}
});

$('#loginbtn').click(function() {
	$.post("process.php",
	{password : $('#password').val()},
	function(data){
		if(data == 'true'){
			$('#logintag').html('<span class="MenuWord"><a href="javascript:void(0)" rel="logout" title="Logout">L</a></span>');
			$('#login').fadeOut('slow');
			$.cookie('login',data);
			$('#panelbtn').load('panel.php');
		}else{
			var ip = data;
			// This part is for an alert box
			alert("System Hacked!\r\nYour IP address is "+ip+"\r\n please don't try again");
		}
	});
});

	/*
	if ($.cookie("URL") != null){
		url2=$.cookie("URL");
		setTimeout(function(){
			$.cookie("URL",null);
			goto1(url2);
		},300);
	}
	else{
		$("#forchange").hide();
		$("#forchange").fadeIn('slow');
	}
	*/
//*******end of Jquery runtime *******
});
//=============================================================
function scrolling(){
	var a = $('#content').height();
	var a2 = $('.scroll').height();
	var c2 = $('.scroll2').height();
	if (($('.scroll').height() + 100) > $('#content').height() ){
			$('#scrollmain').show();
	}else{
			$('#scrollmain').hide();
	}
	if (($('.scroll2').height() + 100) > $('#content').height()){
			$('#scrollcat').show();
	}else{
			$('#scrollcat').hide();
	}
	//$('.scroll').gWaveScrollPane();
}
//=============================================================
function yt(){
	$('a[href^="http://www.youtube.com"]').wrap('<li class="me"></div>');
	$('.me' ).wrapAll('<ul class="yt"></ul>');
	$('<div id="ytvideo"></div>').insertBefore('.yt');
	$(".yt").ytplaylist();
}
//=============================================================
function refresh(){
	scrolling();
	$('h1,a').each(function() {
	var original = $(this).css('color');
		$(this).mouseover(function() {
			if($('h1,a').is('.flash') == false){
				$(this).addClass('flash');
				$(this).animate({ color: '#FF3399' },500);
			}
		}).mouseout(function() {
			if($(this).is('.flash') == true){
				$(this).animate({ color: original },500).removeClass('flash');
			}
		});
	});
	$('a[href^="http"]').not('a[href^="http://alien9.com"]').click(function(){
		var text1 = "This will visit " + $(this).attr("href") + " , are you sure?";
		$(this).attr("target","_blank");
	});
	$('.ajax').not('a[href^="#!"]').not('a[href^="http"]').click(function() {
		strxx=$(this).attr("href");
		if(strxx.indexOf('#!') <0){
			ajaxed="http://alien9.com/#!"+$(this).attr("href");
		}else{
			ajaxed=$(this).attr("href");
		}
		$(this).attr("href",ajaxed);
		location.href=ajaxed;
	});
}
//=============================================================
function spectrum(){
/*
	var a = Math.floor(Math.random() * 250+1);
	var b = Math.floor(Math.random() * 250+1);
	var c='rgb('+a+',0,'+b+')';
	*/
	var x = Math.floor(Math.random() * 7);
	var i = new Array();
	i[0]="\"#FF0000\"";
	i[1]="\"#FFA500\"";
	i[2]="\"#FFFF00\"";
	i[3]="\"#00FFFF\"";
	i[4]="\"#0000FF\"";
	i[5]="\"#4B0082\"";
	i[6]="\"#FF3399\"";
	colornew = i[x];
	$("#rightpadtouchbg").animate({ backgroundColor: colornew }, 1000);
}
//=============================================================
function colortext(x){
	var x = Math.floor(Math.random() * 9);
	var i = new Array();
	i[0]="\"#FF0099\"";
	i[1]="\"#000000\"";
	i[2]="\"#90F\"";
	i[3]="\"#CCFF00\"";
	i[4]="\"#FFCC00\"";
	i[5]="\"#3366FF\"";
	i[6]="\"#FFFF33\"";
	i[7]="\"#F7EF3E\"";
	i[8]="\"#CDFF29\"";
	colornew = i[x];
	//alert (colornew);
	//x.css('color',colornew);
	x.animate({ color:  colornew }, 2000);
}
//=============================================================
function minmax(){
	if ($('#panel').css('width')=='158px'){
		$('#panelright').fadeOut('fast');
		$('#panel').css('width','10px');
		$('#minmax').html("<img src=\"images/left.gif\" />");
	}else{
		$('#panelright').fadeIn('slow');
		$('#panel').css('width','158px');
		$('#minmax').html("<img src=\"images/right.gif\" />");
	}
}
//=============================================================
function logout(){
	$.cookie('login','');
	$('#logintag').html('<span class="MenuWord"><a href="javascript:void(0)" rel="showlogin" title="Login">L</a></span>');
	$('#panelbtn').html('');
}
//=============================================================
function changebg(){
	bgimg= 'url(/img/newbg'+Math.floor(Math.random()*4+1) +'.png)';
	$('body').css('background-image',bgimg);
}

//=============================================================
function goto2(url2){
	 $.post(url2,{url : url2},
	function (data) {
		$("#forchange").html(data);
		
		//$('.bg,#contentp').corner("10px");
		highlightli('x');
		yt();
		$.cookie("ALIEN", null);
	});
}
//=============================================================
function goto1(url2){
	$.cookie("ALIEN", url2);
	$.post(url2, {url : url2},function (data) {
		$("#forchange").hide().html(data).fadeIn();
		highlightli('x');
		yt();
		setTimeout(function(){
			refresh();
		},401);
		$.cookie("ALIEN", null);
	});
}
//=============================================================
function gotopost(id2){
	$('.scroll').css('margin-top','0px');
	$.post("showpost.php",{id : id2},function (data) {
		$(".scroll").hide().html(data).animate({height: 'show',opacity: 'show'},'slow');
		highlightli(id2);
		setTimeout(function(){
			yt();
			refresh();
		},401);
	});
}
//=============================================================
function gototitle(id){
	url2 = 'showtitle2.php?id='+id;
	$('.scroll2').css('margin-top','0px');
	$.post(url2,function (data2) {
		$(".scroll2").hide().html(data2).fadeIn();
		setTimeout(function(){
			refresh();
		},401);
		highlightli(id);
	});
}
//=============================================================
function gototitle2(id){
	url2 = 'showtitle.php?id='+id;
	$.post(url2,function (data2) {
		$("#contentpleft").hide().html(data2).fadeIn();
		setTimeout(function(){
			refresh();
		},401);
		highlightli(id);
	});
}
//=============================================================
function searchblog(key){
	$.post("searchblog.php",{key : key},function (data) {
		$("#forchange").hide().html(data).fadeIn();
		refresh();
		document.title='search: ' + key + ' \- 異道';
	});
}
//=============================================================
function searchdate(dd){
	$.post("searchdate.php",{dd : dd},function (data) {
		$("#forchange").hide().html(data).fadeIn();
		setTimeout(function(){
			refresh();
		},401);
		document.title='date: ' + dd + ' \- 異道';
	});
}
//=============================================================
function checkdate(dd){
	$.post("checkdate.php",{dd : dd},function (data) {
		var str=data;
		if (str=="yes"){
			str = "#"+dd;
			$(str).addClass('haveentry');
		}
	});
}
//=============================================================
function pagination(id){
	url = 'page.php?id='+id;
	setTimeout(function(){
		$.post(url,function (data){
			$("#pagination").show();
			$("#pagination").html(data);
		});
	},300);
}
//=============================================================
function gotosearchtitle(id){
	if($('#contentpleft').length <= 0){
		$("#forchange").fadeOut('slow');
		url = 'searchblog.php';
	}
	$.ajax({
		url : url,
		success : function (data) {
			if($('#contentpleft').length <= 0){
				$("#forchange").fadeIn('slow');
				$("#forchange").html(data);
			}
		setTimeout(function(){
			refresh();
		},401);
		}
	});
}
//=============================================================
function cat(number){
	$.cookie('cat',number);
	$.cookie('page',1);
}
//=============================================================
function page(number){
	$.cookie('page',number);
	if($('#contentpleft').length <= 0){
		var key= $('#searchblog').val();
		setTimeout(function(){searchblog(key)},101);
	}else{
		setTimeout(function(){gototitle()},101);
		setTimeout(function(){pagination()},101);
	}
}
//=============================================================
function newpage(number){
	$.cookie('page',number);
	if($('#contentpleft').length <= 0){
		var key= $('#searchblog').val();
		setTimeout(function(){searchblog(key)},101);
	}else{
		setTimeout(function(){gototitle()},101);
		setTimeout(function(){pagination()},101);
	}
}
//=============================================================
function pagegoto(number,maxnum){
	if(number <= maxnum-0 && number > 0){
		$.cookie('page',number);
		if($('#contentpleft').length <= 0){
			var key= $('#searchblog').val();
			setTimeout(function(){searchblog(key)},101);
		}else{
			setTimeout(function(){gototitle()},101);
			setTimeout(function(){pagination()},101);
		}
	}else{
		alert('invalid');
	}
}
//=============================================================
function showcomments() {
	if($('#comments').css('display') == 'none'){
		$('.displayonce').fadeOut('slow');
		$('#comments').html('');
		$('#comments').fadeIn('slow');
		$('#comments').load('comments.php');
	}else{
		if(typeof  run!='undefined'){
			run=window.clearInterval(run)
		}
		$('#comments').fadeOut('slow');
	}
}
//=============================================================
function showshare() {
	if ($('#share').css('display')=='none'){
		$('.displayonce').fadeOut('slow');
		$('#share').fadeIn('slow');
		$('#share').load('share.php');
	}else{
		$('#share').fadeOut('slow');
	}
}
//=============================================================
function showlogin() {
	if ($('#login').css('display')=='none'){
		$('.displayonce').fadeOut('slow');
		$('#login').fadeIn('slow');
		$('#password').focus();
	}else{
		$('#login').fadeOut('slow');
	}
}
//=============================================================
function showsearch() {
	if ($('#search').css('display')=='none'){
		$('.displayonce').fadeOut('slow');
		$('#search').fadeIn('slow');
		$('#searchblog').focus();
	}else{
		$('#search').fadeOut('slow');
	}
}
//=============================================================
function showdiv(name){
	$('.displayonce').slideUp('fast');
	if ($('#'+name).css('display')==='none'){
		$('#'+name).slideDown('slow');
		if(name=='calendar'){
			$('#calIn').load('calendar.php');
		}
	}else{
		$('#'+name).slideUp('slow');
	}
}
//=============================================================
function showdivpure(name){
	if ($('#'+name).css('display')=='none'){
		$('#'+name).fadeIn('slow');
	}else{
		$('#'+name).fadeOut('slow');
	}
}
//=============================================================
function highlightli(id){
	if(id!='x'){
		$("img[id^='select'],img[id^='img']").remove();
		$("li[id^='title"+id+"']").prepend("<img src=\"images/right.gif\" id=\"selecttilte\""+id+"\" alt=\"title"+id+"\" /> ");
	}
	$("li[id^='title']").click(function() {
		$("img[id^='select'],img[id^='img']").remove();
		$(this).prepend("<img src=\"images/right.gif\" id=\"select"+$(this).attr('id')+"\" alt=\""+$(this).attr('id')+"\" /> ");
	});
	$("li[id^='title']").hover(function() {
		$("img[id^='img']").remove();
		if($(this).children("img[id^='select']").size() < 1){
			$(this).prepend("<img src=\"images/right.gif\" id=\"img"+$(this).attr('id')+"\" alt=\""+$(this).attr('id')+"\" /> ");
		}
	},function(){
		$("img[id^='img']").remove();
	});
}
//=============================================================
function updateUserCount() {
	$('#counter').load('counter.php');
}
//=============================================================
function up(x){
	if (x=='scrollmain'){
		moveup2 = setInterval("moveup('scrollmain')",10);
	}
	if (x=='scrollcat'){
		moveup2 = setInterval("moveup('scrollcat')",10);
	}
}
//=============================================================
function down(x){
	if (x=='scrollmain'){
		movedown2 = setInterval("movedown('scrollmain')",10);
	}
	if (x=='scrollcat'){
		movedown2 = setInterval("movedown('scrollcat')",10);
	}		
}
//=============================================================
function cleardown(){
	if(typeof movedown2!='undefined'){
		movedown2=window.clearInterval(movedown2)
	}
}
//=============================================================
function clearup(){
	if(typeof  moveup2!='undefined'){
		moveup2=window.clearInterval(moveup2)
	}
}
//=============================================================
function moveup(x){
	if(x=='scrollmain'){
		var y = $('.scroll').css('margin-top');
		if(y != undefined){
			y = y.split('px')[0];
			if(y<0){
				y=parseInt(y) + 6;
				y = y+"px"
				$('.scroll').css({
					"margin-top": y
				});
			}
		}
	}
	if(x=='scrollcat'){
		var y = $('.scroll2').css('margin-top');
		if(y != undefined){
			y = y.split('px')[0];
			if(y<0){
				y=parseInt(y) + 6;
				y = y+"px"
				$('.scroll2').css({
					"margin-top": y
				});
			}
		}
	}
}
//=============================================================
function movedown(x){
	if(x=='scrollmain'){
		var c = $('#content').height();
		var c2 = $('.scroll').height();
		var max= c - c2;
		var y = $('.scroll').css('margin-top');
		if(y != undefined){
		y = y.split('px')[0];
			if(y <= 0){		
				if (y > (max - 100)){	
					y = parseInt(y) - 6;
					y = y+"px";
					$('.scroll').css({
						"margin-top": y
					});
				}
			}
		}
	}
	if(x=='scrollcat'){
		var c = $('#content').height();
		var c2 = $('.scroll2').height();
		var max= c - c2;
		var y = $('.scroll2').css('margin-top');
		if(y != undefined){
			y = y.split('px')[0];
			if(y<=0){		
				if (y > (max - 100)){	
					y=parseInt(y) - 6;
					y = y+"px";
					$('.scroll2').css({
						"margin-top": y
					});
				}
			}
		}
	}
}
