$(document).ready(function(){

countUsersOnline();

$("ul.dropdown li").hover(function(){
$(this).addClass("hover");
//$('ul:first',this).css('visibility', 'visible');
$('ul:first',this).fadeIn(500);
}, function(){
$(this).removeClass("hover");
//$('ul:first',this).css('visibility', 'hidden');
$('ul:first',this).fadeOut(500);
});

$("ul.dropdown li ul li:has(ul)").find("a:first").append(" &raquo; ");

$("#heart").hover(function(){
$(this).animate({opacity:1},500);
//$(this).fadeIn(500);
}, function(){
$(this).animate({opacity:0.1},500);
//$(this).fadeOut(500);
});

$('ul.fbg_ul li a').tipsy({gravity: 's',fade: true});


$.jTwitter('JBaronGr', 10, function(data){
$('#posts').empty();
$.each(data, function(i, post){
        $('#posts').append(
        '<div class="post"><span style="color:yellow">'+post.user.name+'</span>&nbsp;<i style="color:#050;">'+parseTwitterDate(post.created_at)+'</i>'
        +' <div style="color:#fff">'
        +    post.text
        +' </div>'

        +'</div>'
        );
});

});


function parseTwitterDate(text) {
var newtext = text.replace(/(\+\S+) (.*)/, '$2 $1')
var date = new Date(Date.parse(newtext)).toLocaleDateString();
//var time = new Date(Date.parse(newtext)).toLocaleTimeString();
//return date +'  ' + time;
return date;
}

$('.fbg_ul').children().hover(function() {$(this).siblings().stop().fadeTo(500,0.5);
}, function() {$(this).siblings().stop().fadeTo(500,1);});

$("ul.fbg_ul li a.open_in_new_window[href]").live("click",function(event){window.open($(this).attr("href"));event.preventDefault();});

$("p a.open_in_new_window[href]").live("click",function(event){window.open($(this).attr("href"));event.preventDefault();});

$("#paypal a.open_in_new_window[href]").live("click",function(event){window.open($(this).attr("href"));event.preventDefault();});

$("#chatopen a.open_in_new_window[href]").live("click",function(event){window.open($(this).attr("href"));event.preventDefault();});

$('div.accordionButton').click(function(){$(this).next("div.accordionContent").slideDown(500).siblings("div.accordionContent").slideUp("slow");});

$("#share").css({ height:$(window).height(), left: "2%", opacity: 1 } );
$("#share .sign").animate({ top: ($(window).height()/2)-($(".sign").height()/2), left: "2.5%" }, 100,function(){} );

$("#share .johnole").animate({ top: ($(window).height()/2)-($("#share .johnole").height()+$(".sign").height()/2), left: "2.5%" }, 100,function(){} );

$("#close_pageitem").click(function(){
 $(".pageitem_container").fadeOut( 500 );
scrollWin('body',20);
});


var triggerchatSign = $("div.chat_sign").overlay({target:'#version', expose: {color: '#000',loadSpeed: 200,opacity: 0.9},
onLoad: function(event) { $("#chatsystemdata").load('chat/chatsystem.php',function(){$("#msgauthor").attr({"disabled":""}); clearTimeout(countuo); });},
onClose: function(event){logMeOut(); clearTimeout(updmsg); countUsersOnline();},
closeOnClick: false});

var triggerInfobox = $("div.infobox").overlay({target:'#version', expose: {color: '#000',loadSpeed: 200,opacity: 0.9},
onLoad: function(event) { $("#chatsystemdata").load('chat/chatsystem.php',function(){$("#msgauthor").attr({"disabled":""}); clearTimeout(countuo); });},
onClose: function(event){logMeOut(); clearTimeout(updmsg); countUsersOnline();},
closeOnClick: false});

$(window).scroll(function(){
$("#share").css({ height:$(window).height() } );
 //$("#share").animate({ top: ($(window).height()/2)-($("#share").height()/2), left: "2%", opacity: 0.5 }, 100,function(){} );
$(".sign").animate({ top: ($(window).height()/2)-($(".sign").height()/2), left: "2.5%" }, 100,function(){} );
}); 

$('.sign').toggle(function() {
$("#wrapper_accordion").fadeIn(500);
$("#share").animate({ top: ($(window).height()/2)-($("#share").height()/2), left: "0%",width:$(window).width(),opacity:1 }, 500
,function(){$(".c2").css({background:"#050505"});
//$(".").fadeIn(500);
$("#share .johnole").fadeOut(500);
//$(".note").animate({ top:  $(".wrapper").offset().top, left: $(".col1wrapper").offset().left,width:"300px" }, 100,function(){} );
} );
}, function() {
$(".c2").css({background:"#050505 url(images/body_background_dark.png) left top"});
$("#wrapper_accordion").fadeOut(500);
$("#share").animate({ top: ($(window).height()/2)-($("#share").height()/2), left: "2%",width:"50px" }, 500,function(){
$("#share .johnole").fadeIn(500);
} );
});

jQuery.fn.shake = function(intShakes /*Amount of shakes*/, intDistance /*Shake distance*/, intDuration /*Time duration*/) {
this.each(function() {
$(this).css({position:"relative"});
var origLeft = parseInt($(this).css("left"), 10)
for (var x=1; x<=intShakes; x++) {
$(this).animate({left:origLeft-(intDistance*-1)}, (((intDuration/intShakes)/4)))
.animate({left:origLeft+intDistance}, ((intDuration/intShakes)/2))
.animate({left:origLeft}, (((intDuration/intShakes)/4)));
}
});
return this;
};

$('#addbutton').attr("disabled", false);
addComment=function(){

$("#nameError").html('');
$("#emailError").html('');
var cname=$("#name").val();
var cemail=$("#email").val();
var error=false;
if(!notEmpty(cname)){$('#nameError').html(' Type Something'); error=true;}
if(!notEmpty(cemail)){$('#emailError').html(' Type Something'); error=true;}
if(!error){
if(cname.length>20) {$('#nameError').html(' Name must be less than 20 characters'); error=true;}
if(!validateName(cname)) {$('#nameError').html(' Non acceptable characters'); error=true;}
if(cemail.length>50) {$('#emailError').html(' Email must be less than 50 characters'); error=true;}
if(!validateEmail(cemail)){$('#emailError').html(' Non acceptable email'); error=true;} 
}
if(!error){
$('#addbutton').attr("disabled", true);
$.post("score.handler.php",{
		score_score: $("#ascore").html(),
		score_ahrs: $("#ahrs").html(),
		score_amins: $("#amins").html(),
		score_asecs: $("#asecs").html(),
		score_game: $("#gametype").html(),
		score_name: cname,
		score_email: cemail,
	}, function(msg) {
$('#addbutton').attr("disabled", false);
if(msg){
$("#addguidepanel").slideToggle("slow", function() { });
$("#highScoreTable").load('scoreTable.php');
$("#name").val('');
$("#email").val('');
$("#nameError").html('');
$("#emailError").html('');
}else
{
$("#addguidepanel").html('something went wrong');

}
});
}
}
});


