/*
	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
*/


var active = false;
var is_home;
var connections = [];


function init_entries() {
	// format

	// run
	init_entries_format();
	
	// listeners
	init_listeners();
}

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

function init_listeners() {
	if (connections.length > 0) {
	 	dojo.forEach(connections, dojo.disconnect);
	}
	
	connections = [];
	
	dojo.query(".show_comments").forEach(function(node) {
		connections.push(
			dojo.connect(node, "onclick", "show_hide_comments")
		);
	});
	
	dojo.query(".show_hide").forEach(function(node) {
		connections.push(
			dojo.connect(node, "onclick", "show_hide")
		);
	});
	
	dojo.query(".share").forEach(function(node) {
		connections.push(
			dojo.connect(node, "onclick", "show_share")
		);
	});
	
	dojo.query(".email").forEach(function(node) {
		connections.push(
			dojo.connect(node, "onclick", "show_email")
		);
	});
	
	dojo.query(".email_send").forEach(function(node) {
		connections.push(
			dojo.connect(node, "onclick", "send_email")
		);
	});
	
	dojo.query(".share_again").forEach(function(node) {
		connections.push(
			dojo.connect(node, "onclick", "re_show_email")
		);
	});
	
	dojo.query(".close, body").forEach(function(node) {
		connections.push(
			dojo.connect(node, "onclick", "on_hide_share")
		);
	});
	
	dojo.query(".entry_footer").forEach(function(node) {
		connections.push(
			dojo.connect(node, "onclick", "stop_prop")
		);
	});
	
	dojo.query(".comment_prev").forEach(function(node) {
		connections.push(
			dojo.connect(node, "onclick", "show_preview")
		);
	});
		
	dojo.query(".comment_sub").forEach(function(node){
		connections.push(
			dojo.connect(node, "onclick", "submit_comment")
		);
	});
	
	dojo.query(".see_more_posts a").forEach(function(node){
		connections.push(
			dojo.connect(node, "onclick", "get_more_posts")
		);
	});
	
	dojo.query(".slideshow_left ul li img").forEach(function(node) {
		connections.push(
			dojo.connect(node, "onclick", "on_slide_thumb")
		);
	});
	
	dojo.query(".slideshow_left .left").forEach(function(node) {
		connections.push(
			dojo.connect(node, "onclick", "on_slide_left")
		);
	});
	
	dojo.query(".slideshow_left .right").forEach(function(node) {
		connections.push(
			dojo.connect(node, "onclick", "on_slide_right")
		);
	});
}

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

function show_share(evt) {
	var entry = evt.target.parentNode.parentNode.parentNode;
	
	dojo.query(".share_box", entry).style("display", "block");
	
	setTimeout(function() {
		active = true;
	},500);
}

function on_hide_share(evt) {
	if (active == true) {
		dojo.query(".share_box").style("display", "none");
		dojo.query(".email_box").style("display", "none");
		dojo.query(".email_success_box").style("display", "none");
		active = false;
	}
}

function show_email(evt) {
	var entry = evt.target.parentNode.parentNode.parentNode.parentNode.parentNode;

	dojo.query(".email_box", entry).style("display", "block");
	dojo.query(".share_box", entry).style("display", "none");
	dojo.query(".email_success_box", entry).style("display", "none");
}

function re_show_email(evt) {
	var entry = evt.target.parentNode.parentNode.parentNode;	
	
	dojo.query(".email_success_box", entry).style("display", "none");
		dojo.query(".email_box", entry).style("display", "block");
	setTimeout(function() {
		dojo.query(".share_box", entry).style("display", "none");
	},250);
}

