﻿var strSessionId;
var strLocation = location.href;
var arrLocation = strLocation.split("/");
var strUploadPath = arrLocation[arrLocation.length-1]
strUploadPath = strUploadPath.replace(".aspx","").replace("#","") + "/uploadfiles.aspx";

$(document).ready(function() {

	if (window.devicePixelRatio) {
		$("head").append('<link href="/css/safari.css" rel="stylesheet" type="text/css" />');
	}

	startLogoFlash();
	startFrontPageFlash();
	startCaseFlash();

	$(document).pngFix();
		
	// AutoLabel
	
	$(".autoLabel").focus(function() {
		autoLabelStart(this);
	});
	
	$(".autoLabel").bind("change blur", function() {
		autoLabelEnd(this);
	});
	
	$(".autoLabelPassword").each(function() {
		autoLabelEnd(this);
	});
	
	$(".autoLabel").each(function() {
		if ($(this).val() != $(this).attr("title")) {
			$(this).addClass("autoLabelActive");
		}
	});
	
	

	$("#loginContainer input[type=checkbox]").checkbox({
		empty: '/css/graphics/main/checkbox-empty.png'
	});
	
	$(".formfield input").focus(function() {
		$(this).parents(".formfield").addClass("formfieldselected");
	});
	
	$(".formfield input").blur(function() {
		$(this).parents(".formfield").removeClass("formfieldselected");
	});
	
	$(".formfield textarea").focus(function() {
		$(this).addClass("selected");
	});
	
	$(".formfield textarea").blur(function() {
		$(this).removeClass("selected");
	});
	
	$(".fileCompleteItem a").live("click", function() {

		var strFile = $(this).attr("title");
	
		$.ajax({
			type: "GET",
			url: strUploadPath + "?mode=deletefile",
			data: "sessionid=" + strSessionId + "&file=" + strFile,
			success: function(msg){
				if (msg.substring(0,2) == 'OK') {
					$(".fileCompleteItem a[title='" + strFile + "']").parents(".fileCompleteItem").remove();
					updateUploadStatus(true);
				}
			}
		});
		return false;
	});
	
	startFileUpload();
	
	$("#deleteAllButton").click(function() {
		updateUploadStatus(true);
		$(".flUpload").fileUploadClearQueue();
		$.ajax({
			type: "GET",
			url: strUploadPath,
			data: "sessionid=" + strSessionId + "&mode=deleteall",
			success: function(msg){
				if (msg.substring(0,2) == 'OK') {
					$(".fileCompleteItem").remove();
				}
			}
		});
	});

});

var bQueueCreated = false;

function startFileUpload() {
	if ($("#uploadButton").size() > 0) {
		
		var intWidth = $("#uploadButton").width();
		var intHeight = $("#uploadButton").height();

		$("#uploadButton").show();
		$("#flashContainer").addClass("flashContainerStylesApplied");
		
		strSessionId = $("#btnSendInquiryContainer input[type='hidden']").val();
		var strFileUploadId = $(".flUpload").attr("id");

		$(".flUpload").fileUpload({
			'uploader': '/css/graphics/flash/uploader.swf',
			'script': strUploadPath,
			'scriptData': {'sessionid': strSessionId},
			'wmode': 'transparent',
			'displayData': '',
			'buttonImg': '/css/graphics/main/1x1-trans.gif',
			'width': intWidth,
			'height': intHeight,
			'multi': true,
			'auto': true,
			'cancelImg': '/css/graphics/main/form/upload/cancel-upload.gif',
			onComplete: function(event, queueID, fileObj, response, data) {
				
				var strFileName = response.replace(/\n/g,"").replace(/\s/gi,"");
				var strFileNameShort = strFileName;
				if (strFileNameShort.length > 40) {
					strFileNameShort = strFileNameShort.substring(0,40) + "...";
				}

				$(".fileCompleteItem").each(function() {
					if ($(this).find("a").attr("title") == strFileName) {
						$(this).remove();
					}
				});
				
				$("#fileUploadStatus").before("<div class=\"fileCompleteItem\"><div class=\"cancel\"><a href=\"#\" title=\"" + strFileName + "\"><img src=\"/css/graphics/main/form/upload/cancel-upload.gif\" /></a></div><span class=\"fileName\">" + strFileNameShort + "<span>(" + $("#uploaded").html() + ")</span></span></div>");
				$(".fileCompleteList").removeClass("hidden");
				
				updateUploadStatus(true);
				
				return true;
			},
			onSelectOnce: function() {
				if (!bQueueCreated){
					bQueueCreated = true;
					$(".fileList").prepend($(".fileUploadQueue"));
				}
				updateUploadStatus(true);
			},
			onProgress: function(event, queueID, fileObj, data) {
				if ($("#" + strFileUploadId + queueID + " .fileName span").size() == 0) {
					$("#" + strFileUploadId + queueID + " .fileName").append("<span>(" + $("#uploading").html() + ")</span>");
				}
					
				return true;
			},
			onAllComplete: function(event, data) {
				updateUploadStatus(true);
			}
		});

	}
}

