$(document).ready(function() {

// --------- GLOBAL

	// external links popups
	$('.fb a, .tw a').click(function () {
		window.open($(this).attr('href'));
		return false;
	});

	// show + hide sitemap
	$('#sitemap-tab a').click(function () {
		if ($('#sitemap').attr("class") == 'down') {
			$('#sitemap').slideUp(500, 'easeInQuad');
		} else {		
			$('#sitemap').slideDown(700, 'easeOutQuad');
		}
		$('#sitemap').toggleClass('down');
		return false;
	});

	// Drop Dow - Sort by
	$('#dropdown ul').hover(
		function () {
			$('.#dropdown li').show();
			$('.#dropdown li.selected').hide();
		}, 
		function () {
			$('#dropdown li:not(.selected)').hide();
			$('.#dropdown li.selected').show();
		}
	);

// --------- Fancybox popup
	$(".product #main .btn_w a, .popup").fancybox({
		'overlayOpacity': 0.6,
		'zoomSpeedIn': 300,
		'zoomSpeedOut': 300
	});
	
	$("#pop_reg_id").fancybox({
		'overlayOpacity': 0.6,
		'zoomSpeedIn': 300,
		'zoomSpeedOut': 300,
		'frameHeight': 110
	});

// --------- INDEX

	// index highlights scrollbar
	$('#scrolling_area').jScrollPane({showArrows:true, scrollbarWidth:12, arrowSize: 12});

// --------- PRODUCT

	// products pagination
	// set some variables
	var per_page = 6;
	var items = $('.products .product').length;
	var pages = Math.ceil(items/per_page);
			start = 0;
			stop = per_page;
	// initial pagination
	$('.products .product').hide();
	$('.products .product').slice(0,per_page).show();
	// place pagination on page
	if (items > per_page) {
		paginator = '<div id="pagination">';
		paginator += '<p id="p_on">Page <span>1</span> of ' + pages + '<br /><a href="#off">(Show all on one page)</a></p>';
		paginator += '<p id="p_off"><a href="#on">Turn pagination back on</a></p>';
		paginator += '<ul>';
		paginator += '<li class="prev"><a class="disabled" href="#page_0">Previous</a></li>';
		paginator += '<li class="next"><a href="#page_2">Next</a></li>';
		paginator += '</ul>';
		paginator += '</div>';
		$('#product_listing').after(paginator);
		$('#p_off').hide();
	}
	// set event handler functionality
	$('#pagination ul a').click(function() {
		if (!$(this).hasClass('disabled')) {
			// get first/last item to show on page from pagination link
			current_page = parseInt(($(this).attr('href').split('#page_')[1]));
			start = (current_page-1)*per_page;
			stop = start+per_page;
			// show/hide items
			$('.products .product').hide();
			$('.products .product').slice(start,stop).show();
			// set current page number
			$('#pagination p span').html(current_page);
			// set previous/next links
			$('#pagination .next a').attr('href', '#page_' + (current_page+1))
			$('#pagination .prev a').attr('href', '#page_' + (current_page-1))
		}
		// set previous/next state else {
		$('#pagination ul a').removeClass('disabled');
		if ($(this).parent().hasClass('next') && current_page == pages) {
			$(this).addClass('disabled');
		} else if ($(this).parent().hasClass('prev') && current_page == 1) {
			$(this).addClass('disabled');
		}
		// alert(current_page);
		return false;
	});
	// turn pagination on/off
	$('#pagination p a').click(function() {
		if ($(this).attr('href') == '#off') {
			$('.products .product').show();
			$('#pagination ul').hide();
			$('#p_on').hide();
			$('#p_off').show();
		} else if ($(this).attr('href') == '#on') {
			$('.products .product').hide();
			$('.products .product').slice(start,stop).show();
			$('#pagination ul').show();
			$('#p_off').hide();
			$('#p_on').show();
		}
		return false;
	});

	// Product spec tabs
	// set default
	$('.product #tab_cnt div:not(#specs)').hide();
	$('.product .tabs li:first').addClass('selected');

	// switch on click function
	$('.tabs a').click(function () {
		// tabs
		$('.tabs li').removeClass('selected');
		$(this).parent().addClass('selected');
		// associated content
		$('.product #tab_cnt div').hide();
		var content_id  = ($(this).attr('href'));
		$(content_id).show();
		return false;
	});

	// Suite quote slideshow
	$('ul#quotes').innerfade({
		speed: 'slow',
		timeout: 8000,
		type: 'sequence',
		containerheight: '80px'
	});

// --------- SUPPORT

	// Tabs and related content
	// set default
	$('.faq #tab_section .main div').hide();
	$('.faq #tab_section .main div:first').show();
	// Questions + Answers in related content
	$('#tab_section dd').hide();
	$('#tab_section dl a').click(function() {
		var current_answer = $(this).parent().next();
		// only do something if the clicked question's answer is not showing already
		if (current_answer.is(':hidden')) {
		// if there's any answer already showing
			if ($('#tab_section dd:visible').length) {
				// slide them up before you slide new one down
				$('#tab_section dd:visible').slideUp(150, function() { current_answer.slideDown(200, 'easeInSine'); });
			} else {
				current_answer.slideDown(200, 'easeInSine');
			}
		}
		return false;
	});

	// Registration
	$('#survey-host-optional').hide();
	// Adds "other" input field for host software
    $('#survey-host').change(function () {
          $('#survey-host option:selected').each(function () {
			if ($(this).attr('value') == 'other') {
				$('#survey-host-optional').show();
				$('#survey-host-optional label').css({width: '205px', backgroundColor: '#fff696'}).animate({backgroundColor: '#fff'}, 1000);
			} else {
				$('#survey-host-optional').hide();
			}
		});
	});
	
	// validate signup form on keyup and submit
	$("#reg_form").validate({
		rules: {
			reg_name: { required: true, minlength: 2 },
			reg_email: { required: true, email: true },
			reg_sn: { required: true },
			reg_id: { required: true }
		},
		messages: {
			reg_name: {
				required: "",
				minlength: "2 characters minimum"
			},
			reg_email: {
				required: "Valid email required"
			},
			reg_sn: {
				required: ""
			},
			reg_id: {
				required: ""
			}
		}
	});

// --------- DOWNLOADS

	// LATEST VERSIONS
	// Make available Demo downloads depending on platform
	// CMS outputs all win + mac versions, we filter them via JS
	// save original state of product dropdown
	var all_demos = $('#demo_product').html();
	// disable product dropdown on load
	$('#demo_product').attr('disabled', 'disabled');
	// remove "select" from first option (leaving it in HTML for people without javascript)
	$('#demo_product option:eq(0)').empty();
	// show+hide options on change
	$('#demo_platform').change(function () {
		// restore original state of products before we filter products per platform
		$('#demo_product').html(all_demos);
		// scan platform dropdown
		$('#demo_platform option:selected').each(function () {
			// get selected platform
			var demo_platform = $(this).val();
			// scan products dropdown
			$('#demo_product option').each(function () {
				// find available product for selected platform
				if ($(this).attr('class') != demo_platform && $(this).val() != 'none') {
					$(this).remove();
				}
			});

			// HARDCODED version specific... hide v4 plugins if 64-bit OS is selected
			if (demo_platform == 'win64') {
				$('#demo_product optgroup.v4').remove();
			}

			// make sure a platform is selected
			if (demo_platform != 'none') {
				// remove the .error class added to first drop down by form submit if present
				$('#demo_platform').removeClass('error');
				// enable second drop down if platform is selected
				$('#demo_product').removeAttr('disabled');
				// make sure the first option is selected
				$('#demo_product option:eq(0)').attr('selected', 'selected');
			} else {
				$('#demo_product').attr('disabled', 'disabled');
				// removes the greyed out "selected" from product drop down
				$('#demo_product option:eq(0)').empty();
				// remove the .error class added to second drop down by form submit if present
				$('#demo_product').removeClass('error');
			}
		});
	});
	$('#demo_product').change(function () {
		// get the val of selected option and assign it to action of form
		$('#demo_product option:selected').each(function () {
			var file_dl = $(this).val();
			$('#demos').attr('action', file_dl);
		});
		if ($('demo_product').val() != 'none') {
			$('#demo_product').removeClass('error');
		}
	});
	// make sure user selects something from both drop downs before stating download
	$('#start_demo_dl').click(function () {
		if ($('#demo_platform').val() == 'none') {
			$('#demo_platform').addClass('error');
			return false;
		} else if ($('#demo_product').val() == 'none') {
			$('#demo_product').addClass('error');
			return false;
		}
	});

	// ARCHIVES
	// Make available archive downloads depending on platform
	// save original state of product dropdown
	var all_products = $('#archive_product').html();
	// disable product dropdown on load
	$('#archive_product').attr('disabled', 'disabled');
	// remove "select" from first option (leaving it in HTML for people without javascript)
	$('#archive_product option:eq(0)').empty();
	// show+hide options on change
	$('#archive_platform').change(function () {
		// restore original state of products before we filter products per platform
		$('#archive_product').html(all_products);
		// scan platform dropdown
		$('#archive_platform option:selected').each(function () {
			// get selected platform
			var platform = $(this).val();
			// scan products dropdown
			$('#archive_product option').each(function () {
				// find available product for selected platform
				if ($(this).attr('class') != platform) {
					$(this).remove();
				}
			});
			// make sure something's selected before enabling product dropdown
			if (platform != 'none') {
				// remove the .error class added to first drop down by form submit if present
				$('#archive_platform').removeClass('error');
				$('#archive_product').removeAttr('disabled');
				// make sure the first option is selected
				$('#archive_product option:eq(0)').attr('selected', 'selected');
				// set form action to download that first selected option is nothing else selected
				$('#archive').attr('action', $('#archive_product option:eq(0)').val());
			} else {
				$('#archive_product').attr('disabled', 'disabled');
			}
		});
	});
	// make sure user selects something from both drop downs before stating download
	$('#start_archive_dl').click(function () {
		if ($('#archive_platform').val() == 'none') {
			$('#archive_platform').addClass('error');
			return false;
		}
	});
	// get the val of selected option and assign it to action of form
	$('#archive_product').change(function () {
		$('#archive_product option:selected').each(function () {
			var archive_dl = $(this).val();
			$('#archive').attr('action', archive_dl);
		});
	});

// --------- CONTACT

	// add minimum word count method
	jQuery.validator.addMethod("minWords", function(value, element, params) { 
    return this.optional(element) || value.match(/\b\w+\b/g).length >= params; 
}, "Please enter at least {0} words."); 
	// validate signup form on keyup and submit
	$("#contact_form").validate({
		rules: {
			first_name: { required: true, minlength: 2 },
			sender_email: { required: true, email: true },
			msg_type: { required: true },
			message: { required: true, minWords: 10 }
		},
		messages: {
			first_name: {
				required: "Required Field",
				minlength: "2 characters minimum"
			},
			msg_type: "",
			sender_email: "Valid email required",
			message: {
				required: "Don't forget to enter your message!",
				minWords: "10 words minimum"
			}
		}
	});

// --------- LICENSING

	// Tabs
	// set default
	$('#tab_container .tab_content').hide();
	// set tab slected state and show content if anchor tag in URL
	// so you can link straight to specific tab
	// get the URL
	var current_url = document.location.toString();
	// if there is a anchor link
	if (current_url.match('#')) {
		// get anchor
		var current_tab = '#' + current_url.split('#')[1];
		// add class to tab in URL
		$('.licensing #tabs a').each(function () {
			var tabs_link = $(this).attr('href');
			if (tabs_link == current_tab) {
				$(this).parent().addClass('selected');
			}
		});
		// show content of tab in URL
		$(current_tab).show();
	// if no anchor in URL set to first tab
	} else {
		$('.tab_content:first').show();
		$('.licensing #tabs li:first').addClass('selected');
	}

	// switch on click function
	$('.licensing #tabs a').click(function () {
		// tabs
		$('.licensing #tabs li').removeClass('selected');
		$(this).parent().addClass('selected');
		// return MP3 player to link state on switch
		$('.player').remove();
		$('.mp3').show();
		// associated content
		$('.tab_content').hide();
		var content_id  = ($(this).attr('href'));
		$(content_id).show();
		// so page doesn't scroll to anchor ID
		return false;
	});

// --------- LICENSING

	// mp3s
	// embed flash player when clik on mp3 link
	$('.mp3 a').click(function() {
		var mp3_link = $(this).attr('href');
		var player_url = 'http://wavearts.com/_img/mediaplayer.swf';
		var flash_vars = 'frontcolor=0x4d7ea1&backcolor=0xffffff&lightcolor=0x3c8ac4&showdigits=false&autostart=true&width=210&height=20';
		var flash_obj = '<li class="player"><object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="210" height="20" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab"><param name="movie" value="' + player_url +'" /><param name="wmode" value="transparent" /><param name="flashvars" value="' + flash_vars + '&file=' + mp3_link + '" /><embed width="210" height="20" flashvars="' + flash_vars + '&file=' + mp3_link + '" src="' + player_url +'" type="application/x-shockwave-flash" /></object></li>';
		$(this).parent().after(flash_obj);
		$(this).parent().hide();
		return false;
	});

});
