$(function() {
	//download box for home page
	$(".download_box").jqm({
		trigger: '.dl_box_trigger',
		modal: true				
	});

	$(".download_box").jqmAddClose('.download_box .close');
	
	//download box for product comaparison page
	$(".download_box").jqmAddTrigger(".pc_dl_try");	
	
	$(".pc_dl_try").click(function() {
		var productCode = $(this).attr("id");
		handleProductDownloads(productCode);
		
	});
	
	$('.dl_box_trigger, .pc_dl_try').click(function() {
		$('input[title!=""]').hint();
	});
	
	$(".download_now").click(function() {		
		$("form#dl_form").submit();
		return false;
	});
	
	$("#dl_form").ajaxForm({
		dataType:  'json', 
		async: false,
        beforeSubmit:  validateDownload, 
        success:       processJson
	});
	
	$(".fb_download").jqm({
		modal: true				
	});
	
	$(".fb_download").jqmAddClose('.fb_download .close');

	
	//BH newsletter signup
	$("#signup_form_download").ajaxForm({        
       
	});					
});

function handleProductDownloads(productCode) {	
	var productName = $("." + productCode + " input.product_name").attr("value");
	var downloadUrl = $("." + productCode + " input.download_url").attr("value");
	var fileSize =  $("." + productCode + " input.file_size").attr("value");
	
	if(productCode != 'reveal') {
		$('.download_box .trial_limitation_with_arrow').hide();
		$('.download_box .trial_limitation_box').hide();
	}else {
		$('.download_box .trial_limitation_with_arrow').show();
	}
	
	$(".download_box span.product_name").html(productName);
	$(".download_box span.file_size").html(fileSize);
	$(".download_box a.download_now").attr("title", "Download " + productName);	
	$(".download_box input#product").attr("value", productCode);
	$(".download_box input#downurl").attr("value", downloadUrl);	
	
}

function validateDownload(formData, jqForm, options) {
	var emailValue = $(".download_email").attr("value");
	
	if($(".download_email").attr("value") == $(".download_email").attr("title") || $(".download_email").attr("value") == '') {
		$(".download_email_error").html( i18n.translate("Please enter your email address.") );
		$(".download_email_error").show();				
		return false;
	}else if(!emailIsValid(emailValue)) {										
		$(".download_email_error").html( i18n.translate("Please enter a valid email address.") );
		$(".download_email_error").show();
		return false;
	}
	
	$("#download_form").html('<img src="/en/images/javascript/loadingmini.gif"/> <font>Downloading...</font>');
	
	if($.browser.msie && jQuery.browser.version == '6.0') {
		window.open($("#downurl").attr("value"),'Download','toolbar=0,location=no,directories=0,status=0,scrollbars=0,resizeable=0,width=1,height=1,top=0,left=0');
    	window.focus();				
	}
	
	//bluehornet newsletter subscription	
	if($("#ok_to_email").attr("checked") == true) {
		signupNewsletter(emailValue);		
	}	
		
	return true;
}

function processJson(data) {
	if(!($.browser.msie && jQuery.browser.version == '6.0')) {
		if ($.browser.msie) {
			window.open(data.location, 'Download', 'width=100,height=50,scrollbars=0,resizable=0,location=0')
		} else {
			$("body").append('<iframe frameborder="0" scrolling="no" src="'+ data.location +'" width="1" height="1"></iframe>');
		}
	}
	
	$("div.download_email_error").empty();
	$(".download_box").jqmHide();
	
	//successfully started the download
	pageTracker._trackEvent(/*category*/ 'SuccessfulDownload_' + $("input#product").attr("value"), /*action*/ location.pathname, /*label*/ 'scr_SuccessfulDownload');
	
	var downloadThanksHTML = '<div class="download_thanks"><h1 class="hello">' + i18n.translate('Thank you for downloading') + '</h1>' + 
			'<p style="width:100%; color: #333; font-size: 12px;  text-align: left;">' + i18n.translate('Your download should begin shortly. If your download does not start automatically, ') + 
			'<a style="color:#146792" href="' + data.location + '">' + i18n.translate('please click here') + '</a>.</p></div>';
			 
	if($("input#custom_thanks").attr("value") != 'true') {
		//use the default download form element for the thank you message
		$("#download_form").html(
			downloadThanksHTML		
		);	
	}
	$(".fb_download").jqmHide();
	
}

function emptyForm() {
	$('.download_email').val('');
	$('div.download_email_error').empty();
	$('input[title!=""]').hint();
	return false;
}
