(function($) {
    $.fn.normaliseHeight = function() {
        var obj = $(this);
        var h = [];
        var sortNumber = function(a,b){
            return a - b;
        }
        $(obj).each(function(){
            h.push($(this).height());
        });
        h.sort(sortNumber);
        h.reverse();
        $(obj).each(function(){
            $(this).height(h[0]);
        });
    };
})(jQuery);

$(function(){
	$('a[rel=email]').each(function(){
		var ea = $(this).attr('href');
		var et = $(this).text();
		var em = (ea.indexOf(et)>-1) ? true : false;
		var ea = ea.replace(/#/,'@');
		var ea = ea.replace(/\*/g,'.');
		if (em) {
			$(this).text(ea.slice(7));
		}
		$(this).attr('href',ea);
	});
	
	if($.browser.msie){
		$('body').addClass('ie');
		if($.browser.version == '7.0'){
			$('body').addClass('ie7');
		}
	}
	
	//sets location based on userip
	$('#userip').each(function(){
		var ipadd = 'http://api.hostip.info/get_html.php?ip='+$(this).val();
		try {
			$.get(ipadd, function(data) {
				$('#userlocation').val(data);
			});
		} catch(err) {
		}
	});
	
	//sets platform from user-agent
	$('#userplatform').each(function(){
		var p = navigator.platform;
		$(this).val(p);
	});
	
	//displays full user-agent
	$('#useragent').each(function(){
		var os = navigator.userAgent;
		$(this).val(os);
	});
	
	$('#menu').superfish({delay: 100, animation: {opacity:'show',height:'show'}, speed: 'fast', autoArrows: false, dropShadows: false});
	
	$('a[rel=lightbox]').fancybox({titlePosition: 'inside'});
	
	$('ul.gallery a').fancybox({titlePosition: 'inside'});
	
	$('a.vidOverview').fancybox({titleShow:false});
	
	$.ajaxHistory.initialize();
	$('ul.navigation ul').each(function(){
		$(this).parent().addClass('subnav');
	});
	
	$('ul.navigation li.subnav a').toggle(function(){
		$(this).next().show();
		$(this).parent().addClass('visible')
		return false;
	}, function(){
		$(this).next().hide();
		$(this).parent().removeClass('visible')
		return false;
	});
	
	$('.compat td:contains("No")').addClass('no');
	$('.links li').normaliseHeight();
	$('.links li').click(function(){
		window.open($('a:eq(0)', this).attr('href'));
		return false;
	});
	$('.news li').click(function(){
		location.href = $('a:eq(0)', this).attr('href');
		return false;
	});
	
	$('ul.subsection').hide().each(function(){
		var list = $(this);
		$(list).prev().click(function(){
			$(this).next().toggle();
		});
	});
	
	$('.accordion').accordion();
	
	$('#tabcontainer').each(function(){
		if (location.hash) {
			var ctab = location.hash;
			var i = $('.tabs a').index($('.tabs a[href$='+ctab+']'));
			if (i==-1){
				i = 0;
			}
		} else {
			var i = 0;
		}
		$('.tabs li:eq('+i+')').addClass('selected');
		$('.tabpanel').hide().eq(i).show();
	});
	
	$('.tabs a').click(function(){
		var tc = $(this).parent();
		var c = $('.tabs a').index(this);
		var hr = $('.tabs a:eq('+c+')').attr('href');
		$('.tabs li').removeClass('selected').eq(c).addClass('selected');
		$('.tabpanel').hide().eq(c).show();
		return false;
	});

	$('.thickbox').openDOMWindow({ 
		eventType:'click', 
		width:780,
		height:350
	});
	
	$('.popup').each(function(){
		$('fieldset.step', this).slice(1).hide();
	});
	
	$('.errormsg').each(function(){
		$(this).change(function(){
			if($('option:selected', this).val() == 'Yes'){
				$(this).parent().next().show();
			} else {
				$(this).parent().next().hide();
			}
		});
	});
	$('.next a, .prev a').click(function(){
		var fs = $(this).parent().parent().parent().parent();
		$('.step:visible').removeClass('incomplete');
		$('.step:visible .required').removeClass('err');
		$('.step:visible .required').each(function(){
			if($(this).val() == ''){
				$(this).addClass('err');
				$(this).parent().addClass('err');
				$('.step:visible').addClass('incomplete');
			}
		});
		var s = $(this).attr('href');
		if($('.step:visible').hasClass('incomplete')){
			alert('Please complete all required fields');	
		} else {
			$('.step', fs).hide();
			$(s).show();
		}
		return false;
	});
	
	// Support form submit functions
	$('.formSupport').submit(function(){
		//var fs = $(this).parent().parent().parent().parent();
		$('.step:visible').removeClass('incomplete');
		$('.step:visible .required').removeClass('err');
		$('.step:visible .required').each(function(){
			if($(this).val() == ''){
				$(this).addClass('err');
				$(this).parent().addClass('err');
				$('.step:visible').addClass('incomplete');
			}
		});
		//var s = $(this).attr('href');
		if($('.step:visible').hasClass('incomplete')){
			alert('Please complete all required fields');
			return false;	
		} else {
			if($(this).attr('id')=='supportLevel1'){
				if($('.step:visible').attr('id') == 'level1step5'){
					//Assemble subject line
					var d = new Date();
					var uid = d.getDate().toString() + (d.getMonth()+1).toString() + d.getFullYear().toString() + ' / ';
					var platform = $('#level1os option:selected').text().indexOf('Windows') > -1 ? 'W / ' : 'M / ';
					var ver = $('#level1version option:selected').text().replace('Vectorworks ','').replace(' ','');
					var sl = 'L1 / '+ ver + platform + uid + $('#level1subject').val();
					//console.log(sl);
					$('#level1subject').val(sl);
					//console.log($('#subject1').val());
				} else {
					return false;
				}
			}
			
			if($(this).attr('id')=='supportLevel2'){
				if($('.step:visible').attr('id') == 'level2step5'){
					//Assemble subject line
					var d = new Date();
					var uid = d.getDate().toString() + (d.getMonth()+1).toString() + d.getFullYear().toString() + ' / ';
					var platform = $('#os option:selected').text().indexOf('Windows') > -1 ? 'W / ' : 'M / ';
					var ver = $('#version option:selected').text().replace('Vectorworks ','').replace(' ','');
					var sl = 'L2 / '+ ver + platform + uid + $('#level2subject').val();
					//console.log(sl);
					$('#level2subject').val(sl);
				} else {
					return false;
				}
			}
			
		}
	});
	
	
	//Set JS version of Price Request email
	$('#requestQuote #mail_options').val('PlainTemplate=price.txt,AlwaysList,TemplateMissing=');
	//Set JS version of server-side required fields
	$('#requestQuote #required_fields').val('email:Your email address,realname:Your name,licenses');
	
	// Price Request (Quote) form handler
	$('#requestQuote').submit(function(){
		var f = $(this);
		//error checking, zero and blank values
		$(this).removeClass('incomplete');
		//checks text fields
		$('input.required', this).each(function(){
			if($(this).val() == ''){
				$(this).addClass('err');
				$(this).parent().addClass('err');
				$(f).addClass('incomplete');
			}
		});
		//checks drop downs
		$('select.required', this).each(function(){
			if($(this).val() == 0){
				$(this).addClass('err');
				$(f).addClass('incomplete');
			}
		});
		//exit if the form is incomplete
		if($(this).hasClass('incomplete')){
			alert('Please complete all required fields');
			return false;
		} else {
		//assemble data and submit form
			var x=''
			$('.license_options', this).each(function(){
				var q = $('select:eq(1)', this).val();
				var m = $('select:eq(0)', this).val();
				x += q+' x '+m+'\n';
			});
			$('#requestQuote #licenses').text(x);
		}
	});
	
	// Demo Request form handler
	$('#requestDemo').submit(function(){
		var f = $(this);
		//error checking, zero and blank values
		$(this).removeClass('incomplete');
		//checks text fields
		$('input.required', this).each(function(){
			if($(this).val() == ''){
				$(this).addClass('err');
				$(this).parent().addClass('err');
				$(f).addClass('incomplete');
			}
		});
		//checks drop downs
		$('select.required', this).each(function(){
			if($(this).val() == 0){
				$(this).addClass('err');
				$(f).addClass('incomplete');
			}
		});
		//exit if the form is incomplete
		if($(this).hasClass('incomplete')){
			alert('Please complete all required fields');
			return false;
		} else {
		//assemble data and submit form
			$('#requestDemo #company').each(function(){
				if ($(this).val() == ''){
					$('#requestDemo input[name=mail_options]').val('PlainTemplate=dvd_noco.txt,AlwaysList,TemplateMissing=');
				}
			});
		}
	});

	//Remove additional license options
	$('.license_options').slice(1).remove();
	$('.license_clone').show();
	
	$('.module').change(function(){
		if ($('option:selected',this).hasClass('professional')){
			if($(this).next().hasClass('professional')){
				return false;
			} else {
				$(this).next().removeClass('education').addClass('professional').empty().append('<option value="0">Qty</option><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option><option value="6">6</option><option value="7">7</option><option value="8">8</option><option value="9">9</option><option value="10">10</option>');
			}
		} else if ($('option:selected',this).hasClass('education')) {
			if($(this).next().hasClass('education')){
				return false;
			} else {
				$(this).next().removeClass('professional').addClass('education').empty().append('<option value="0">Qty</option><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option><option value="6">6</option><option value="7">7</option><option value="8">8</option><option value="9">9</option><option value="10">10</option><option value="15">15</option><option value="20">20</option><option value="25">25</option><option value="30">30</option><option value="40">40</option><option value="50">50</option>');
			}
		}
	});
	
	$('.windowClose').click(function(){
		$.closeDOMWindow();
	});
	$('.section-layers').each(function(){
		var d = this;
		$('div', this).hide();
		$('.section-menu a', this).each(function(){
			$(this).click(function(){
				var l = $(this).attr('href');
				$('div', d).hide();
				$(l).fadeIn();
				return false;
			});
		});
	});
	$('.log tbody').each(function(){
		$('tr:even', this).addClass('even');
	});
	
	$('.faq tbody').each(function(){
		$('tr:odd', this).addClass('odd');
	});
	
	$('#courses .navigation a').click(function(){
		$('#courses .course').hide();
		var c = $(this).attr('href');
		$('#courses .navigation li').removeClass('selected');
		$(this).parent().addClass('selected');
		$(c).fadeIn();
		return false;
	});
	
	$('#courses').each(function(){
		if (location.hash) {
			var ctab = location.hash;
			var i = $('.navigation a').index($('.navigation a[href$='+ctab+']'));
			if (i>-1){
				$('.navigation li:eq('+i+')').addClass('selected');
				i++
			}
		} else {
			var i = 0;
		}
		$('.course').hide().eq(i).show();
	});
	
	$('#library .navigation a').click(function(){
		$('#library .videos').hide();
		var c = $(this).attr('href');
		$('#library .navigation li').removeClass('selected');
		$(this).parent().addClass('selected');
		$(c).fadeIn();
		return false;
	});
	
	$('#library').each(function(){
		if (location.hash) {
			var ctab = location.hash;
			var i = $('.navigation a').index($('.navigation a[href$='+ctab+']'));
			if (i==-1){
				i = 0;
			}
		} else {
			var i = 0;
		}
		$('.navigation li:eq('+i+')').addClass('selected');
		$('.videos').hide().eq(i).show();
	});
	
	$('.reviews .review:even').addClass('even-review');
	
	$('.add').click(function(){
		var fs = $(this).parent().parent().parent();
		$(fs).after($(fs).clone(true));
		if($('select:eq(1)', $(fs).next()).hasClass('education')){
			$('select:eq(1)', $(fs).next()).removeClass('education').addClass('professional').empty().append('<option value="0">Qty</option><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option><option value="6">6</option><option value="7">7</option><option value="8">8</option><option value="9">9</option><option value="10">10</option>');
		}
	});
	
	$('.remove').click(function(){
		var fs = $(this).parent().parent().parent();
		var fsl = $('fieldset.license_options').length;
		if(fsl>1){
			$(fs).remove();
		}
	});
	
});