$(function(){
	// LavaLamp menu
	$('ul.lavaLamp').lavaLamp();
	
	// AutoSuggest submit on topic selection
	$('.ui-autocomplete').click(function() {
		$('#keywordSearch').submit();
	})
		
	// Initialize text field "in use" changes
	$("input:text,input:password,select").live("focusin",function(){
		$(this).addClass("active");
	});
	$("input:text,input:password,select").live("focusout",function(){
		$(this).removeClass("active");
	});
	
	if (! jQuery.browser.webkit){
		$('input[title!=""]').hint();
	}
	
	// Form button rollover
	$(".button").live('mouseover mouseout', function(event){
		if($(this).hasClass("alt")){$(this).removeClass("alt");}
		else{$(this).addClass("alt");}
	});
	
	// Row rollover
	$(".results tr").hover(
		function(){
			if($(this).hasClass("highlight")){$(this).removeClass("highlight");}
			else{$(this).addClass("highlight");}
		},
		function(){
			if($(this).hasClass("highlight")){$(this).removeClass("highlight");}
			else{$(this).addClass("highlight");}
		}
	);
	
	// Featured items rollover
	$(".featuredItems img, .ads.rollover img").hover(
		function(){
			var newSrc = $(this).attr("src").replace("_bw\.jpg", ".jpg");
			$(this).attr("src", newSrc);
		},
		function(){
			var newSrc = $(this).attr("src").replace("\.jpg", "_bw.jpg");
			$(this).attr("src", newSrc);
		}
	);
	
	// Tab group rollover
	$(".tabGroup .tab img").mouseover(function(){
		$(".tabGroup .tab img").each(function(){
			var reString = /_bw\.jpg/;
			if($(this).attr("src").search(reString) == -1){
				var newSrc = $(this).attr("src").replace("\.jpg", "_bw.jpg");
				$(this).attr("src", newSrc);
				
				var tab = $(this).parent(".tab").attr("class").replace("tab ", "");
				tab = tab.replace(" first", "").replace(" last", "");
				$(".display." + tab).hide("fast");
			}
		});
		
		var newSrc = $(this).attr("src").replace("_bw\.jpg", ".jpg");
		$(this).attr("src", newSrc);
		
		var tab = $(this).parent(".tab").attr("class").replace("tab ", "");
		tab = tab.replace(" first", "").replace(" last", "");
		$(".display." + tab).show("fast");
	});	
	
	// Category/seminar menu slide out
	$(".categories .category").hover(
		function(){
			if(! $.browser.msie || parseFloat($.browser.version) >= 7){
				$(".topics ", $(this)).css({marginLeft:'-319px', display:'inline'});
				$(".slideout-topics").css({marginLeft:'-319px'});
				_hack_DontHideSlideoutBG = true;
			}else{
				$(this).css({backgroundColor:'#F29001'});
				$("a", this).css({color:'#333'});
			}
		},
		function(){
			if(! $.browser.msie || parseFloat($.browser.version) >= 7){
				$(".topics", $(this)).css({marginLeft:'0', display:'none'});
				
				_hack_DontHideSlideoutBG = false;
				window.setTimeout(function(){
					if(! _hack_DontHideSlideoutBG){
						$(".slideout-topics").css({marginLeft:'0'});
					}
				}, 50);
			}else{
				$(this).css({backgroundColor:'transparent'});
				$("a", this).css({color:'FFF'});
			}
		}
	);
	
	function showTopics(){
		$(".topics ", currentCategory).css({marginLeft:'-319px', display:'inline'});
	}
	// Initialize "Read details" buttons
	$(".readMore").toggle(
		function(){
			$(this).html("&ndash; Hide description");
			$(this).parent().children(".description").slideDown("fast");
		},
		function(){
			$(this).text("+ Read description");
			$(this).parent().children(".description").slideUp("fast");
		}
	);
	
	// Initialize "View more" buttons
	$(".viewMore").toggle(
		function(){
			$(this).html("&ndash; View less");
			$(this).parent().find(".extra").show("fast");
		},
		function(){
			$(this).text("+ View more");
			$(this).parent().find(".extra").hide("fast");
		}
	);
	
	// Initialize Session details
	$(".toggle_nav").toggle(
		function(){
			$(this).parent().children(".toggle_view").slideDown("fast");
			$(this).find(".flag").text("-");
		},
		function(){
			$(this).parent().children(".toggle_view").slideUp("fast");
			$(this).find(".flag").text("+");
		}
	);
	
	$("form.stateCity").relatedSelects({
		onChangeLoad: '/proxy/proxy.cfc?method=getCitiesByState&shortTitle=' + $("input[name='shortTitle']").val() + '&returnformat=json',
		selects:{
			'state': {loadingMessage:'Loading Cities'},
			'city': []
		}
	});
	
	// Cycle 
	$(".slide.hide").removeClass("hide");
	$(".home .main, .quoteBar").cycle({speed:1000, timeout:7000});
	
	// Audio/Webinar Listing Filter
	$(".eventListing .filters li").click(function(){
		var table = $(".listing.results");
		var filter = $(this).text();
		
		$(".listing.results tr").removeClass("alt");
		
		if(filter == "View Audio Conferences"){
			$(".listing.results tr.webinar").addClass("hide");
			$(".listing.results tr.audio").removeClass("hide");
		}else if(filter == "View Webinars"){
			$(".listing.results tr.audio").addClass("hide");
			$(".listing.results tr.webinar").removeClass("hide");
		}else{
			$(".listing.results tr.hide").removeClass("hide");
		}
		
		$(".listing.results tr:not(.hide):odd").addClass("alt");
	});

	// Form validator
	jQuery.validator.messages.required = "";
	$("form.validate").not("#frmEmailToAFriend").validate({
		invalidHandler: function(e, validator) {
			var errors = validator.numberOfInvalids();
			if (errors) {
				var message = errors == 1
					? 'You missed 1 field. Please correct below.'
					: 'You missed ' + errors + ' fields.  Please correct below.';
				$("div.error span").html(message);
				$("div.error").show();
			} else {
				$("div.error").hide();
			}
		},
		errorLabelContainer: "div.error ul",
		wrapper: "li",
		onkeyup: false,
		submitHandler: function(form) {
			$("div.error").hide();
			form.submit();
		},
		messages: {
			confirm_email: {
				required: " ",
				equalTo: 'The "E-mail Address" and "Confirm E-mail" fields must match.'	
			},
			email: {
				required: " ",
				email: "Please enter a valid e-mail address, example: you@yourdomain.com"
			}
		},
		debug:true
	});
	
	
	$("#frmEmailToAFriend").validate({
		invalidHandler: function(e, validator) {
			var errors = validator.numberOfInvalids();
			if (errors) {
				var message = errors == 1
					? 'You missed 1 field. Please correct below.'
					: 'You missed ' + errors + ' fields.  Please correct below.';
				$("div.error span").html(message);
				$("div.error").show();
			} else {
				$("div.error").hide();
			}
		},
		errorLabelContainer: "div.error ul",
		wrapper: "li",
		onkeyup: false,
		submitHandler: function(form) {
			$("div.error").hide();
			
			$.ajax({
				type: "post",
				url: "/proxy/proxy.cfc?method=emailToAFriend&returnformat=json",
				data: "from=" + $("#frmEmailToAFriend #emailFrom").val() +
						"&to=" + $("#frmEmailToAFriend #emailTo").val() +
						"&subject=" + $("#frmEmailToAFriend #emailSubject").val() +
						"&message=" + $("#frmEmailToAFriend #emailMessage").val(),
				success: function(data){
					if(data){$(".dialog.emailToAFriend").dialog("close");}
					else{$(".dialog.emailToAFriend").dialog("close");}
				}
			});
			
			return false;
		},
		debug:true
	});
	
	// Datepicker
	$(".date").datepicker();
	
	// Dialog box
	$(".dialog.emailToAFriend").dialog({autoOpen:false, closeOnEscape:true, modal:true, title:"Email to a Friend", width:400, height:375});
	
	$(".toggleEmailToAFriend").click(function(){
		$(".dialog.emailToAFriend").dialog("open");
	});

	$(".dialog.description").dialog({autoOpen:false, closeOnEscape:true, modal:true, title:"We recommend you also attend...", width:500, height:350});
	$(".toggleDialog").click(function(){
		$(".dialog").dialog("open");
	});	
	
	$(".cart-offer .dialog").dialog({autoOpen:false, closeOnEscape:true, modal:true, title:"BUNDLE AND SAVE!", width:300, height:250});
	$(".toggleReadMore").click(function(){
		$(".dialog").dialog("open");
	});	
	
	$(".btnCloseDialog").live("click", function(){
		$(".dialog").dialog("close");
	});

	$(".quickSearch").live("click", function(e){
		if($(".quickSearch .nextStep").length){
			$(".registerNowForm").show("slide");
			e.stopPropagation();
			e.preventDefault();
		}else{
			window.location = $(".quickSearch a").attr("href");
		}
	});
	
	$("#btnRegisterNowClose").bind("click", function(){
		$(".registerNowForm").hide("slide");
	});
	
	$("#registerNowTopic").bind("submit", function(){
		var action = $(this).attr("action");
		action += "/topic/" + $("#topicSelect").val();
		$(this).attr("action", action);
	});
	
	$("#btnSearchZip").bind("click", function(){
		$(".registerNowForm .state").val("");
	});
	
	$("#btnSearchState").bind("click", function(){
		$(".registerNowForm .zip").val("");
	});
});

function dump(arr,level) {
	var dumped_text = ""; 
	if(!level) level = 0;
	
	//The padding given at the beginning of the line.
	var level_padding = "";
	for(var j=0;j<level+1;j++) level_padding += "    ";
	
	if(typeof(arr) == 'object') { //Array/Hashes/Objects 
		for(var item in arr) {
			var value = arr[item];
			
			if(typeof(value) == 'object') { //If it is an array,
				dumped_text += level_padding + "'" + item + "' ...\n";
				dumped_text += dump(value,level+1);
			} else {
				dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
			}
		}
	} else { //Stings/Chars/Numbers etc.
		dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
	}
	return dumped_text;
}
