/*******************************************************************************
	BASE FUNCTIONS
*******************************************************************************/



/* Find the position of an object and what it relates to */
	function findPos(obj) {
		var curleft = topheight = parentheight = curtop = curheight = 0;
		if (obj.offsetParent) {
			curleft = obj.offsetLeft
			curtop = obj.offsetTop
			curheight = obj.offsetHeight
			parentheight = obj.offsetParent.offsetHeight

			while (obj = obj.offsetParent) {
				curleft += obj.offsetLeft
				curtop += obj.offsetTop
				curheight += obj.offsetHeight
			}
		}
		curheight = curheight - parentheight;
		topheight = Number(((curtop + (parentheight * 2)) - curheight) + curtop);
		return [curleft,curtop,curheight,topheight,parentheight];
	}


/*  FIND THE HEIGHT OF THE PAGE */
	function findHeight() {
	  var myHeight = 0;
	  if( typeof( window.innerHeight ) == 'number' ) {
		//Non-IE
		myHeight = window.innerHeight;
	  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myHeight = document.documentElement.clientHeight;
	  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myHeight = document.body.clientHeight;
	  }
	  return myHeight;
	}

	//Get a query string variable
	function getQueryVariable(variable, target) {

	  if(target){
		  var query = target.substring(1);
	  } else {
		  var query = window.location.search.substring(1);
	  }

	  var vars = query.split("&");
	  for (var i=0;i<vars.length;i++) {
		var pair = vars[i].split("=");
		if (pair[0] == variable) {
		  return pair[1];
		}
	  }
	  return false;
	}

	//Get a query string variable	from the anchor
	function getAnchorQueryVariable(variable, target) {

	  if(target){
		  var query = target.substring(1);
	  } else {
		  var query = window.location.search.substring(1);
	  }

	  var vars = query.split("#")[1].split("&");
	  for (var i=0;i<vars.length;i++) {
			var pair = vars[i].split("=");
			if (pair[0] == variable) {
				return pair[1];
			}
	  }
	  return false;
	}

	function isPhoneNumber(s)
	{

		// Check for correct phone number
		rePhoneNumber = new RegExp(/^(?:\([2-9]\d{2}\)\ ?|[2-9]\d{2}[\s|\.|\-]?)[2-9]\d{2}[\s|\.|\-]?\d{4}$/);

		if (!rePhoneNumber.test(s)) {
		  //alert("Phone Number Must Be Entered As: 555-555-1234");
		  return false;
		}

		return true;
	}

	function isEmail(s)
	{

		// Check for correct email
		reEmail = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);

		if (!reEmail.test(s)) {
		  return false;
		}

		return true;
	}

	function checkInput(obj, text){
		if(obj.value == text){
			obj.value = "";
		} else if(obj.value == " " || obj.value == ""){
			obj.value = text;
		}
	}



