$(document).ready(function(){	
	
	// Opens links with rel="external" in a new window/tab
	$('a[rel*=external]').click( function() {
		window.open(this.href);
		return false;
	});
	
	// Opens screen shots on product detail pages in a fancybox
	if( jQuery.isFunction(jQuery.fn.fancybox) ){
		
		$("a.fancybox:not(.youtube)").fancybox();

		$("#call_back_trigger").fancybox({
			'type'			: 'ajax',
			'scrolling'		: 'no',
			'titleShow'		: false,
			'onClosed'		: function() {
			    $("#Message").hide();
			}
		});
		
		
		$("#call_back_form").live("submit",function(event){
				
			if ($("#fFirstname").val().length < 1 || $("#fSurname").val().length < 1 || $("#fTel").val().length < 1 || $("#fEmail").val().length < 1) {
			    $("#Message").show();
			    $.fancybox.resize();
			    return false;
			}
		
			$.fancybox.showActivity();
		
			$.ajax({
				type	: "POST",
				cache	: false,
				url		: "/index.php",
				data	: $(this).serializeArray(),
				success: function(data) {
					$.fancybox(data);
				}
			});
		
			return false;
		});

		$("a.fancybox.youtube").click(function() {
		$.fancybox({
				'padding'		: 0,
				'autoScale'		: false,
				'transitionIn'	: 'none',
				'transitionOut'	: 'none',
				'title'			: this.title,
				'width'			: 853,
				'height'		: 510,
				'href'			: this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
				'type'			: 'swf',
				'swf'			: {
				   	 'wmode'		: 'transparent',
					'allowfullscreen'	: 'true'
				}
			});
		
			return false;
		});
	
	}
	
/*
	$("#call_back_form").validate({
		submitHandler: function(form) {
			$.ajax({
				type	: "POST",
				cache	: false,
				url		: "/index.php",
				data	: $(this).serializeArray(),
				success: function(data) {
					$.fancybox(data);
				}
			});
	
			return false;
			//form.submit();
		} else {
			
			$("#Message").show();
		    $.fancybox.resize();
		    return false;
		
		}
		$.fancybox.showActivity();
	});
*/
	
	
	// Fast switching between online and offline product versions on the contact/freetrial form
	$("input.fMode").change(function(){
	    if ($("input.fMode:checked").val() == 'Online'){
	        $('#description_multiple').show();
	        $('#description_cdrom').hide();
	    } else if ($("input.fMode:checked").val() == 'CD-ROM') {
	    	$('#description_multiple').hide();
	        $('#description_cdrom').show();
	    }
    });
    
    // toggling of faq details on the support page
    // append links to the element directly preceding the element with a class of "toggle"
    $("dd.toggle").each(function (i) {
    	toggle_anchor = $(this).attr('rel');
    	$(this).prev().wrapInner('<a href="#'+toggle_anchor+'" name="'+toggle_anchor+'" class="toggle_trigger"></a>');
    });
	// hide all of the elements with a class of 'toggle'
	$('dd.toggle').hide();
	
	// capture clicks on the toggle links
	$('a.toggle_trigger').click(function() {
		// change the link text depending on whether the element is shown or hidden
		if ($(this).parent().hasClass("closed")) {
			$(this).parent().next('.toggle').slideDown('slow');
			$(this).parent().removeClass("closed");
			$(this).parent().addClass("open");
		} else {
			$(this).parent().next('.toggle').slideUp('slow');
			$(this).parent().removeClass("open");
			$(this).parent().addClass("closed");
		}
		
		// return false so any link destination is not followed
		return false;
	});
	
if( jQuery.isFunction(jQuery.fn.scrollTo) ){
    // replace # with #_ in all links containing #
    $('a[href*=#]').each(function () {
        $(this).attr('href', $(this).attr('href').replace('#', '#_'));
    });

    // scrollTo if #_ found
    hashname = window.location.hash.replace('#_', '');
    // find element to scroll to (<a name="">)
    elem = $('a[name="' + hashname + '"]');

    if(elem) {
         $(document).scrollTo(elem, 800);
         $('dd#t'+hashname).addClass("open");
         $('dd#d'+hashname).slideDown();
         console.log(hashname);
    }
}
	
	//toggle class on main menu dropdowns
	$("#main_nav > ul li")
		.mouseover(function(){
			$(this).addClass("hov");
		})
		.mouseout(function(){
			$(this).removeClass("hov");
		})
		

});
