//play aroun with this vars alex

menu_animate_duration=500; // pretty self explanatory

$form_elements = new Array('full_name','email','enquiry');


//not under here

var activated_menu;


$(document).ready(function(){
   initTaveuni();
 
 });







function initTaveuni() {

	//$("#reservations_text > form").attr('target','_blank'); // fool the validator :P
	$("#contact_form").attr('target','_self'); // fool the validator :P


	$("#contact_form").submit(checkContactForm); //event handler for contact form
	
	$("#resForm").submit(checkResForm); 
	
	for(x=0;x<$form_elements.length;x++) { //attach events to form elements
	
	
	}
	
	
	
	$('#text1, #footer_text').css('opacity', 0.9999); //fix firefox 2 font rendering on a mac 
	
	$('#submitSlide').html('<input type="button" value="Check" class="submit_button" onClick="slideForm()" id="submitMore" />');
	
	$('#moreForm').css('display','none');
	
	
	
	$menu_items = new Array('accommodation','facilities','eco_activities','diving','location','rates_and_specials','contacts');
	//$("#menu_image").bind("mouseout", slideMenuLeft);
var ie6=false;
 ie6 = (
	window.external &&
	typeof window.XMLHttpRequest == "undefined"
);

//alert(ie6);

	if (ie6==false || typeof(ie6)=='undefined') { //ie 6 sucks

		for(x=0;x<$menu_items.length;x++) { //attach events to buttons
				$("#menu_" + $menu_items[x]).bind("mouseover", {which: $menu_items[x]}, slideMenuRight);
				$("#menu_" + $menu_items[x]+'_submenu').addClass('trans_submenu'); //set up classes for javascript use
				$("#menu_" + $menu_items[x]+'_submenu').css('marginLeft',0); //rewind them to the left;
		}

	}

	startSlideshow();

}





function slideForm() {

$('#submitMore').fadeOut(100);
$('#moreForm').slideDown(1000);
//$('#text1').animate({height: '448px' })
	
}

function resetClass(args) {
	
	$element=(args.data.element);
	$('#' + $element).removeClass();
	$('#error_text').text('').hide(100);
		
}


function highlightError(element) {

	$('#'+element).addClass('input_error');
	$('#'+element).focus();
	$('#'+element).one('keyup',{element: element},resetClass);
	//console.log(element);
}


function showError(text) {
	$('#error_text').text(text).show(100);
}