/*******************************************************************************
	SPECIFIC FUNCTIONS
*******************************************************************************/

	function setup_template_select(){
		var template_select = document.getElementById('widget_template_select');
		var image_field = document.getElementById('image_field');

		template_select.onchange = function(){
			if(this.options[this.selectedIndex].className == "has_image"){
				image_field.style.display = 'block';
			} else {
				image_field.style.display = 'none';
			}
		}
	}
	function show_contacts(contact){
		$("div.contact_info").css("display", "none");
		$("#contact_" + contact).css("display", "");
	}

	function setup_page_section(){

		$("#page_section").bind("change", function(){
			if(this.selectedIndex > 0){
				$.get("../../lib/get_section_pages.php?s="+this.options[this.selectedIndex].value, function(data){
					//alert("Data Loaded: " + data);
					$("#page_parent").html(data);
					document.getElementById("pick_template").style.display = 'block';
					if(document.getElementById("next_step")){
						document.getElementById("next_step").style.display = 'block';
					}
				 });
			} else {
					document.getElementById("pick_template").style.display = 'none';
					if(document.getElementById("next_step")){
						document.getElementById("next_step").style.display = 'none';
					}
					document.getElementById("page_parent").innerHTML = "<option value=\"0\">Select a section above first.</option>";
			}
			return false;
		});
	}
	function show_hide_pages(parent_link){
		if(parent_link.className == "expanded"){
			parent_link.className = "minimized";
			parent_link.parentNode.nextSibling.style.display = 'none';
		} else {
			parent_link.className = "expanded";
			parent_link.parentNode.nextSibling.style.display = 'block';
		}
	}

	function setup_editpage(){
		$("a.minimized").bind("click", function(){
			//show_edit_pages(this);
			show_hide_pages(this);
			return false;
		});
		$("a.expanded").bind("click", function(){
			show_hide_pages(this);
			//hide_edit_pages(this);
			return false;
		});
		$("a.publish_link").bind("click", function(){
			update_page_status(this);
			return false;
		});
	}

	function setup_callouts(){
		$("#callout_list li.callout").Draggable(
			{
				revert:	true,
				opacity:	0.5,
				fx:			300
			});
		$("#callout_holder").Droppable(
			{
				accept :		'callout',
				onDrop:			add_callout_to_panel
			});
	}
	function setup_home_callouts(){
		
		$("#callout_list li.callout").Draggable(
			{
				revert:	true,
				ghosting: true,
				opacity:	0.5,
				fx:			300
			});
		$("#home_callout_holder").Droppable(
			{
				accept :		'callout',
				onDrop:			add_home_callout_to_panel
			});
	}

	function update_page_status(obj){
		//alert("hit | " + obj.href);
		$.get(obj.href, function(data){
			$("#" + data.split("^")[0]).html(data.split("^")[1]);
		 });
	}

	var add_home_callout_to_panel = function(dragged){
		var added_callout = dragged.id.split('_')[1];
		var added_callout_content = dragged.innerHTML;
		var num_callouts = $("#home_callout_holder div").length;
		if(num_callouts < 3){
			var callout_to_place = "<div id=\"holder_" + added_callout + "\"><input type=\"hidden\" name=\"callout[]\" value=\"" + added_callout + "\" /><a class=\"remove_callout\" onclick=\"$(this).parent().remove(); return false;\">remove callout</a><span>" + added_callout_content + "</span></div>";
			if(!document.getElementById("holder_"+added_callout)){
				$("#home_callout_holder").append(callout_to_place);
			}
			/*
			$("#home_callout_holder").find('a').bind("click", function(){
				alert($(this).parent().attr('id'));
				//$(this).parent().remove();
			});
			*/
		}
	}

	var add_callout_to_panel = function(dragged){
		var added_callout = dragged.id.split('_')[1];
		var added_callout_content = dragged.innerHTML;
		var num_callouts = document.getElementById("callout_holder").getElementsByTagName('div').length;
		var callout_to_place = "<div id=\"holder_" + added_callout + "\"><input type=\"hidden\" name=\"callout[" + num_callouts +"]\" value=\"" + added_callout + "\" /><a class=\"remove_callout\" onclick=\"$(this).parent().remove(); return false;\">remove callout</a><span>" + added_callout_content + "</span></div>";
		if(!document.getElementById("holder_"+added_callout)){
			$("#callout_holder").append(callout_to_place);
		}
		/*
		$("#callout_holder").find('a').bind("click", function(){
			//alert(this.parentNode.id);
			$(this).parent().remove();
		});
		*/
	}

	$.accordian = function(items, first, options) {

		var active = first;
		var running = 0;

		var titles = options && options.titles || '.title';
		var contents = options && options.contents || '.content';
		var onClick = options && options.onClick || function(){};
		var onShow = options && options.onShow || function(){};
		var onHide = options && options.onHide || function(){};
		var showSpeed = options && options.showSpeed || 'slow';
		var hideSpeed = options && options.hideSpeed || 'fast';

		$(items).not(active).children(contents).hide();
		$(items).not(active).each(onHide);
		$(active).each(onShow);

		$(items).children(titles).click(function(e){

			var p = $(contents, this.parentNode);
			$(this.parentNode).each(onClick);

			if (running || !p.is(":hidden")) return false;
			running = 2;

			$(active).children(contents).not(':hidden').slideUp(hideSpeed, function(){--running;});
			p.slideDown(showSpeed, function(){--running;});

			$(active).each(onHide);
			active = '#' + $(this.parentNode)[0].id;
			$(active).each(onShow);

			return false;
		});

	};

	function simpleLog(message) {
		//$('<div>' + message + '</div>').appendTo('#log');
	}

	var add_product_item = function(){
		$.get("../../templates/pages/_product_project.html", function(data){
			//alert("Data Loaded: " + data);
			$("#product_table").append(data);
		 });
	}

	var remove_product_item = function(object){
		$("#project_" + object.id.split("_")[2]).remove();
	}

	var add_case_study = function(){
		$.get("../../templates/pages/_case.html", function(data){
			//alert("Data Loaded: " + data);
			$("#case_table").append(data);
		 });
	}

	var remove_case_study = function(object){
		$("#project_" + object.id.split("_")[2]).remove();
	}

