﻿$J(function(){

	initClearInputUsername();
	initClearInputSearch();
	initClearPassword();   
  $J("input.num").numeric();
	initFlashBorders() ;
	initBoxMenu() ;
	initTicker() ;
	initCorpMembers() ;
	checkBrowserWidth() ;
  window.onresize = checkBrowserWidth;
  
  
  initAdmBtns() ;
  
	$J("body").addClass("hasJs");
		
	if(	$J("#linkIET").length > 0){ //if the element exists
	    if (typeof ZZAjaxRequests != "undefined"){
	        ZZAjaxRequests.IsMemberPaying(onSuccessIsMemberPaying);
	    }
	}
});




/* Admin box menu
===================================================================================== */
function initBoxMenu() {
	$J(".boxTree a.ctl00_MainContent_EditMenu_1").click (function() { return false ; });
}


/* ticker
===================================================================================== */
var over = false;
function initTicker() {

	// send ie 6 running
	if (typeof document.body.style.maxHeight == "undefined") return ;
	
	// remove admin wrapper
	if ($J(".ticker div.admContent")[0]) {
		$J(".ticker div.admContent").children().clone().insertAfter($J(".ticker div.admContent"));
		$J(".ticker div.admContent").remove();
	}
	
	var wrap = $J(".ticker > div");
	//wrap.find("> a").remove();
	wrap.each(
		function() {
			if ($J(this).height() > $J(this).parent().height()) {
				tick($J(this));
			}
		}
	);
}
function tick(wrap) {

	var item = wrap.find("div:eq(0)");
	var h = item.height();
	var s = parseInt(item.css("margin-top"));
	
	var top = parseInt(wrap.css("top"))
	if (isNaN(top)) { top = 0 };
	top = top-h-s;
	
	wrap.pause2(6000).animate({top: top}, 700, "easeout", function() {
		wrap.pause2(2000).css("padding-top", parseInt(wrap.css("padding-top"))+h+s)
		wrap.append(item);
		tick(wrap);
	});
}


/* corporate members
===================================================================================== */
function initCorpMembers() {

	// send ie 6 running
	if (typeof document.body.style.maxHeight == "undefined") return ;
	
	$J("ul.corpMembers li.on").pause2(4000).fadeOut (
		"slow",
		function(){
			$J(this).removeClass("on");
			var li = $J(this).next() ;
		  if (!li.find("img").attr("src")) {
				li = $J(this).parent().find("li:first-child") ;
			}
		  li.fadeIn (
				"slow",
				function() {
					li.addClass("on");
					initCorpMembers() ;
				}	
			)
		}
	);
}


/* New clear input
===================================================================================== */
function initClearInputUsername() {
	
	var fields = ".username" ;

	$J(fields).attr("default", "username:") ;
	$J(fields).focus ( function() {		
		if ($J(fields).val() == $J(fields).attr("default")) 
			$J(fields).val("") ;
	})
	$J(fields).blur ( function() {
		if ($J(fields).val() == "") 
			$J(fields).val($J(fields).attr("default")) ;
	})
}

function initClearInputSearch() {
	
	var fields = ".keywords" ;

	$J(fields).attr("default", "keywords:") ;
	$J(fields).focus ( function() {		
		if ($J(fields).val() == $J(fields).attr("default")) 
			$J(fields).val("") ;
	})
	$J(fields).blur ( function() {
		if ($J(fields).val() == "") 
			$J(fields).val($J(fields).attr("default")) ;
	})	
}


function initClearPassword() {
    if ($J(".head input.password").val() == "") {
        $J(".head input.password").css("display", "none");
        $J(".head input.passwordFake").css("display", "inline");
    }
    else {
        $J(".head input.passwordFake").css("display", "none");
        $J(".head input.password").css("display", "inline");	        
    }
	
    $J(".head input.password").blur(
        function() {
            if ($J(this).val() == "") {
	            $J(this).css("display", "none");
	            $J(".head input.passwordFake").css("display", "inline");		        
	        }
        }
    );
    
    $J(".head input.passwordFake").focus(
        function() {
            $J(this).css("display", "none");
            $J(".head input.password").css("display", "inline");
            $J(".head input.password").focus();		           
        }
    );
}

/* Window Size Layout
===================================================================================== */
function checkBrowserWidth() {
	// send ie 6 running
	if (typeof document.body.style.maxHeight == "undefined") return ;
	var w = getBrowserWidth() ;
	if (w < 985)	{
		setStylesheet("800");
	}	else {
		setStylesheet("1024");
	}
	return true;
}

function getBrowserWidth() {
	if (window.innerWidth) {
		return window.innerWidth;
	}	else if (document.documentElement && document.documentElement.clientWidth != 0)	{
		return document.documentElement.clientWidth;
	}	else if (document.body)	{
		return document.body.clientWidth;
	}	
	return 0;
}

function setStylesheet(styleTitle) {
	var currTag;
	if (document.getElementsByTagName) {
		for (var i = 0; (currTag = document.getElementsByTagName("link")[i]); i++) {
			if (currTag.getAttribute("rel").indexOf("style") != -1 && currTag.getAttribute("title")) {
				currTag.disabled = true;
				if(currTag.getAttribute("title") == styleTitle) {
					currTag.disabled = false;
				}
			}
		}
	}	
	return true;
}

/* Flash Border Removal
===================================================================================== */
function initFlashBorders(){
    if ($J.browser.msie){
        $J("object[@type='application/x-shockwave-flash']").each(function(){
                var h = this.outerHTML ;
				var params = '' ;
				var j = this.childNodes.length - 1 ;
				do {
					var p = this.childNodes[j] ;
					if (p.tagName == "PARAM") params += p.outerHTML ;
				} while (j--) ;
				var tag = h.split(">")[0] + ">" ;
				this.outerHTML = tag + params + this.innerHTML + " </object>" ;
            });
            
        $J("object[@type='application/x-xstandard']").each(function(){
                var h = this.outerHTML ;
				var params = '' ;
				var j = this.childNodes.length - 1 ;
				do {
					var p = this.childNodes[j] ;
					if (p.tagName == "PARAM") params += p.outerHTML ;
				} while (j--) ;
				var tag = h.split(">")[0] + ">" ;
				this.outerHTML = tag + params + this.innerHTML + " </object>" ;
            });
    }
}


/* Admin Buttons (shared for login)
===================================================================================== */
function initAdmBtns() {
    $J(".btnAdm").wrap('<div class="btnAdm"></div>') ;
    $J("input.btnAdm, a.btnAdm").removeClass('btnAdm') ;
}

/* IET registration
===================================================================================== */
function onSuccessIsMemberPaying(response){
    if(response.value == true){
        ZZAjaxRequests.GetIETLink(onSuccessGetIETLink);
	}
	else{
	    $J("#linkIET").html('<a href="/Membership/Renew-your-membership">Please register as a paying member</a> and then update this page');
	}
}

function onSuccessGetIETLink(response){
    $J("#hrefIET").attr("href", response.value);
}

function showLink(){
    if($J("#cbAgree").attr("checked") == 1){
        $J("#linkIET").css("display", "block");
    }
    else{
        $J("#linkIET").css("display", "none");
    }
}