$(document).ready(function(){
	var ori_label = $('#nav').html();
	
	jQuery.fn.rollover_mainmenu = function() {
		
		  $(this).oneTime(200, "open", function() {
			$(this).find('ul').eq(0).slideDown();
 			$(this).stopTime("hide");
		  });
  
		if ($(this).find('a').eq(0).attr('class')!='selected') {
			$(this).find('a').eq(0).css('color','#000');
		}
	}
	jQuery.fn.rollout_mainmenu = function() {
		$(this).stopTime("open");
		$(this).find('ul').eq(0).slideUp();
		if ($(this).find('a').eq(0).attr('class')=='selected') {
			$(this).find('a').eq(0).css('color','#3FBDF0');
		} else {
			$(this).find('a').eq(0).css('color','#999');
		}
	}
	jQuery.fn.rollover_mainmenu1 = function() {
		$(this).find('ul').eq(0).css('left',$(this).parent().width());
		$(this).find('ul').eq(0).css('top',0);
		//$(this).find('ul').eq(0).slideDown('fast');
		if ($(this).find('a').eq(0).attr('class')!='selected') {
			$(this).find('a').eq(0).css('color','#000');
		}
	}
	jQuery.fn.rollout_mainmenu1 = function() {
		//$(this).find('ul').eq(0).slideUp('fast');
		if ($(this).find('a').eq(0).attr('class')=='selected') {
			$(this).find('a').eq(0).css('color','#3FBDF0');
		} else {
			$(this).find('a').eq(0).css('color','#999');
		}
	}
	jQuery.fn.rollout_finishes = function() {
		$('#nav').html(ori_label+' / '+$(this).attr('alt'));
	}
	
	$('#mainmenu ul li').bind("mouseenter",jQuery.fn.rollover_mainmenu);
	$('#mainmenu ul li').bind("mouseleave",jQuery.fn.rollout_mainmenu);
	$('#mainmenu ul ul li').bind("mouseenter",jQuery.fn.rollover_mainmenu1);
	$('#mainmenu ul ul li').bind("mouseleave",jQuery.fn.rollout_mainmenu1);
	$('.finishes img').bind("mouseenter",jQuery.fn.rollout_finishes);
	
	if (path) {
		var path_arr = path.split('/');
		var my_path = '';
		for (var i=0; i<path_arr.length; i++) {
			my_path += ((my_path=='')?'':'_')+path_arr[i];
			$('#'+my_path).attr('class','selected');
		}
	}
	
	$('.slideshow').cycle({fx:'fade',startingSlide: 0,timeout:4000,cleartype:false, cleartypeNoBg:true});
});

function load_tv(file,title) {
	$('#overlay_title').html(title);
	$('#overlay_flash').load("backend.php", {func:'load_tv',file:file},function() {$('#overlay').slideDown();});
}

function close_tv(file) {
	$('#overlay_flash').html('');
	$('#overlay').slideUp();
}
function validate_form() {
	
	var input_arr = new Array('firstname','lastname','contact','receivenews');		
	return validate_input(input_arr);
	
}

function validate_input(input_arr) {
	for (i=0; i<input_arr.length; i++) {
		switch (input_arr[i]) {
			case 'firstname':			
				if (!validate_textbox("#firstname",'Please fill in First Name')) {
					return false;
				}
			break;
			case 'lastname':			
				if (!validate_textbox("#lastname",'Please fill in Last Name')) {
					return false;
				}
			break;
			case 'email':			
				if (!validate_email("#email",'Invalid Email')) {
					return false;
				}
			break;
			case 'contact':			
				if (!validate_email("#email",'') && !validate_textbox("#dayphone",'') && !validate_textbox("#nightphone",'') && !validate_textbox("#mobile",'')) {
					alert ('Please enter one of the contact methods: email, work phone, home phone or mobile');
					return false;
				}
			break;
			case 'receivenews':			
				if (!validate_radio(["#receivenews_0","#receivenews_1"],'Please select yes or no to recieve information from Cramer Property')) {
					return false;
				}
			break;
			case 'bedrooms':			
				if (!validate_select("#bedrooms",'Please specify apartment type')) {
					return false;
				}
			break;
			case 'referral':			
				if (!validate_select("#referral",'Please specify referral')) {
					return false;
				}
			break;
			case 'purpose':			
				if (!validate_select("#purpose",'Please specify purpose')) {
					return false;
				}
			break;
			case 'realpricerange':			
				if (!validate_select("#realpricerange",'Please specify price range')) {
					return false;
				}
			break;
		}
	}
	return true;
}
function start_home_slide() {
	$('.slideshow_off').removeClass().addClass('.slideshow').cycle({fx:'fade',startingSlide: 0,timeout:4000,cleartype:false, cleartypeNoBg:true});	
	return true;
}
function validate_textbox(my_id,msg) {
	if ($(my_id).val()==null||$(my_id).val()=="") {
		$(my_id).focus();
		if (msg !="") alert(msg);
		return false;
	}
	return true;
}
function validate_checkbox(my_id,msg) {
	if($(my_id+':checked').size() == 0){
		$(my_id).focus();
		if (msg !="") alert(msg);
		return false;
	}
	return true;
}
function validate_radio(my_id_arr,msg) {
	var check = false;
	for (var i=0; i<my_id_arr.length; i++) {
		if($(my_id_arr[i]+':checked').size() > 0){
			check = true;
		}
	}
	if (check) {
		return true;
	} else {
		$(my_id_arr[i]).focus();
		if (msg !="") alert(msg);
		return false;
	}
}
function validate_select(my_id,msg) {
	if($(my_id).val() == "nil"){
		$(my_id).focus();
		if (msg !="") alert(msg);
		return false;
	}
	return true;
}
function validate_email (my_id,msg) {
	var str = $(my_id).val();
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	var check= true;
	if (str.indexOf(at)==-1){
		check = false;
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		check = false;
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		check = false;
	}

	 if (str.indexOf(at,(lat+1))!=-1){
		check = false;
	 }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		check = false;
	 }

	 if (str.indexOf(dot,(lat+2))==-1){
		check = false;
	 }
	
	 if (str.indexOf(" ")!=-1){
		check = false;
	 }
	 if (check) {
		return true;
	 } else {
		$(my_id).focus();
		if (msg !="") alert (msg);
		return false;	
	 }
				
}