/*******************************************************************************
	MYHEMLOCK FUNCTIONS
*******************************************************************************/
	function save_edits(){
		
		var content_to_send = $("#myhemlock_edit_form input").serialize() + "&" + $("#myhemlock_edit_form select").serialize();
		$.post("/lib/myhemlock_edit.php", content_to_send, function(data){
			//alert("Data Loaded: " + data);
			$("#myhemlock_content").empty();
			$("#myhemlock_content").append(data.split("|")[1]);
			
			$.post("/lib/myhemlock_refresh.php", "id="+data.split("|")[0], function(data){
				//alert("Data Loaded: " + data);
				var success = parseInt(data.split("^")[0]);
				//alert(success);
				if(success > 0){
					$("#tool_plate").empty();
					$("#tool_plate").append(data.split("^")[1]);
				}
			});
		});		
	}
	function do_login(){
		//alert($("#myhemlock_form input").serialize());
		var content_to_send = $("#myhemlock_form input").serialize();
		$.post("/lib/myhemlock_login.php", content_to_send, function(data){
			//alert("Data Loaded: " + data);
			var success = parseInt(data.split("^")[0]);
			//alert(success);
			if(success > 0){
				$("#myhemlock_username").removeClass("err_mes");
				$("#myhemlock_password").removeClass("err_mes");
				$("#tool_plate").empty();
				$("#tool_plate").append(data.split("^")[1]);
				close_slide_panel();
				setup_loggedin();
			} else {
				$("#myhemlock_username").addClass("err_mes");
				$("#myhemlock_password").addClass("err_mes");
				slide_panel('login_problem.html');
			}
		});
	}

	function do_logout(){
		//alert($("#myhemlock_form input").serialize());
		$.get("/lib/myhemlock_logout.php?logout=1", function(data){
			//alert("Data Loaded: " + data);
			var success = parseInt(data.split("^")[0]);
			//alert(success);
			if(success > 0){
				$("#tool_plate").empty();
				$("#tool_plate").append(data.split("^")[1]);
				setup_login_check();
			}
		});
	}

	function login_check(){
		var is_valid = true;
		if($("#myhemlock_username").attr("value") == "" || $("#myhemlock_username").attr("value") == "Email"){
			is_valid = false;
		}
		if($("#myhemlock_password").attr("value") == "" || $("#myhemlock_password").attr("value") == "Password"){
			is_valid = false;
		}
		//alert(is_valid);

		if(is_valid){
			$("#myhemlock_form a.btn_go").removeClass('disabled');
			$("#myhemlock_form a.btn_go").bind("click", function(){
				do_login();
				return false;
			})
		}
	}
	function setup_loggedin(){
	}

	function setup_login_check(){
		$("#myhemlock_username").bind("blur", function(){
			login_check();
		});
		$("#myhemlock_password").bind("blur", function(){
			login_check();
		});
		$("#myhemlock_username").bind("change", function(){
			login_check();
		});
		$("#myhemlock_password").bind("change", function(){
			login_check();
		});
		$("#myhemlock_rememberme").bind("click", function(){
			if(this.checked){
				this.value=1;
			} else {
				this.value=0
			}
		});
		$("#myhemlock_form a.btn_go").bind("click", function(){
			return false;
		});
		login_check();
	}

	function setup_req_check(){
		$("#reg_form .required").bind("blur", function(){
			req_check();
		});
		$("#reg_form select.required").bind("change", function(){
			req_check();
		});
		$("#btn_next").bind("click", function(){
			return false;
		});
		req_check();
	}

	function req_check(){
		var is_valid = true;
		$.each($("#reg_form .required"), function(i, n){
			//alert( "Value: " + n.value );
			if(n.name == "email"){
				if(!isEmail(n.value)){
					is_valid = false;
				}
			} else {
				if(n.value == "" || n.value == "-"){
					is_valid = false;
				}
			}
		});
		if(is_valid){
			$("#btn_next").removeClass('disabled');
			$("#btn_next").bind("click", function(){
				submit_with_reqs('reg_form', 'step2');
				return false;
			})
		}
	}

