var scrollCell, scrollCell2, contentHeight, contentHeight2, timer, timer2, lefttime, topStop, topStop2, theTimer, currentSong, videoFile, currentMenu;

// AJAX INIT STUFF -- START
var xmlhttp=null;

try
{
	xmlhttp = new XMLHttpRequest();
}
catch (error)
{
	try
	{
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	catch (error)
	{
		xmlhttp = null;
	}
}
//  END

function echeck(str){
 var filter=/^.+@.+\..{2,4}$/

 if (filter.test(str))
    result=true
 else {
    result=false
	}
 return (result) }


function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function changeBack(whichMenu){ //console.log(whichMenu);
	theTimer = setTimeout("hideMenu('"+whichMenu+"')", 800);
} 

function showMenu(whichMenu){
	clearTimeout(theTimer);
	$('#' + whichMenu).slideDown('fast');
}

function hideMenu(whichMenu){		
	$('#' + whichMenu).slideUp('fast');
}

function dropNav(){
	$("div.navigation > ul > li").hover(
		function () {
			if($(this).find('ul').attr('id') != currentMenu) $('#' + currentMenu).slideUp('fast');
			
			currentMenu = $(this).find('ul').attr('id'); //console.log(currentMenu);
			showMenu(currentMenu);
		}, 
		function () {
			changeBack(currentMenu);
		}
	);
	$("div.navigation ul li ul").hover(
		function () {
			clearTimeout(theTimer);
		}, 
		function () {
			changeBack(currentMenu);
		}
	);
}

function photogallery(){
	$('h2.photo-header').toggle( function() {
		$(this).parent().find('div.photos').slideDown('medium');
		$(this).find('span.plus').html('-');
	}, function() {
		$(this).parent().find('div.photos').slideUp('medium');
		$(this).find('span.plus').html('+');
	});
}

function corners(type){
	if(type == 'div.red-cell'){
		$(type).prepend('<span class="tl"><img src="/images/corners/rb-tr.jpg" width="10" height="10" /></span>');
		$(type).append('<span class="bl"><img src="/images/corners/rb-br.jpg" width="10" height="10" /></span>');
	}
	if(type == 'div.red-cell h2'){
		$(type).wrapInner('<strong />');
		$(type).prepend('<span class="tl"><img src="/images/corners/rr-tr.jpg" width="10" height="10" /></span>');
		$(type).append('<span class="bl"><img src="/images/corners/rr-br.jpg" width="10" height="10" /></span>');
	}
	if(type == 'div.gray-cell'){
		$(type).prepend('<span class="tl"><img src="/images/corners/bb-tr.jpg" width="10" height="10" /></span>');
		$(type).append('<span class="bl"><img src="/images/corners/bb-br.jpg" width="10" height="10" /></span>');
	}
	if(type == 'div.gray-cell h2'){
		$(type).prepend('<span class="tl"><img src="/images/corners/bg-tr.jpg" width="10" height="10" /></span>');
		$(type).append('<span class="bl"><img src="/images/corners/bg-br.jpg" width="10" height="10" /></span>');
	}
}

function checkForm(){
	var isGood = true;
	$('.req').each( function (){
		//alert($(this).attr('name'));
		var value = trim($(this).val());
		if(value == ''){
			$(this).parent(0).css({'color':'red'});
			isGood = false;
		}else{
			$(this).parent(0).css({'color':'#b79c71'});
		}
		if($(this).attr('name') == 'email' && !echeck(value)){
			$(this).parent(0).css({'color':'red',});
			isGood = false;
		}
	})
	
	if(!isGood){
		alert('Please fix RED errors above');
	}else{
		sendForm();
	}
	
	return isGood; //isGood;
}

function sendForm(){
	var formvars = $("#genform").serialize(); 
	
	xmlhttp.open("GET", "/ajaxHandler.php?action=contact-form&" + formvars + "&time=" + new  Date().getTime());
	xmlhttp.onreadystatechange = function(){
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
			var response = xmlhttp.responseText; //alert(response);
			document.getElementById('genform').innerHTML = '<p style="text-align:center; font-size:18px;"><br><br><br>Your message has been sent.<br><br>Thank You,<br>Che Ko Tae Kwon Do</p>';
		}
	}
	
	xmlhttp.send(null);
	
	return false; }

$(document).ready(function() {
	dropNav();
	photogallery();
	
	corners('div.red-cell');
	corners('div.red-cell h2');
	corners('div.gray-cell');
	corners('div.gray-cell h2');
//	$('div.gray-cell').corners('10px');
//	$('h2').corners('10px');
	//$('.video-thumb').corners('10px');
//	$('div.gallery div').corners('10px');
//	$('div.photos div').corners('10px');
	
	$(".video-thumb").colorbox({ inline:true, href:"#video"});
	$("a[rel='home']").colorbox();
	$("a[rel='set1']").colorbox();
	$("a[rel='set2']").colorbox();
	$("a[rel='set3']").colorbox();
	$(".vid").colorbox({ innerWidth:425, innerHeight:344});
	$(".vid1").colorbox({ inline:true, href:"#video1"});
	$(".vid2").colorbox({ inline:true, href:"#video2"});
	$(".vid3").colorbox({ inline:true, href:"#video3"});
	
	/*playerReady('player');
	showCD();
	video();*/
});
