/*
	Copyright (c) 2004-2010, The Dojo Foundation All Rights Reserved.
	Available via Academic Free License >= 2.1 OR the modified BSD license.
	see: http://dojotoolkit.org/license for details
*/


// hide the page
/* 
(function(){
  // if firefox 3.5+, hide content till load (or 3 seconds) to prevent FOUT
  var d = document, e = d.documentElement, s = d.createElement('style');
  if (e.style.MozTransform === ''){ // gecko 1.9.1 inference
    s.textContent = 'body{visibility:hidden}';
    e.firstChild.appendChild(s);
    function f(){ s.parentNode && s.parentNode.removeChild(s); }
    addEventListener('load',f,false);
   //setTimeout(f,3000); 
  }
})();
*/

// variables
var m = 0;
var limit = 0;
var more = false;
var isLogin = false;

dojo.addOnLoad(init);

function init() {
	// format	

	// listeners
	dojo.query("input, textarea").connect("focus", "clear_text");
	
	
	dojo.query(".secondary_nav .client_login").connect("onclick", "login_show");
	dojo.query(".secondary_nav").connect("onclick", "stop_props");
		
	dojo.query(".clients li").connect("onmouseover", "on_client_mouse_over");
	dojo.query(".clients li").connect("onmouseout", "on_client_mouse_out");
	
	dojo.query("body").connect("onclick", "login_hide");

	// run	
	init_aside();
	init_entries();
	
	if(typeof init_secondary == 'function') {
		init_secondary();
	}
	
	if(typeof init_blogs == 'function') {
		init_blogs();
	}
}

////////////////////////////////////////////////////////////////////////////////////////////////////
/////     LISTENERS
////////////////////////////////////////////////////////////////////////////////////////////////////

function stop_props(evt) {
	evt.stopPropagation();
}

// show login form 
function login_show(evt) {
	var login_form = dojo.query(".login_form_container").style("display", "block");
	var login_show = dojo.query(".secondary_nav .client_login").style("display", "none");
	
	setTimeout(function() {
		isLogin = true;
	},250);
}

function login_hide() {
	if (isLogin == true) {
	 	var login_form = dojo.query(".login_form_container").style("display", "none");
	 	var login_show = dojo.query(".secondary_nav .client_login").style("display", "block");
	 	isLogin = false;
	 }
}

// submit login form
function login_submit(evt) {
     login_submit_form();
}

function login_submit_form() {
	dojo.xhrPost({
        content: {
            UserName: dojo.byId('UserName').value,
            Password: dojo.byId('Password').value
        },
        url: '/login/',
        load: function(response, ioArgs){
			if (response == "error") {
				dojo.byId('UserName').value = "username";
            	dojo.byId('Password').value = "password";
				alert("Your username and password do not match. Please try logging in again.");
				
			} else {
				dojo.byId('login_form').setAttribute("action","https://digitalbucket.net/members/login.aspx");
				dojo.byId('login_form').submit();
			}
        }
    });
}

// client list rollover
function on_client_mouse_over(evt) {
	dojo.query(".clients li").style("zIndex", "1");
	dojo.style(evt.target, "zIndex", "10");
	
	var pos_info = dojo.position(evt.target, true);
	
	var tip = dojo.byId("client_tooltip");
	tip.innerHTML = evt.target.innerHTML;
	
	dojo.style("client_tooltip", {left:pos_info.x-32.5+"px", top:pos_info.y-36+"px", display: "block", zIndex: "5"});
	
	evt.stopPropagation();
}

// client list rollout
function on_client_mouse_out(evt) {
	dojo.style("client_tooltip", {display: "none", zIndex: "0"});
}

///// clear text from inputs /////
function clear_text(evt) {
	var static_text = ["can we help you find something?", "Name", "Email Address", "Type your message", "username", "password", "URL (optional)", "Comment"];
	
	Array.prototype.hasObject = (
		!Array.indexOf ? function (o) {
			var l = this.length + 1;
			while (l -= 1) { 
				if (this[l - 1] === o) {
					return true;
				}
			}
			return false;
			} : function (o) {
				return (this.indexOf(o) !== -1);
			}
	);

	if (static_text.hasObject(evt.target.value)) {
		if (evt.target.value == "URL (optional)") {
		  	evt.target.value = "http://";
		} else {
			evt.target.value = "";
		}
	}
}

////////////////////////////////////////////////////////////////////////////////////////////////////
/////     FUNCTIONS
////////////////////////////////////////////////////////////////////////////////////////////////////