function send_email(evt) {
	var entry = evt.target.parentNode.parentNode.parentNode.parentNode.parentNode;
	var d = entry.id.charAt(entry.id.length-1);
	
	dojo.xhrPost({
		content : {
			to : dojo.query(".email_to", entry)[0].value,
			from : dojo.query(".email_from", entry)[0].value,
			url : dojo.query(".email_url", entry)[0].value,
			message : dojo.query(".email_message", entry)[0].value
		},
		url: '/ajax/send/',
		load: function(response, ioArgs) {
			if(response == "sent"){
				dojo.query(".email_to", entry)[0].value = "";
				from : dojo.query(".email_from", entry)[0].value = "";
				dojo.query(".email_message", entry)[0].value = "";
				dojo.query(".email_box", entry).style("display", "none");
				dojo.query(".email_success_box", entry).style("display", "block");
			} else {
				alert("We were unable to send your email. Please check that you've entered correctly formatted data");
			}
		}
	});
}

function show_hide(evt) {
	var entry = evt.target.parentNode.parentNode.parentNode;
	if (dojo.byId("view_list") != null) {
	 	dojo.byId("view_list").setAttribute("class", "");
	 	dojo.byId("view_expanded").setAttribute("class", "");
	}

	if(entry.open == true) {
		contract_entry(entry);
		entry.open = false;
		dojo.style(evt.target, "backgroundPosition", "-85px -325px");
	} else {
		expand_entry(entry);
		entry.open = true;
		dojo.style(evt.target, "backgroundPosition", "-85px -345px");	
	}
}

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

// set up entries
function init_entries_format () {
	// close main copy
	dojo.query(".share_box, .email_box, .email_success_box").style("display", "none");
	
	// share box setup
	dojo.query(".career_entry, .entry").forEach(function(node){
		node.share = false;
	});	
}

function format_index() {
	dojo.query(".entry_long").style("height", "0px");
	dojo.query(".entry_type_long").style("display", "none");
	dojo.query(".entry_long").style("display", "none");
}

function format_no_index() {
	dojo.query(".entry_type_short, entry_type_static").style("display", "none");
	dojo.query(".entry").style("marginBottom", "0px");
	dojo.query(".show_hide").style("display", "none");

    var entries = dojo.query(".entry");
    dojo.forEach(entries, function(entry, i) {
        var l = dojo.query(".col_left", entry)[0];
        var lCoords = dojo.coords(l);
        var r = dojo.query(".col_right", entry)[0];
        var rCoords = dojo.coords(r);
        if (lCoords.h > rCoords.h) {
            dojo.style(r, "height", lCoords.h + "px");
        } else if (lCoords) {
            dojo.style(l, "height", rCoords.h + "px");
        }
    });
}


function hide_share() {
	dojo.query(".share_box, .email_box, .email_success_box").forEach(function(node, index, arr){
		var entry = node.parentNode.parentNode.parentNode;
		dojo.style(node, "display", "none");
		node.share = false;
	});
}

function expand_entry(entry) {
	var entry_long = dojo.query(".entry_long", entry)[0];
	dojo.query(".entry_short", entry).style("marginBottom", "0px");
	dojo.query(".entry_type_short", entry).style("display", "none");
	dojo.query(".entry_type_long", entry).style("display", "block");
	
	if (entry_long != undefined) {
	 	dojo.fx.wipeIn({
	 		node: entry_long,
	 		duration: 500,
                        onEnd: function() {
                            var entries = dojo.query(".entry");
                            dojo.forEach(entries, function(entry, i) {
                                var l = dojo.query(".col_left", entry)[0];
                                var lCoords = dojo.coords(l);
                                var r = dojo.query(".col_right", entry)[0];
                                var rCoords = dojo.coords(r);
                                if (lCoords.h > rCoords.h) {
                                    dojo.style(r, "height", lCoords.h + "px");
                                } else if (lCoords) {
                                    dojo.style(l, "height", rCoords.h + "px");
                                }
                            });
                        }
	 	}).play();
	}
}

function contract_entry(entry) {
	var entry_long = dojo.query(".entry_long", entry)[0];
	dojo.query(".entry_short", entry).style("marginBottom", "20px");
	dojo.query(".entry_type_short", entry).style("display", "block");
	dojo.query(".entry_type_long", entry).style("display", "none");
	if (entry_long != undefined) {
		dojo.fx.wipeOut({
			node: entry_long,
			duration: 500
		}).play();
	}
}

