// ---------------------------------------------------------
//  INIT
// ---------------------------------------------------------
function initCount(){
		startCount()
		 
}

function checkContentEditable() {
	 if(ReadCookie('contentEditable') == 'true') {
		window.adminallowed = 1
		show_ViewerAdminTools();
	} else {
		window.adminallowed = 0;
		SetCookie("contentEditable", "false", 0);
	}
}




function bindClickToEnter(){
	var lettrsp;
  if($.browser.mozilla){
  	lettrsp = '0.5em';
  } else {
  	lettrsp = '0.2em';
  }
	$('#splashPic img').mouseover(function(){
		$('#splashTitle').animate({ color: '#ffffff' }, 150, '', function(){
			$('#splashTitle').html("click image to view selected work");
				$('#splashTitle').animate({ color: '#585353', 'letterSpacing': lettrsp}, 500);
		});
	});
	$('#splashPic img').mouseout(function(){
		$('#splashTitle').animate({ color: '#ffffff' }, 100, '', function(){
			$('#splashTitle').text(window.config.shortTitle);
				$('#splashTitle').animate({ color: '#585353', 'letterSpacing':'.2em'}, 500);
		});
	});
}





function loginBoxRemove() {
		$('#login').css("display", "none");
		$('#loginLink').css("display", "block");
		$('body').unbind('mousedown')
}

function logoutLink(){
	$('#login, #loginLink').css("display", "none");
	$('#logoutLink').css("display", "block")
		.unbind("click").click(function(){
			window.login = false;
			SetCookie("login", "false", 0);
			$('.noshow').css("display", "none");
			$('#logoutLink').css("display", "none");
			$('#loginLink').css("display", "block");
			window.adminallowed = 0;
			SetCookie("contentEditable", "false", 0);
			$("#thumbs").sortable("destroy");
			//alert('logged out')
			hide_ViewerAdminTools();
			window.tooltips = false;
			$(".tooltip").remove();
		});
}

//------------------------------------------------------------
// LOGIN
//------------------------------------------------------------

function loginInit() {
		checklogin();
		$('#goButton').click(function(){
			checkPassword();
		});
		$("body").keypress(function(e){
			if (e.keyCode == 13) {
				checkPassword();
			}
		});
}

function checkPassword() {
	var password = $('#password').val();
	$.getJSON('php/checkpass.php',
		{"pass":password},
		function(data) {
			if (data.status == "user") {
				passSuccess();
			}
			if (data.status == "admin") {
				passSuccess();
				SetCookie("contentEditable", "true", 1);
				window.adminallowed = 1;
				initReorder()
				show_ViewerAdminTools();
			}
			if (data.status == "failed") {
				$('#login input').effect("shake", { times:4, distance: 2 }, 30);
				$('#loginInstructions').text("Paswword Incorrect");				
			}		  
	});
}



function passSuccess() {
	window.login = true;
	$('.noshow').css({"display":"block"})
	loginBoxRemove();
	logoutLink();
	SetCookie("login", "true", 1);
}

function checklogin() {
	if(ReadCookie('login') == 'true') {
		window.login = true;
		logoutLink();
	} else {
		window.login = false;
	}
}


// ---------------------------------------------------------
//  EMAIL
// ---------------------------------------------------------

function generate_address( username ) {
        var domain = "eduardopaulo.com";
        var atsign = "&#64;";
        var addr = username + atsign + domain;
        document.write( 
          "<" + "a" + " " + "href=" + "mail" + "to:" + addr + " class='bodySmallLettersp'"+">" +
          addr +
          "<\/a>");
      }


function countdown () {
	if (count>1) {
		count = count-1;
		draw(count);
	} else {
	window.clearInterval(mytimer);
		getAddy();
	}
}

function startCount () {
	count = 40;
	mytimer = window.setInterval('countdown ()', 40);	
}

function draw(content) {
	var message = "Please wait... "
	var paddedCount = pad(content,2);
	$('#addyDiv').html(message + paddedCount);
}
	

function getAddy() {
	var ajaxObject = $.getJSON('php/addy.php','',function(data){
		$("#addyDiv").fadeOut(200, function () {
		$('#addyDiv').html(data);
		$("#addyDiv").fadeIn(1300);
		});
	
			
	});
}

function pad(number,length) {
    var str = '' + number;
    while (str.length < length)
        str = '0' + str;
    return str;
}


function SetCookie( name, value, expiredays ) {
	if(arguments.length < 2) { return; }
	var n = arguments[0];
	var v = arguments[1];
	var d = 0;
	if(arguments.length > 2) { d = parseInt(arguments[2]); }
	var exp = '';
	if(d > 0) {
		var now = new Date();
		then = now.getTime() + (d * 24 * 60 * 60 * 1000);
		now.setTime(then);
		exp = '; expires=' + now.toGMTString();
		}
	document.cookie = n + "=" + escape(String(v)) + '; path=/' + exp;
} 

function ReadCookie(name) {
	var cookiecontent = new String();
	if(document.cookie.length > 0) {
		var cookiename = name+ '=';
		var cookiebegin = document.cookie.indexOf(cookiename);
		var cookieend = 0;
		if(cookiebegin > -1) {
			cookiebegin += cookiename.length;
			cookieend = document.cookie.indexOf(";",cookiebegin);
			if(cookieend < cookiebegin) { cookieend = document.cookie.length; }
			cookiecontent = document.cookie.substring(cookiebegin,cookieend);
			}
		}
	return unescape(cookiecontent);
} 