function startLogoFlash() {
	var flashvars = {
		logolink: "/"
	};
	var params = {
		scale: "noscale",
		bgcolor: "#FFFFFF",
		allowscriptaccess: "always",
		wmode: "transparent"
	};
	var attributes = {};
	attributes.id = "content";
	attributes.name = "content";

	swfobject.embedSWF("/css/graphics/flash/logo.swf","logo","268","78","8.0.0","/css/graphics/flash/expressInstall.swf", flashvars, params, attributes);
}

function updateUploadStatus(bRunAgain) {

	var strUploadStatus = $("#uploadstatus").html();
	var intFilesUploading = $(".fileUploadQueueItem:visible").size();
	var intFilesUploaded = $(".fileCompleteItem").size();
	var intFilesIalt = intFilesUploaded + intFilesUploading;

	strUploadStatus = strUploadStatus.replace(/#1/g,intFilesUploaded).replace(/#2/,intFilesIalt)
	$(".fileCompleteList #fileUploadStatus").html(strUploadStatus);
	
	if (intFilesIalt == 0) {
		$("#deleteAllButton").hide();
	} else {
		$("#deleteAllButton").show();
	}
	
	if (intFilesIalt === 0) {
		$("#fileUploadStatus").hide();
	} else {
		$("#fileUploadStatus").show();
	}
	
	if (bRunAgain) {
		setTimeout("updateUploadStatus(false)",300);
	}
}

function startFrontPageFlash() {
	if ($("#frontPageFlash").size() > 0) {
		var strXmlLink = $("#frontPageFlash").attr("title");
		if (strXmlLink != '') {
			var flashvars = {
				xmlpath: strXmlLink
			};
			var params = {
				scale: "noscale",
				bgcolor: "#FFFFFF",
				allowscriptaccess: "always"
			};
			var attributes = {};
			attributes.id = "content";
			attributes.name = "content";

			swfobject.embedSWF("/css/graphics/flash/top.swf","frontPageFlash","900","224","8.0.0","/css/graphics/flash/expressInstall.swf", flashvars, params, attributes);
		}
	}
}

function startCaseFlash() {
	if ($("#frontCases").size() > 0) {
		var strXmlLink = $("#frontCases").attr("title");
		if (strXmlLink != '') {
		
			strXmlLink = strXmlLink.replace("\.aspx","") + "/casebanner.aspx";
	
			var flashvars = {
				xmlpath: strXmlLink
			};
			var params = {
				scale: "noscale",
				bgcolor: "#FFFFFF",
				allowscriptaccess: "always",
				wmode: "transparent"
			};
			var attributes = {};
			attributes.id = "content";
			attributes.name = "content";

			swfobject.embedSWF("/css/graphics/flash/casebox.swf","frontCases","363","163","8.0.0","/css/graphics/flash/expressInstall.swf", flashvars, params, attributes);
		}
	}
	
}

function autoLabelStart(objCaller) {
	var strTitle = $(objCaller).attr("title");
	
	if($(objCaller).val() == strTitle) {
	
		$(objCaller).addClass("autoLabelActive");
		
		if($(objCaller).hasClass("autoLabelPassword")) {
			var strId = $(objCaller).attr("id");
			var objContainer = $(objCaller).parent();
			var strField = objContainer.html();
			if ($(objCaller).val() == strTitle) {
				strField = strField.replace("type=\"text\"","type=\"password\"");
				strField = strField.replace("value=\""+ strTitle +"\"","value=\"\"");
				strField = strField.replace("value="+ strTitle,"value=\"\"");
				if (strField.match("type=\"?password")) {
				} else {
					strField = strField.replace("value=","type=\"password\" value=");
				}
				objContainer.html(strField);
				objContainer.find("input").val("");
			}
			setTimeout("$('#" + strId + "').bind('blur',function() {autoLabelEnd(this);}).focus()",100);
		} else {
			$(objCaller).val("");
		}
	}
}

function autoLabelEnd(objCaller) {
	var strValueNow;
	if($(objCaller).val() == "" || $(objCaller).val() == $(objCaller).attr("title")) {
		
		strValueNow = $(objCaller).val();
		
		if($(objCaller).hasClass("autoLabelPassword")) {
			var objContainer = $(objCaller).parent();
			var strField = objContainer.html();
			strField = strField.replace("type=\"password\"","type=\"text\"")
			strField = strField.replace("type=password","type=text")
			objContainer.html(strField);
			objContainer.find(".autoLabel").bind("focus",function() {
				autoLabelStart(this);
			});
			
			objContainer.find(".autoLabel").val(strValueNow);
			
			if(objContainer.find(".autoLabel").val() == "") {
				objContainer.find(".autoLabel").val($(objCaller).attr("title"))
			}
			
		} else {
			$(objCaller).val($(objCaller).attr("title"));
		}
	}

}