function setup_add_tools(){
		$.each($("#myhemlock .btn_add"), function(i, n){
			$("#" + n.id).unbind("click");
			$("#" + n.id).bind("click", function(){
				$("#tool_" + this.id.split("_")[1]).attr("value", 1);
				$("#signin_" + this.id.split("_")[1]).css("display","");
				$("#add_" + this.id.split("_")[1]).removeClass('btn_add');
				$("#add_" + this.id.split("_")[1]).addClass('btn_delete');
				setup_add_tools();
				return false;
			});
		});
		$.each($("#myhemlock .btn_delete"), function(i, n){
			$("#" + n.id).unbind("click");
			
			$("#" + n.id).bind("click", function(){
				$("#tool_" + this.id.split("_")[1]).attr("value", 0);
				$("#signin_" + this.id.split("_")[1]).css("display","none");
				$("#add_" + this.id.split("_")[1]).removeClass('btn_delete');
				$("#add_" + this.id.split("_")[1]).addClass('btn_add');
				setup_add_tools();
				return false;
			});
		});

		$("#btn_next").bind("click", function(){
			submit_with_reqs('reg_form', 'step3');
			return false;
		})
		$("#btn_back").bind("click", function(){
			load_next_page('step1');
			return false;
		})
	}

	function submit_with_reqs(form_id, next_step){
		//load_next_page(page_to_load);
		//alert("submit form:" + $("#" + form_id + " table input").serialize() + "&" + $("#" + form_id + " table select").serialize());
		var content_to_send = $("#" + form_id + " input").serialize() + "&" + $("#" + form_id + " select").serialize();
		$.post("/lib/myhemlock_reg.php?step=" + next_step, content_to_send, function(data){
			//alert("Data Loaded: " + data);
			$("#myhemlock_content").empty();
			$("#myhemlock_content").append(data);
		});
	}

	function load_next_page(page_to_load){
		//alert(step);
		$.get("/lib/myhemlock_reg.php?step=" + page_to_load, function(data){
			//alert("Data Loaded: " + data);
			$("#myhemlock_content").empty();
			$("#myhemlock_content").append(data);
		});
	}

	function open_panel(page_to_load){
		if(document.getElementById('myhemlock_content_container').style.display == 'none' || document.getElementById('myhemlock_content_container').style.display == ''){
			if(document.getElementById('sub_plate').style.marginRight.split("px")[0] != '0'){
				$("#tool_plate").css("background", "url(/img/myhemlock/bgd_content_rt.gif) right top no-repeat");
				$.get("/lib/myhemlock_reg.php?step=" + page_to_load, function(data){
					//alert("Data Loaded: " + data);
					$("#myhemlock_content").empty();
					$("#myhemlock_content").append(data);
					$("#tool_plate_container").animate(
						{
							width: "745px"
						}, 500
					);
					$("#tool_plate").animate(
						{
							width: "745px"
						}, 500, function(){
							$("#myhemlock").css("background", "url(/img/myhemlock/bgd_full_content.jpg) left bottom no-repeat");

							$("#myhemlock_content_container").slideDown(500);
						}
					);
				});
			} else {
				$("#sub_plate").animate({
					marginRight: "-380px"
				},500, function(){
					$("#sub_plate").css("display", "none");
					open_panel(page_to_load);
				});
			}
		} else {
			load_next_page(page_to_load);
		}
	}
	function open_edit_panel(){
		if(document.getElementById('myhemlock_content_container').style.display == 'none' || document.getElementById('myhemlock_content_container').style.display == ''){
			if(document.getElementById('sub_plate').style.marginRight.split("px")[0] != '0'){
				$("#tool_plate").css("background", "url(/img/myhemlock/bgd_content_rt.gif) right top no-repeat");
				$.get("/lib/myhemlock_edit.php", function(data){
					//alert("Data Loaded: " + data);
					$("#myhemlock_content").empty();
					$("#myhemlock_content").append(data);
					$("#tool_plate_container").animate(
						{
							width: "745px"
						}, 500
					);
					$("#tool_plate").animate(
						{
							width: "745px"
						}, 500, function(){
							$("#myhemlock").css("background", "url(/img/myhemlock/bgd_full_content.jpg) left bottom no-repeat");

							$("#myhemlock_content_container").slideDown(500);
						}
					);
				});
			} else {
				$("#sub_plate").animate({
					marginRight: "-380px"
				},500, function(){
					$("#sub_plate").css("display", "none");
					open_edit_panel();
				});
			}
		}
	}

	function close_panel(){
		$("#myhemlock_content_container").slideUp(500, function(){
			$("#myhemlock").css("background", "none");
			$("#tool_plate_container").animate(
				{
					width: "370px"
				}, 500
			);
			$("#tool_plate").animate(
				{
					width: "370px"
				}, 500
			);
		});
	}

	function close_slide_panel(){
		$("#sub_plate").animate({
			marginRight: "-380px"
		},500, function(){
			$("#sub_plate").css("display", "none");
		});
	}

	function slide_panel(page_to_load){
		if(document.getElementById('tool_plate').style.width.split("px")[0] > 370){
			$("#myhemlock_content_container").slideUp(500, function(){
				$("#myhemlock").css("background", "none");
				$("#tool_plate_container").animate(
					{
						width: "370px"
					}, 500
				);
				$("#tool_plate").animate(
					{
						width: "370px"
					}, 500, function(){
						slide_panel(page_to_load);
					}
				);
			});
		} else {
			$.get("/myhemlock/" + page_to_load, function(data){
				$("#sub_plate").empty();
				$("#sub_plate").append(data);
				$("#tool_plate").css("background", "url(/img/myhemlock/bgd_sub_rt.gif) right top no-repeat");
				$("#sub_plate").css("display", "block");
				$("#sub_plate").animate({
					marginRight: "0px"
				},500);
			});
		}
	}
	function move_contact(num){
		var contact_width = parseInt($('#myhemlock_contacts div.contact').css('width').split("px")[0]);
		var num_contacts = $('#myhemlock_contacts div.contact').length;
		var max_contacts = (num_contacts-1);


		$('#myhemlock_contacts div.contacts').css("width", (num_contacts * contact_width));

		var current_position = Math.abs(Math.ceil(parseInt($('#myhemlock_contacts div.contacts').css('left').split("px")[0]) / parseInt(contact_width)));
		var total_width = $('#myhemlock_contacts div.contacts').css('width').split("px")[0];

		if(num > 0){
			max_contacts += 1;
			var next_pos = 0-((contact_width * (current_position -1)) * num);
		} else {
			var next_pos = (contact_width * (current_position +1)) * num;
		}

		/*alert(next_pos);*/
		/*alert((current_position >= 0) + " | " + (current_position <= (num_contacts-1)) + " | " + parseInt(num * contact_width));*/
		if(current_position >= 0 && current_position < max_contacts && next_pos <= 0){
			/*alert(current_position + " | " + num_contacts);*/
			$('#myhemlock_contacts div.contacts').animate(
				{
					left: next_pos + "px"
				}, 300
			);
		}
	}
	function slide_sample(num){
		var sample_width = parseInt($('#sample_content_container img').css('width').split("px")[0]);
		var num_samples = $('#sample_content_container img').length;
		var max_samples = (num_samples-1);
		var total_width = num_samples * sample_width;

		$('#sample_content_container').css("width", total_width);

		var current_position = Math.abs(Math.ceil(parseInt($('#sample_content_container').css('left').split("px")[0]) / parseInt(sample_width)));

		if(num > 0){
			max_samples += 1;
			var next_pos = 0-((sample_width * (current_position -1)) * num);
		} else {
			var next_pos = (sample_width * (current_position +1)) * num;
		}

		if(current_position >= 0 && current_position < max_samples && next_pos <= 0){
			/*alert(current_position + " | " + num_contacts);*/
			$('#sample_content_container').animate(
				{
					left: next_pos + "px"
				}, 200
			);
		}
	}
	
	function clear_form(form_id){
		var form, elements, i, elm; 
		form = document.getElementById 
			? document.getElementById(form_id) 
			: document.forms[form_id];
		
		if (document.getElementsByTagName)
		{
			elements = form.getElementsByTagName('input');
			for( i=0, elm; elm=elements.item(i++); )
			{
				if (elm.getAttribute('type') == "text")
				{
					elm.value = '';
				}
			}
			elements = form.getElementsByTagName('select');
			for( i=0, elm; elm=elements.item(i++); )
			{
				elm.options.selectedIndex=0;
			}
			elements = form.getElementsByTagName('textarea');
			for( i=0, elm; elm=elements.item(i++); )
			{
				elm.value = '';
			}
		}
		return false;
	}

