// JavaScript Document

$(document).ready(function() {
	/**
	 * Symposium 2011 A Look Back video
	 */
	/*$("#hometube").click(function() {
		$.fancybox({
				'padding'		: 10,
				'autoScale'		: false,
				'transitionIn'	: 'none',
				'transitionOut'	: 'none',
				//'title'			: this.title,
				'width'			: 720,
				'height'		: 486,
				'href'			: this.href.replace(new RegExp("watch\\?v=", "i"), 'v/')+"",
				'type'			: 'swf',
				'swf'			: {
					'wmode'				: 'transparent',
					'allowfullscreen'	: 'true'
				}
				
			});
	
		return false;
	});
	*/
	
	
	
	var isIE7 = ($.browser.msie && parseInt($.browser.version, 10) == 7);
	
	
	$("#hometube").fancybox({
		'padding'		: 10,
		'transitionIn'	: 'none',
		'transitionOut'	: 'none',
		'type'			: 'iframe',
		'width'			: 720,
		'height'		: 488
		
	});
	//----end homepage youtube video
	
	
	
	
	var hcontent = 512;
	var currpage = 0;
	var currFormPage = 0;
	var bgtimer;
	var str_datauser = '';
	
	/*alert(GLOB_REL);
	if(GLOB_REL != undefined){
		currpage = GLOB_REL;
	}*/
	
	// Event handlers
	$.address.init(function(event) {
		var arMass = ['home', 'symposium', 'agenda', 'about', 'dca', 'lookingback', 'faqs', 'contact', 'register'];
		path = event.value.split('/').join("");
		//alert(path);
		for(i=0; i<arMass.length; i+=1){
			if(path == arMass[i]){
				//alert(path + "=" + (i+1) );
				
				var showpage = i;
		
				if(showpage == 8) resetForm();
				if(showpage == 9) resetForm(); //can't attend
				
				if(showpage != currpage) {
					$( "#mainmenu li:eq("+currpage+")" ).removeClass( 'alpha_btn' );
					
					//to top
					$( "#listcontent li.page:eq("+showpage+") div.contentborderpage" ).scrollTop(0);
					
					if ( isIE7 ) {
						$( "#listcontent li.page:eq("+showpage+")" ).css( 'top', '0px' );
						$( "#listcontent li.page:eq("+showpage+")" ).show();
						$( "#listcontent li.page:eq("+currpage+")" ).hide();
					}else{
						$( "#listcontent li.page:eq("+showpage+")" ).css( 'top', '-512px' );
						$( "#listcontent li.page:eq("+showpage+")" ).show();
						$( "#listcontent li.page:eq("+showpage+")" ).stop().animate({
							top: "0px"
						}, 1000 );
						$( "#listcontent li.page:eq("+currpage+")" ).hide();
					}
					
					currpage = showpage;
					
					
					//wait 1 sec before load background
					clearInterval(bgtimer);
					bgtimer = setTimeout("loadNewBg("+currpage+")", 1200);
					//loadNewBg( currpage );
					
					
					$( "#mainmenu li:eq("+showpage+")" ).addClass( 'alpha_btn' );
				}
				
			}
		}
		
		/*log('init: "' + event.value + '"');
		$('a').address(function() {
			return $(this).attr('href').replace(location.pathname, '');
		});
		*/
	});/*.change(function(event) {
		log('change: "' + event.value + '"');
		var text = (event.value == '') ? 'Home' : 
			event.pathNames[0].substr(0, 1).toUpperCase() + 
			event.pathNames[0].substr(1);
		$('a').each(function() {
			$(this).toggleClass('selected', $(this).text() == text);
		});
	}).internalChange(function(event) {
		log('internalChange: "' + event.value + '"');
	}).bind('externalChange', {msg: 'The value of the event is "{value}".'}, function(event) {
		log('externalChange: ' + event.data.msg.replace(/\{value\}/, event.value));
	});*/
	
	
	
	//set position of pages
	$( "#listcontent li.page" ).each(function(i){
		if(i != currpage) $(this).hide();
		$(this).css("top", i*(hcontent+84)+"px");
		
		if(i==8) $('#wrapper').show();
	});
	
	$( "#mainmenu li:first-child" ).addClass( 'alpha_btn' );
	/*$( "#mainmenu li:first-child a" ).css( 'padding-left', 44 );
	$( "#mainmenu li:last-child a" ).css( 'padding-right', 31 );*/
	
	$( "#mainmenu li:last-child" ).css( 'background', '#244a60' );
	$( "#mainmenu li:last-child a" ).css( 'color', '#ebeef1' );
	
	
	$( "#mainmenu li" ).mouseenter(function(){
		if($(this).attr("id") != "regbtn")
			$("a", this).css( 'color', '#1B74A8' );
    }).mouseleave(function(){
		if($(this).attr("id") != "regbtn")
			$("a", this).css( 'color', '#63534d' );
    });
	
	
	$( "#mainmenu li" ).click(function(event) { 
		event.preventDefault();
		var showpage = $(this).index();
		
		if(showpage == 8) resetForm();
		if(showpage == 9) resetForm(); //can't attend
		
		if(showpage != currpage) {
			$( "#mainmenu li:eq("+currpage+")" ).removeClass( 'alpha_btn' );
			
			//to top
			$( "#listcontent li.page:eq("+showpage+") div.contentborderpage" ).scrollTop(0);
			
			if ( isIE7 ) {
				$( "#listcontent li.page:eq("+showpage+")" ).css( 'top', '0px' );
				$( "#listcontent li.page:eq("+showpage+")" ).show();
				$( "#listcontent li.page:eq("+currpage+")" ).hide();
			}else{
				$( "#listcontent li.page:eq("+showpage+")" ).css( 'top', '-512px' );
				$( "#listcontent li.page:eq("+showpage+")" ).show();
				$( "#listcontent li.page:eq("+showpage+")" ).stop().animate({
					top: "0px"
				}, 1000 );
				$( "#listcontent li.page:eq("+currpage+")" ).stop().animate({
					top: "596px"
				}, 1000, function(){ $(this).hide(); } );
			}
			
			currpage = showpage;
			
			
			//wait 1 sec before load background
			clearInterval(bgtimer);
			bgtimer = setTimeout("loadNewBg("+currpage+")", 1200);
			//loadNewBg( currpage );
			
			
			$( "#mainmenu li:eq("+showpage+")" ).addClass( 'alpha_btn' );
		}
	});
	
	$( "a.abtn_reg" ).click(function(event) { 
		event.preventDefault();
		var showpage = parseInt( $(this).attr("goto"), 10 );
		
		resetForm();
		
		if(showpage != currpage) {
			$( "#mainmenu li:eq("+currpage+")" ).removeClass( 'alpha_btn' );
			
			if ( isIE7 ) {
				$( "#listcontent li.page:eq("+showpage+")" ).css( 'top', '0px' );
				$( "#listcontent li.page:eq("+showpage+")" ).show();
				$( "#listcontent li.page:eq("+currpage+")" ).hide();
			}else{
				$( "#listcontent li.page:eq("+showpage+")" ).css( 'top', '-512px' );
				$( "#listcontent li.page:eq("+showpage+")" ).show();
				$( "#listcontent li.page:eq("+showpage+")" ).stop().animate({
					top: "0px"
				}, 1000 );
				$( "#listcontent li.page:eq("+currpage+")" ).stop().animate({
					top: "596px"
				}, 1000, function(){ $(this).hide(); } );
			}
			currpage = showpage;
			
			//wait 1 sec before load background
			clearInterval(bgtimer);
			bgtimer = setTimeout("loadNewBg("+currpage+")", 1500);
			
			$( "#mainmenu li:eq("+showpage+")" ).addClass( 'alpha_btn' );
		}
	});
	
	$( "a.ontop" ).click(function(event) { 
		event.preventDefault();
		$(this).parents("div.contentborderpage").animate({ scrollTop: '0px' }, 'slow');
	});
	$( "a.ask" ).click(function(event) { 
		event.preventDefault();
		var obj = $( "h3#"+$(this).attr("goto") );
		var position = obj.position();
		var pos_sub = $(this).parents("div.subpage").position();
		$(this).parents("div.contentborderpage").animate({ scrollTop: position.top - pos_sub.top }, 'slow');
	});
	$( "a.ainvited" ).click(function(event) { 
		event.preventDefault();
		var obj = $( "p#"+$(this).attr("goto") );
		var position = obj.position();
		var pos_sub = $(this).parents("div.subpage").position();
		$(this).parents("div.contentborderpage").animate({ scrollTop: position.top - pos_sub.top }, 'slow');
	});
	
	
	
	
	//fancybox. speakers
	$("a.iframe").fancybox({
		'width'				: 1000,
		'height'			: 530,
		'type'				: 'iframe',
		'transitionIn'		: 'none',
		'transitionOut'		: 'none'
	});
	
	
	//-----
	// Form
	
	//set position of pages for Form
	$( "#form_ul li.formpage" ).each(function(i){
		if(i != currFormPage) $(this).hide();
		$(this).css("top", i*380+"px");
	});
	
	$( ".form_btn" ).click(function(event) { 
		event.preventDefault();
		
		var showpage = parseInt( $(this).attr("goto"), 10 );
		var isError = false;
		
		if( showpage==1 && $('input:radio[name=iwillattend]:checked').val() == undefined ) isError = true;
		
		if( showpage==2 ) {
			if($('select[name=prefix] option:selected').val() == '') {
				$('select[name=prefix]').next('.err').html('required'); isError=true;
			}else{
				$('select[name=prefix]').next('.err').html('');
			}
			
			validate_inputtext($('input[name=fname]')) ? isError=true : false ;
			validate_inputtext($('input[name=lname]')) ? isError=true : false ;
			validate_inputtext($('input[name=badge]')) ? isError=true : false ;
			validate_inputtext($('input[name=address]')) ? isError=true : false ;
			validate_inputtext($('input[name=city]')) ? isError=true : false ;
			validate_inputtext($('input[name=state]')) ? isError=true : false ;
			validate_inputtext($('input[name=zip]')) ? isError=true : false ;
			validate_inputtext($('input[name=phone]')) ? isError=true : false ;
			validate_inputtext($('input[name=fax]')) ? isError=true : false ;
			validate_inputtext($('input[name=mobile]')) ? isError=true : false ;
			
			// email
			if(!validate_email($('input[name=email]').val())) {
				$('input[name=email]').next('.err').html('Invalid email address'); isError=true;
			}else{
				$('input[name=email]').next('.err').html('');
			}
			if($('input[name=email]').val() != $('input[name=email2]').val()) {
				$('input[name=email2]').next('.err').html('Email addresses are not the same'); isError=true;
			}else{
				$('input[name=email2]').next('.err').html('');
			}
			
			validate_inputtext($('input[name=company]')) ? isError=true : false ;
			
			if($('input[name=industryother]').parent().css('display') != 'none')
				validate_inputtext($('input[name=industryother]')) ? isError=true : false ;
			
			validate_inputtext($('input[name=title]')) ? isError=true : false ;
			validate_inputtext($('input[name=department]')) ? isError=true : false ;
			
			if($('input[name=topthreeother]').parent().css('display') != 'none')
				validate_inputtext($('input[name=topthreeother]')) ? isError=true : false ;
			
			if($('#blockNeedRoom').css('display') != 'none') {
				
			}
			
			/*var ext = $('#so-reg-photo').val().split('.').pop().toLowerCase();
			if($.inArray(ext, ['gif','png','jpg','jpeg']) == -1) {
				alert('Invalid extension of file for upload!');
				isError=true;
			}*/

			
			
			//---------------------------
			
			if(isError){
				var obj = $("span.err:contains('required'):first");
				var position = obj.position();
				var p1=500;
				var p2=500;
				var p3=500;
				
				var objemail = $("span.err:contains('Invalid email address')");
				var position_email = objemail.position();
				
				var objemail2 = $("span.err:contains('Email addresses are not the same')");
				var position_email2 = objemail2.position();
				
				if(position != null) p1 = position.top;
				if(position_email != null) p2 = position_email.top;
				if(position_email2 != null) p3 = position_email2.top;
				
				var pos_sub = $(this).parents("div.divform").position();
				$(this).parents("li.formpage").animate({ scrollTop: Math.min(p1,p2,p3) - pos_sub.top - 10 }, 'slow');
			}else{
				if( $('input#so-reg-topthree17').is(':checked') )
					$('input#so-reg-topthree17').val( $('input[name=topthreeother]').val() );
				
				str_datauser = ''+
				'	  <b>Name Prefix:</b> '+$("select[name=prefix] option:selected").val()+
				'<br/><b>First Name:</b> '+$("input[name=fname]").val()+
				'<br/><b>Last Name:</b> '+$("input[name=lname]").val()+
				'<br/><b>Badge Name:</b> '+$("input[name=badge]").val()+
				'<br/><b>Business Phone:</b> '+$("input[name=phone]").val()+
				'<br/><b>Business Fax:</b> '+$("input[name=fax]").val()+
				'<br/><b>E-mail:</b> '+$("input[name=email]").val()+
				'<br/><b>Company/Organization:</b> '+$("input[name=company]").val()+
				'<br/><b>Hotel Room?</b> '+$("select[name=needroom] option:selected").val();
				
				if($("select[name=needroom] option:selected").val() == 'Yes'){
					str_datauser += ''+
					'<br/><b>Check-in Date:</b> '+$("select[name=checkin] option:selected").val()+
					'<br/><b>Check-out Date:</b> '+$("select[name=checkout] option:selected").val()+
					'<br/><b>Room Type:</b> '+$("select[name=room] option:selected").val()+
					'<br/><b>Will you be attending the welcome reception and dinner on Sunday, March 18?</b> '+$("select[name=welcome] option:selected").val();
					
					if($("select[name=dietary] option:selected").val() == 'Yes')
						str_datauser += ''+
						'<br/><b>Special Needs:</b> '+$("textarea[name=dietaryinfo]").val();
				}
				
				$('div#str_datauser').html(str_datauser);
			}
		}
		
		if(!isError && showpage != currFormPage) {
			if ( isIE7 ) {
				$( "#form_ul li.formpage:eq("+showpage+")" ).css( 'top', '0px' ).show();
				$( "#form_ul li.formpage:eq("+currFormPage+")" ).hide();
			}else{
				if(showpage > currFormPage) {
					$( "#form_ul li.formpage:eq("+showpage+")" ).css( 'top', '380px' ).show().stop().animate({top: "0px"}, 1000 );
					$( "#form_ul li.formpage:eq("+currFormPage+")" ).stop().animate({top: "-380px"}, 1000, function(){$(this).hide();});
				}else{
					$( "#form_ul li.formpage:eq("+showpage+")" ).css( 'top', '-380px' ).show().stop().animate({top: "0px"}, 1000 );
					$( "#form_ul li.formpage:eq("+currFormPage+")" ).stop().animate({top: "380px"}, 1000, function(){$(this).hide();});
				}
			}
			currFormPage = showpage;
		}
		
	});
	
	$('select[name=industry]').change(function() {
		if($('select[name=industry] option:selected').val() == 'Other') {
			$('input[name=industryother]').parent().show();
		}else{
			$('input[name=industryother]').parent().hide();
			$('input[name=industryother]').val('');
		}
	}).change();
	
	$('input#so-reg-topthree17').change(function() {
		if($(this).is(':checked')) {
			$('input[name=topthreeother]').parent().show();
		}else{
			$('input[name=topthreeother]').parent().hide();
			$('input[name=topthreeother]').val('');
		}
	}).change();
	
	$('input[name="topthree[]"]').change(function() {
		if($('input[name="topthree[]"]:checked').length == 3) 
			$('input[name="topthree[]"]:not(:checked)').attr("disabled", true);
		else
			$('input[name="topthree[]"]:not(:checked)').removeAttr("disabled");
	}).change();
	
	$('select[name=needroom]').change(function() {
		if($('option:selected', this).val() == 'Yes')
			$('#blockNeedRoom').show();
		else
			$('#blockNeedRoom').hide();
	}).change();
	
	$('select[name=dietary]').change(function() {
		if($('option:selected', this).val() == 'Yes') {
			$('textarea[name=dietaryinfo]').parent().show();
		}else{
			$('textarea[name=dietaryinfo]').parent().hide();
			$('textarea[name=dietaryinfo]').val('');
		}
	}).change();
	
	
	function resetForm() {
		var showpage = 0;
		
		//$('#so-reg-checkin option:eq(2)').selected(true);
		
		$(':input','#so-reg-form')
		 .not(':button, :submit, :reset, :radio, :checkbox, .noterase')
		 .val('')
		 .removeAttr('checked')
		 .removeAttr('selected');
		$('span.err').html('');
		
		$(':input','#so-cant-form')
		 .not(':button, :submit, :reset, :radio, :checkbox, .noterase')
		 .val('')
		 .removeAttr('checked')
		 .removeAttr('selected');
		$('span.err').html('');
		$('#extra_cant').css( 'display', 'none' );
		
		
		if(showpage != currFormPage) {
			$( "#form_ul li.formpage:eq("+showpage+")" ).show().css('top', "0px");
			$( "#form_ul li.formpage:eq("+currFormPage+")" ).css({top: "-380px"}).hide();
			currFormPage = showpage;
		}
		
		$("#so-reg-checkin option:last").attr('selected', 'selected');
	}
	
	function validate_inputtext(inptxt) {
		if(inptxt.val() == '') {
			inptxt.next('.err').html('required');
			return true;
		}else{
			inptxt.next('.err').html('');
			return false;
		}
	}
	
	
	/*$("#so-reg-form").submit(function(event) {
		event.preventDefault();
		$.post( $(this).attr( 'action' ), $(this).serialize(), 
		function (xml) {
			if( $(xml).find('status').text() == 'ok' ) {
				$('#str_datauser2').html(str_datauser);
				var showpage = 3;
				if(showpage != currFormPage) {
					$( "#form_ul li.formpage:eq("+showpage+")" ).css( 'top', '380px' ).show().stop().animate({top: "0px"}, 1000 );
					$( "#form_ul li.formpage:eq("+currFormPage+")" ).stop().animate({top: "-380px"}, 1000, function(){$(this).hide();});
					currFormPage = showpage;
				}
			}else{
				$('#str_datauser2').html($(xml).find('message').text());
			}
		}, "xml");
	});*/
	
	
	var options = { 
        //target:        '#output1',   // target element(s) to be updated with server response 
        //beforeSubmit: function(){ busyFormReg=true; }, //showRequest,  // pre-submit callback 
        success:       processXml,  // post-submit callback 
 
        // other available options: 
        //url:       url         // override for form's 'action' attribute 
        //type:      type        // 'get' or 'post', override for form's 'method' attribute 
        dataType:  'xml', //null        // 'xml', 'script', or 'json' (expected server response type) 
        //clearForm: true        // clear all form fields after successful submit 
        //resetForm: true        // reset the form after successful submit 
		
		iframe: true
		
		/*error: function(response, status, err){
			// This option doesn't catch any of the error below, 
			// everything is always 'OK' a.k.a 200
			alert(respons.status+" | "+err);
		}*/
 
        // $.ajax options can be used here too, for example: 
        //timeout:   3000 
    }; 

	
	$("#so-reg-form input[name=submit_btn]").click(function() {
		$(this).hide();
	});
	
    // bind form using 'ajaxForm' 
	
	
	$('#so-reg-form').ajaxForm(options);
	
	function processXml(xml) {
		$("#so-reg-form input[name=submit_btn]").delay(1000).show();
		if( $(xml).find('status').text() == 'ok' ) {
			$('#str_datauser2').html(str_datauser);
			var showpage = 3;
			if(showpage != currFormPage) {
				if ( isIE7 ) {
					$( "#form_ul li.formpage:eq("+showpage+")" ).css( 'top', '0px' ).show();
					$( "#form_ul li.formpage:eq("+currFormPage+")" ).hide();
				}else{
					$( "#form_ul li.formpage:eq("+showpage+")" ).css( 'top', '380px' ).show().stop().animate({top: "0px"}, 1000 );
					$( "#form_ul li.formpage:eq("+currFormPage+")" ).stop().animate({top: "-380px"}, 1000, function(){$(this).hide();});
				}
				currFormPage = showpage;
			}
		}else{
			//$('#str_datauser2').html($(xml).find('message').text());
			alert("" + $(xml).find('message').text());
		}
		
		// 'responseXML' is the XML document returned by the server; we use 
		// jQuery to extract the content of the message node from the XML doc 
		/*var message = $('message', responseXML).text(); 
		alert(message); */
	}
	
	
	
	
	
	
	
	$("#so-cant-form").submit(function(event) {
		event.preventDefault();
		$.post( $(this).attr( 'action' ), $(this).serialize(), 
		function (xml) {
			if( $(xml).find('status').text() == 'ok' ) {
				
				resetForm();
				$('#extra_cant').css( 'display', 'block' );
				
				/*
				$('#str_datauser2').html(str_datauser);
				var showpage = 3;
				if(showpage != currFormPage) {
					$( "#form_ul li.formpage:eq("+showpage+")" ).css( 'top', '380px' ).show().stop().animate({top: "0px"}, 1000 );
					$( "#form_ul li.formpage:eq("+currFormPage+")" ).stop().animate({top: "-380px"}, 1000, function(){$(this).hide();});
					currFormPage = showpage;
				}
				*/
			}else{
				//$('#str_datauser2').html($(xml).find('message').text());
				alert("" + $(xml).find('message').text());
			}
		}, "xml");
	});
	
	
	
	// on resize window and vertical align
	
	$(window).resize(function() {
		//$('body').prepend('<div>' + $(window).width() + '</div>');
		var newypos = 0;
		if($(window).height()-730 > 0) newypos = ($(window).height()-730)/2;
		$('#wrapper').parent().css('top', newypos);
	}).resize();
	
	
	
	
	
	
	
	
	
});//end jQuery








function validate_email(address) {
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	if(reg.test(address) == false) {
		return false;
	}else{
		return true;
	}
}

function flashMovie(movieName) {

if (window.document[movieName] != null)
	if (window.document[movieName].length == null)
		return window.document[movieName];
	else
		return window.document[movieName][1];
else
	if (document[movieName].length == null)
		return document[movieName];
	else
		return document[movieName][1];
/*

    var movie;
    try
    {
        movie = document[movieName];
        movie = (movie == null) ? window[movieName] : movie;        
    }
    catch (e)
    {
        return null;
    }
    return movie;
*/
}

function loadNewBg(num)
{
	var obj=flashMovie("myId");
	obj.loadBg(num);
	/*
	try {
		obj.loadBg(num);
    }
    catch (err) {
        alert("Error: " + err.toString());
    }
	*/
}