var email_re =/^(("[\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 ;

function checkContactForm() {
	$f = new Array();
	for($i=0;$i<$form_elements.length;$i++) {
		$f[$form_elements[$i]]=$('#id_' + $form_elements[$i]).attr('value');
		//alert($i + ' .... ' + $('#id_' + $form_elements[$i]).attr('value'));
	}
	
	var name_re =/^[a-z-'\s]{2,}$/i;
	if (!$f['full_name'].match(name_re)) {
		showError("Please enter your full name.");
		
		highlightError('id_full_name');
		return false;
  	}
		
	
	if (!$f['email'].match(email_re)) {
		showError("Please enter a valid email address."); 
		highlightError('id_email');
		return false;
  	}
		
		
	if ($f['enquiry']=='') {
		showError("Please enter an enquiry."); 
		highlightError('id_enquiry');
		return false;
  	}
	return true; // passed all regexs, now its server side time!!
}

















function checkResForm() {
	
	form = new Array();
	
	
	$('#resForm input[type=text]').each(function() {
																   
																   form[$(this).attr('id').replace('Id','')]=$(this).attr('value');
																   
		//debug($(this).attr('id'));
		
																   });
	
		
	
	

	

	
	var name_re =/^[a-z-'\s]{2,}$/i;
	if (!form['fullName'].match(name_re)) {
		showErrorRes("Please enter your full name.");
		//$('#fullNameId').focus();
		highlightErrorRes('fullNameId');
		return false;
  	}
		
	
	if (!form['email'].match(email_re) ) {
		showErrorRes("Please enter a valid email address."); 
		//$('#emailId').focus();
				highlightErrorRes('emailId');

		return false;
  	}
		
	/*	
	if (!form['contactPhone'].match(phone_re) || form['contactPhone']=='Number' ) {
		showError("Please enter your number."); 
		highlightError('contactPhone');
		return false;
  	}
	
	*/
	
	return true; // passed all regexs, now its server side time!!

	
}

function showErrorRes(errorText) {
	
	$('#errorBox').text(errorText).fadeIn(500);

	
}

function highlightErrorRes(element) {

	$('#'+element).addClass('input_error');
	$('#'+element).focus();
	$('#'+element).one('keyup',{element: element},resetClass);
	//console.log(element);
}







//////////////////////////////////////////////////////////
// ALEX's AWESOME TRANSITION GALLERY                    //
// with help from jQuery (http://www.jquery.com)        //
//////////////////////////////////////////////////////////


//variables .. edit them

var current_image =0; //image to start on
var slideshow_speed=3; // speed between changes in seconds
var fade_duration=2; //speed it fades between in seconds, this gets added onto the time between images
//var bg_colour = 'white'; // the background colour to load in

//don't edit under here sir

var duration_to_use=(fade_duration*1000)/2; //divide it between the two

function startSlideshow() {
    imgs = document.getElementById('slideshow_images').getElementsByTagName('img'); // get array of images to use
   
       $("#slideshow_panel").html('<img src="" id="slideshow_image" alt="" />'); //set an image element in div
        //$("#image_slideshow").css('background-color',bg_colour);


    setImage(imgs[current_image].src,imgs[current_image].alt); //call initial image
    
    //$("#slideshow_mask").animate({left:'0'},1000,'swing'); //swing in overlay
   
   if (imgs.length > 1) {  // if more than one image in gallery
    	
		setInterval("nextImage()", ((slideshow_speed+fade_duration)*1000)); //set interval
	}
}


function nextImage() {

    img_src = (imgs[current_image].src); // get uri of image
    img_alt = (imgs[current_image].alt); // get alt of image
    
    //alert (img_src);
    
				$('#slideshow_panel').css('backgroundImage','url(' + img_src +')');  //cross fade it!

	
    $("#slideshow_image").animate({opacity:'0.0'},duration_to_use); //fade it out
    $("#slideshow_image").queue(function () { //queue it up
       setImage(img_src,img_alt); // set new image
       $(this).dequeue(); // escape queue
    });
    $("#slideshow_image").animate({opacity:'1.0'},duration_to_use); // fade it back in
    
}


function setImage(uri,alt) {

	$("#slideshow_image").attr('src',uri); // set image's src
   	$("#slideshow_image").attr('alt',alt); // set image's alternate text
	
	//console.log(uri);

	//$("#slideshow_image").hide();

//$('#slideshow_panel').css('backgroundColor','red');

    if (current_image==imgs.length-1) { // if this is last image in set
        current_image=0; // reset image count
    } else {
        current_image++; // add to image count
    }



}





// MENU SYSTEM - 
// BY ALEX ... jQuery'd :P

function slideMenuRight(event) {

	if (typeof(slideDelay)!='undefined') { // if their is a timeout present
		clearTimeout(slideDelay);  // kill it
	}
	
	$div=(event.data.which); 

	if ($div!=activated_menu) { // if this is different to currently activated
	
		slideMenuLeft(); // slide previous menu
		activated_menu=$div; // set up this menu as active

	slideDelay = setTimeout('slideMenuRightDelay()',200); //delay to keep stress on rendering engine and jQuery to minimum
	//alert('slide');
	
	}


}


function slideMenuRightDelay() {
	//alert('#menu_' + activated_menu + '_submenu');



$('#menu_' + activated_menu + '_submenu').animate({ 
			 opacity: 1.0,
			marginLeft: "43px" // slide and animate it
			
		  }, menu_animate_duration);
	

}
	


function slideMenuLeft($div) {
	
	$div=activated_menu;


$('#menu_' + $div + '_submenu').animate({ 
         opacity: 0.0,
        marginLeft: "0px"
        
      }, menu_animate_duration);
	
}



function popUp(URL) {
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=1,width=450,height=425,left = 287,top = 159');");
}