/*******************************************************************************
	INIT FUNCTIONS
*******************************************************************************/

	function init() {
		// use this function for all newer browsers
		var template_select = document.getElementById('widget_template_select');
		var page_section = document.getElementById('page_section');
		var editpage = document.getElementById('editpage');
		var editnews = document.getElementById('editnews');
		var editindustryaward = document.getElementById('editindustryaward');
		var edithemlockaward = document.getElementById('edithemlockaward');
		var callout_list = document.getElementById('callout_list');
		var callout_holder = document.getElementById('callout_holder');
		var product_table = document.getElementById('product_table');
		var case_table = document.getElementById('case_table');
		var myhemlock = document.getElementById('myhemlock');
		var main_menu = document.getElementById('main_menu');

		if(myhemlock){
			$("#myhemlock_username").bind("focus", function(){
				if(this.value == "Email"){
					this.value = "";
				}
			});
			$("#myhemlock_username").bind("blur", function(){
				if(this.value == ""){
					this.value = "Email";
				}
			});
			$("#myhemlock_password").bind("focus", function(){
				if(this.value == "Password"){
					this.value = "";
				}
			});
			$("#myhemlock_password").bind("blur", function(){
				if(this.value == ""){
					this.value = "";
				}
			});

			$("#myhemlock_register").bind("click", function(){
				open_panel('step1');
				return false;
			});
			$("#myhemlock_reg_close").bind("click", function(){
				close_panel();
				return false;
			});
			$("#myhemlock_benefits").bind("click", function(){
				open_panel('benefits');
				return false;
			});
			
			$("#myhemlock_recover").bind("click", function(){
				open_panel('recover');
				return false;
			});
			
			$("#myhemlock_remember").bind("click", function(){
				slide_panel('rememberme.html');
				return false;
			});
			$("#myhemlock_contacts").bind("click", function(){
				slide_panel('contacts.php');
				return false;
			});
			$("#myhemlock_edit").bind("click", function(){
				open_edit_panel();
				return false;
			});
			if(document.getElementById('myhemlock_form')){
				setup_login_check();
			}
			if(document.getElementById('signedin_content')){
				setup_loggedin();
			}
		}


$('#sample_content_container').cycle({ 
    prev:   '#prev', 
    next:   '#next', 
    timeout:       4000,  // milliseconds between slide transitions (0 to disable auto advance) 
    speed:         1000,  // speed of the transition (any valid fx speed value) 
    pause:         1,     // true to enable "pause on hover" 
    delay:         100     // additional delay (in ms) for first transition (hint: can be negative) 

});
/*
		$("#sample_callout a.left_arrow").bind("click", function(){
				slide_sample(1);
				return false;
			});
		$("#sample_callout a.right_arrow").bind("click", function(){
				slide_sample(-1);
				return false;
			});
*/
		if(case_table){
			$("a.add_case").bind("click", add_case_study);
			$("a.remove_case").bind("click", function(){
				remove_case_study(this);
			});
		}

		if(product_table){
			$("a.add_project").bind("click", add_product_item);
			$("a.remove_project").bind("click", function(){
				remove_product_item(this);
			});
		}

		if(callout_list && callout_holder){
			setup_callouts();
		}

		if(document.getElementById('home_callout_holder')){
			setup_home_callouts();
		}

		if(editpage || editnews || editindustryaward || edithemlockaward){
			setup_editpage();
		}

		if(page_section){
			setup_page_section();
		}

		if(template_select){
			setup_template_select();
		}
		
		if(document.getElementById('add_callout_link')){
			$("#add_callout_link").bind("click", function(){
				if($("#link_table tr.link_tr").length <= 7){
					$.get("../../templates/widgets/_link.html", function(data){
						//alert("Data Loaded: " + data);
						$("#link_table").append(data);
					});
				}
				return false;
			});
		}

		if(window.location.href.split("#").length > 1){
			url_string = window.location.href.split("#")[1];
			dyn_type = url_string.split('&')[0].split('=')[1];
			if(dyn_type == "faq"){
				$.get("/lib/dyn_content.php?" + url_string, function(data){
					//alert(data);
					if(parseInt(data.split('|')[0]) >= 1){
						
						$("#faq_list a").removeClass("selected");
						$("#faq_nav_" + data.split('|')[0]).addClass("selected");
						
						var project_name = data.split('|')[1];
						$("#faq_q").empty();
						$("#faq_q").append(project_name);
						var project_client = data.split('|')[2];
						$("#faq_a").empty();
						$("#faq_a").append(project_client);
					}
				});
			}else {
				$.get("/lib/dyn_content.php?" + url_string, function(data){
					if(parseInt(data.split('|')[0]) >= 0){
						
						$("#product_nav a").removeClass("selected");
						$("#project_nav_" + data.split('|')[0]).addClass("selected");
						
						var project_name = data.split('|')[2];
						$("#project_name").empty();
						$("#project_name").append(project_name);
						var project_client = data.split('|')[1];
						$("#project_client").empty();
						$("#project_client").append(project_client);
						var project_image = data.split('|')[3];
						$("#project_image").attr("src", project_image);
					/*	$("#project_image").attr("alt", project_name);*/
						if(data.split('|').length > 4){
							var project_body = data.split('|')[4];
							$("#project_body").empty();
							$("#project_body").append(project_body);
						}
					}
				});
			}
		}

		$("#product_nav a").bind("click", function(){
			$("#product_nav a").removeClass("selected");
			$(this).addClass("selected");
			$.get("/lib/dyn_content.php?" + this.href.split("#")[1], function(data){
				//alert(data);
				if(parseInt(data.split('|')[0]) >= 1){
					var project_name = data.split('|')[2];
					$("#project_name").empty();
					$("#project_name").append(project_name);
					var project_client = data.split('|')[1];
					$("#project_client").empty();
					$("#project_client").append(project_client);
					var project_image = data.split('|')[3];
					$("#project_image").attr("src", project_image);
				/*	$("#project_image").attr("alt", project_name);*/
					if(data.split('|').length > 4){
						var project_body = data.split('|')[4];
						$("#project_body").empty();
						$("#project_body").append(project_body);
					}
				}
			});
		});

		$("#faq_list a").bind("click", function(){
			$("#faq_list a").removeClass("selected");
			$(this).addClass("selected");
				
			$.get("/lib/dyn_content.php?" + this.href.split("#")[1], function(data){
				//alert(data);
				if(parseInt(data.split('|')[0]) >= 1){
					var project_name = data.split('|')[1];
					$("#faq_q").empty();
					$("#faq_q").append(project_name);
					var project_client = data.split('|')[2];
					$("#faq_a").empty();
					$("#faq_a").append(project_client);
				}
			});
		});
		$("#industryaward_form a.btn_go").bind("click", function(){
			form_data = $("#industryaward_form select").serialize();
			year = form_data.split("&")[0].split("=")[1];
			content_id = form_data.split("&")[1].split("=")[1];

			$.get("/lib/dyn_content.php?type=industryaward&i=" + content_id + "&year=" + year, function(data){
				//alert(data);
				$("#award_data").empty();
				if(parseInt(data.split('|')[0]) >= 1){
					$("#award_data").append(data.split("|")[1]);
				} else {
					$("#award_data").append("<h2>There are no awards for the selection you made.</h2>");
				}
			});
			return false;
		});
		if(document.getElementById('hemlockaward_form')){
			$("#hemlockaward_form a.btn_go").bind("click", function(){
				form_data = $("#hemlockaward_form select").serialize();
				year = form_data.split("&")[0].split("=")[1];
				content_id = form_data.split("&")[1].split("=")[1];
	
				$.get("/lib/dyn_content.php?type=hemlockaward&i=" + content_id + "&year=" + year, function(data){
					//alert(data);
					$("#award_data").empty();
					if(parseInt(data.split('|')[0]) >= 1){
						var project_name = data.split('|')[1];
						$("#project_name").empty();
						$("#project_name").append(project_name);
						var project_client = data.split('|')[2];
						$("#project_client").empty();
						$("#project_client").append(project_client);
						var project_image = data.split('|')[3];
						$("#project_image").attr("src", project_image);
						$("#project_image").attr("alt", project_name);
						if(data.split('|').length > 4){
							var project_body = data.split('|')[4];
							/*$("#award_content_container").empty();
							$("#award_content_container").append(project_body);*/
							$("#award_content_container #scroller").empty();
							$("#award_content_container #scroller").append(project_body);
						}
					}else{
						$("#award_data").append("<h2>There are no awards for the selection you made.</h2>");
					}
				});
				$('.scroll-pane').jScrollPane({showArrows:true});
				return false;
			});
			$("#hemlockaward_form #award_year").bind("change", function(){
				form_data = $("#hemlockaward_form select").serialize();
				year = form_data.split("&")[0].split("=")[1];
				category = form_data.split("&")[1].split("=")[1];
				$.get("/lib/pop_award_content.php?type=category&i=" + year, function(data){
					$("#hemlockaward_form #award_category").empty();
					if(parseInt(data.split('|')[0]) == 1){
						$("#hemlockaward_form #award_category").append(data.split("|")[1]);
					}
				});
			});
			/*$("#hemlockaward_form #award_category").bind("change", function(){
				form_data = $("#hemlockaward_form select").serialize();
				year = form_data.split("&")[0].split("=")[1];
				category = form_data.split("&")[1].split("=")[1];
				$.get("/lib/pop_award_content.php?type=year&i=" + category, function(data){
					$("#hemlockaward_form #award_year").empty();
					if(parseInt(data.split('|')[0]) == 1){
						$("#hemlockaward_form #award_year").append(data.split("|")[1]);
					}
				});
			});*/			

			$("#project_detail").bind("click", function(){
				$("#award_panel").animate({
					left: "-364px"
				},250);
				$("#hemlockaward_form").hide();		// the form needs to be hidden in IE6
				$("#award_panel a.btn_close").bind("click", function(){
					$("#award_panel").animate({
						left: "0px"
					},250);
					$("#hemlockaward_form").show();
					//$("#award_content_container").removeClass("custom_scroll_bar");
					//$("#award_content_container").removeClass("scroll-pane");
					return false;
				});
				//$("#award_content_container").addClass("custom_scroll_bar");
				//$("#award_content_container").addClass("scroll-pane");
				return false;
			});
			
			//$('#award_content_container').jScrollPane({scrollbarWidth:20, scrollbarMargin:10});
			$('.scroll-pane').jScrollPane({showArrows:true});
		}
	}


/*******************************************************************************
	NON-FUNCTIONS SCRIPTS
*******************************************************************************/
$(document).ready(function() {
	init();
});
//if (document.getElementById && document.getElementsByTagName && document.createElement) {
	//window.onload = init;
//}
