/*****************
Javascript/jQuery
Eran Finkle

*****************/


function jQ() {
	
	// var timeout;
	
	menu();		// menu events
	forms();
	
	// hide alt & title attr on mouseover on <a, img> tags
	$('a, img').hover(function() {
		if (this.alt) {
			$(this).attr('tAlt', this.alt);
			this.alt = '';
		}
		if (this.title) {
			$(this).attr('tit', this.title);
			this.title = '';
		}
	}, function() {
		if ($(this).attr('tAlt')) {
			this.alt = $(this).attr('tAlt');
			$(this).removeAttr('tAlt');
		}
		if ($(this).attr('tit')) {
			this.title = $(this).attr('tit');
			$(this).removeAttr('tit');
		}
	});
	
	// Policies menu actions
	$('#policytabs li').click(function() {
		if (!$(this).hasClass('selected')) {
			$('#policytabs li.selected').removeClass('selected');
			$(this).addClass('selected');
			$('#policydata li.active').removeClass('active');
			$('#policydata li#'+$(this).attr('tid')).addClass('active');
		}
	});
	
	// Policies FAQ actions
	$('#faqtabs li').click(function() {
		if (!$(this).hasClass('selected')) {
			$('#faqtabs li.selected').removeClass('selected');
			$(this).addClass('selected');
			$('#faqdata li.active').removeClass('active');
			$('#faqdata li#'+$(this).attr('tid')).addClass('active');
		}
	});
	
	// fake radio buttons - comment out to go back to the dafault
	var radio = $('input:radio');
	radio
		.filter(':checked').parent().addClass('checked').end().end()		// mark checked real elements
		.parent().addClass('radio')		// css radio image
		.click(function() {
			if (!$(this).hasClass('checked')) {		// act only if not checked
				radio.parent().removeClass('checked').children('input:radio').removeAttr('checked');		// remove all other radios
				$(this).addClass('checked').children('input:radio').click();
			}
		});

	// fake checkboxes - comment out to go back to the dafault
	var check = $('input:checkbox');
	check.filter(':checked').parent().addClass('checked');		// check off fake checkbox on load
	check
		.parent()
			.addClass('checkbox')		// css checkbox image
			.click(function() 
			{
				// force check off for IE, IE can't check invisible elements
				if ( (navigator.userAgent.indexOf('IE') != -1) ) {
					if ( $('input:checkbox', this).is(':checked') )
						$('input:checkbox', this).removeAttr('checked');
					else
						$('input:checkbox', this).attr('checked','checked');
				}
				
				if ( $('input:checkbox', this).is(':checked') )
					$(this).addClass('checked');
				else
					$(this).removeClass('checked');
			});

	// clear text from input fields with parent class 'clrtext'
	$('.clrtext input')
		.focus(function() {
			$(this).css({'color':''}).val('').unbind();
		})
		.each(function() {
			$(this).css({'color':'#ccc'})
		});
	
}

// menu functions
function menu() {
	var timeout;	// semi global for auto hide event
		
	// pre-cache images for instant loading
	{
		img = new Image();
		img.src = "img/login-bg-box.png";	// login bg box
		img = new Image();
		img.src = "img/form-inp-txt-login.png";	// input fields
		img = new Image();
		img.src = "img/login-b-login.png";	// login button
	}
	
	// top menu hover effect
	$('#nav_main > li.menu').hover(function() {				// bind hover only on menu items
		if (timeout) clearTimeout(timeout);						// clear auto hide
		$(this).addClass('mo');									// show submenu
	}, function() {
		var li = $(this);
		timeout = setTimeout(function() { li.removeClass('mo'); }, 1500);		// auto hide 1.5s delay
	});
	
	$('#nav_main > li').mouseenter(function() {		// when entering any menu item..
		if (timeout) clearTimeout(timeout);					// clear auto hide
		$('#nav_main > li').not(this).removeClass('mo');	// hide all open menus except the hovered one
		if ( $('#loginFormCont').is(':visible') && !$(this).is($('#ddLogin').parent()) ) 
			$('#ddLogin').click();		// hide login dropdown
	});

	// top menu Login dropdown	<li class="dd"><a id="ddLogin">
	$('#ddLogin').click(function() {
			var ul = $('#loginFormCont');	// form container
			if (ul.is(':hidden')) {
				$(this).parent().addClass('mo');
				ul.slideDown();
			} else {
				ul.slideUp();
				$(this).parent().removeClass('mo');
			}
				
			return false;		// disable standard click
		});
		// .mouseleave(function() {
			// if ( $('#loginFormCont').is(':visible') ) timeout = setTimeout(function() { $('#ddLogin').click(); }, 3000);
		// });
		
	$('#formLogin .close').click(function() { $('#ddLogin').click(); });	// bind X close login form

}

function forms() {
	// Contact us
	$('#formCus').submit(function() {
		var valid = true;
		$('.mand', this).each(function() {
			if (!this.value) {
				valid = false;
				return;
			}
		});
		
		if (!valid) {
			$('.non-valid', this).css('visibility','visible');
			return false;
		} else {
		
		}
	});
	
	// Beta Step 1
	$('#formBetaStep1').submit(function() {
		var valid = true;
		$('input[type="text"], input[type="password"]', this).each(function() {
			if (!this.value) {
				valid = false;
				return;
			}
		});
		
		if ( !$('input:checkbox', this).is(':checked') )
			valid = false;
		
		if (!valid) {
			$('.non-valid', this).css('visibility','visible');
			return false;
		} else {
			//location.href = 'beta-register-step-2.php';		// remove if using jsp
			//return false;		// remove if using jsp
		}
		
	});
	
	// Beta Step 2 - FAQ
	$('#faqnewadminButton').click(function() 
	{
		var conn = $('#faqnewadmin');
		if (conn.is(':hidden')) {
			$('#faqnewadmin').fadeIn();
			$('#container').append('<div id="overlay"></div>');
			$('#overlay').click(function() { $('#faqnewadminButton').click(); });
		} else {
			$('#faqnewadmin').fadeOut();
			$('#overlay').remove();
		}
	});
	
	$('#faqnewadmin .close').click(function() { $('#faqnewadminButton').click(); });
	
	$('#faqnewadminButton').click(function() {
		return false;
	});
	
	
    $('#formBetaStep2 input:radio').click(function() {
        var par = $(this).closest('p');
        $('#formBetaStep2 p').not(par).find('.man-set').slideUp();
        if (par.has('.man-set') && $('.man-set', par).is(':hidden'))
            $('.man-set', par).slideDown();
    });

	
	// Beta Step 2 - Check Connection
	$('#ChkConnButton').click(function() 
	{
		var myIP = document.getElementById("myIP");
		var theip = document.getElementById("theip");
		myIP.innerHTML = document.forms.formBetaStep2.srvip.value;
		theip.value = document.forms.formBetaStep2.srvip.value;
		
		var conn = $('#chkConnCont');
		if (conn.is(':hidden')) {
			$('#chkConnCont').fadeIn();
			$('#container').append('<div id="overlay"></div>');
			$('#overlay').click(function() { $('#ChkConnButton').click(); });
		} else {
			$('#chkConnCont').fadeOut();
			$('#overlay').remove();
		}
	});
	
	$('#chkConnCont .close').click(function() { $('#ChkConnButton').click(); });
	
	$('#chkConn').click(function() {
		return false;
	});
}
