/*
 *  File Download Plugin
 */
 
(function($) { 
 
//Plugin
$.FileDownload = {
	click : function(e) {	
		var h = decodeURI($(this).attr("href").toLowerCase());
		var i = h.indexOf("/sitecore/content/common/");
		h = h.substr(i);
		$.FileDownload.start(h);
		return false;
	},
	start : function(resourcePath) {
		_gaq.push(['_trackPageview', '/_/actions/download/start']); 
		$.FileDownload._info = null;
		$.JSONService("Download")
			.ok($.FileDownload._gotInfo)
			.call("info", {path:resourcePath});
	},
	willResume : function() {
		return ($.FileDownload._info == null ? false : true);
	},
	
	//Private Fields & Methods
	_info : null,
	_gotInfo : function(info) {
		$.FileDownload._info = info;
		if($.FileDownload._info.IsProtected && !$.UserIsLoggedIn) {
			//$(".login", this).show();
			//$(".formats", this).hide();
			$.DialogController.show("/appendix/dialogs/login");
			return;
		}
		
		_gaq.push(['_trackPageview', '/_/actions/download/choose']); 
		$.DialogController.show("/Appendix/Dialogs/File Download");
	},
	_setupDialog : function() {
		if( !$.DialogController.isShowing("/appendix/dialogs/file download") ) {
			return;
		}
		
		//Show format options
		$(".login", this).hide();
		$(".formats", this).show();
		
		
		$(".button:not(.cancel):not(.login):not(.help)", this).hide().val("");
		$(".format-group", this).hide();
		
		var frmtCount = 0;
		var frmtUrl = null;
		var $firstFrmt = null;
		for(var format in $.FileDownload._info.Formats)
		{
			frmtCount++;
			if(frmtUrl == null) { frmtUrl = $.FileDownload._info.Formats[format] };
			
			var buttonClass = ".button." + format.replace(/\s+/ig, "-"); 
			var buttonUrl = encodeURI($.FileDownload._info.Formats[format]);
			
			var $fmt = $(buttonClass, this)
				.show()
				.attr("data-value", buttonUrl)
				.click($.FileDownload._choseFormat);
			$fmt.parent().show();
			if(frmtCount == 1) {
				$firstFrmt = $fmt;
			}
		}
	},
	_resumeAfterLogin : function() {
		if($.FileDownload._info != null) {
			$.DialogController.show("/Appendix/Dialogs/File Download");
		}
	},
	_choseFormat : function() {
		var url = decodeURI($(this).attr("data-value"));
		var i = url.lastIndexOf("/");
		var name = url.substr(i + 1);
		
		_gaq.push(['_trackPageview', '/_/actions/download/complete']);
		_gaq.push(['_trackEvent', 'Download', name]);
		
		$.JSONService("Download").ok(null).call("track", {id:url, name:name});
		$.DialogController.dismiss();
		$.FileDownload._info = null;
		window.open(url, "_blank");
	},
	_cleanup : function(e, name) {
		$.FileDownload._info = null;
	}
}

//Wireup
$(function() {
	//Bind To Links
	$("a[href^='/sitecore/content/Common/']").click($.FileDownload.click);
	$.JSONService().add("Download", "/netsmartz_v3/services/downloads.asmx");
	$(window).bind("login", $.FileDownload._resumeAfterLogin);
	$("#dialog-controller").bind("showDialog", $.FileDownload._setupDialog);
	$("#dialog-controller").bind("dismissDialog", $.FileDownload._cleanup);
	
	$("body").bind("mouseup", function(e) {
		var h = $(e.target).attr("href") || "";
		if( e.button == 2 && h.indexOf("/sitecore/content/Common/") != -1 ) {
			$(e.target)[0].oncontextmenu = function() {
                return false;
            }
			return false;
		}
	});
});
 
}(jQuery));