function scrollWin(element,offset){
if(element=='body') $('html, body').animate({scrollTop:0-offset}, 800);
else $('html, body').animate({scrollTop:$(element).offset().top-($(window).height()-$(element).height())/2-offset}, 800);
}


function countUsersOnline(){
$('#countusersonline').load('chat/usercount.php');
countuo=setTimeout('countUsersOnline()', 2000);
}
//validation functions

validateName=function(string){
if(!string.match(/[^\w \xC0-\xFF]/g)) return true;
else return false;
}

validateEmail=function(string){
if(string.match(/^(\w|\.|-)+?@(\w|-)+?\.\w{2,4}($|\.\w{2,4})$/gim)) return true;
else return false;
}

trimComment=function(string){

return string.match(/^\s*|\s*$/gm);

}
validateComment=function(string){
//if(!string.match(/\/\*[\s\S]*?\*\//gm))
 return true;
//else return false;
}

function notEmpty(elem){
	if(elem.length == 0){
		return false;
	}
	return true;
}

function isNumeric(elem){
	var numericExpression = /^[0-9]+$/;
	if(elem.value.match(numericExpression)){
		return true;
	}else{
		return false;
	}
}

function isAlphabet(elem){
	var alphaExp = /^[a-zA-Z]+$/;
	if(elem.value.match(alphaExp)){
		return true;
	}else{
		return false;
	}
}

function isAlphanumeric(val){
	var alphaExp = /^[0-9a-zA-Z]+$/;
	if(val.match(alphaExp)){
		return true;
	}else{
		return false;
	}
}

function lengthRestriction(val, min, max){
	if(val.length >= min && val.length <= max){
		return true;
	}else{
		return false;
	}
}


