/**
 * @author ardy
 */
$(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();
	});
	
	$("#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 = $("a#" + productCode + " input.product_name").attr("value");
	var downloadUrl = $("a#" + productCode + " input.download_url").attr("value");
	var fileSize =  $("a#" + productCode + " input.file_size").attr("value");
	
	$(".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_email_error").html('<img src="/en/images/components/ajaxload/round.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 signupNewsletter(email) {
	var action = '/oem/templates/requests/signupRequestBlueHornet.php';
	var cid = 'f836b6714c1873754f7512be18c2d499';
	var message = 'Your subscription has been registered. To ensure delivery of your newsletter(s), please add email@e.muvee.com to your address book. You can unsubscribe anytime by simply clicking on the \'unsubscribe\' link at the bottom of any of our newsletter.';
	var grp = $("input#grp").attr('value');
	
	$.post(action, { cid: cid, message: message, grp: grp, email: email }, 
		function(data) {
			process(data);
		}, "xml");
	
}

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');
	
}		