/**
 * @author davisja
 */

function showNotice(argObj){
	this.html = argObj.html || "";
	this.cssClass = argObj.cssClass || "notice";
	this.duration = argObj.duration || 5000;
	
	if (this.html.length > 0){
		var newDiv = $("<div>").attr("id","msgDiv").attr("class",this.cssClass + " ui-corner-all").html(html);
		var wHeight = $(window).height();
		var wWidth = $(window).width();
		
		$("body").append(newDiv);
		$(newDiv).animate({opacity: 1.0}, this.duration).fadeOut('slow', function() {
			$(newDiv).remove();
		});
		var nWidth = (wWidth / 2) - ($(newDiv).width() / 2);
		if($.browser.msie) { /* IE Hack */
			var nHeight = document.body.scrollTop + 100;
		}else{
			var nHeight = window.pageYOffset + 100;	
		}
		
		$(newDiv).css({
			left		: 		nWidth + 'px',
			top			: 		nHeight + 'px'
		});
	}
}

function cartPop(action,PID){
	if (action = 'add'){
		$.ajax({
			url: '/ajax/ajaxController.cfm'
			,dataType: 'json'
			,cache: false
			,data: {PID: PID, action: 'cart', cartAction: 'insert'}
			,success: function(response){
				if (response.SUCCESS)
					//alert(response.MSG);
					//showNotice({html: response.MSG});
					showModal({
						titleText: "Add To Cart"
						,html: "Item Added To Cart"
						,showOverlay:false
						,buttons: {
							"View Shopping Cart" : function(){window.location.href='index.cfm?PG=cart&cm=view';}
							,"Continue": function(){$(this).dialog("close");}
						}
					});
				else
					//alert("Error Adding Item");
					//showNotice({html: response.MSG, cssClass: 'error', duration: 10000});
					showModal({
						titleText: "Error Adding Item"
						,html: response.MSG
					,showOverlay:false
						,buttons: {"Ok": function(){$(this).dialog("close");}}
					});
			}
		});
	}
	/*var cartPopWin = document.getElementById("cartWin");
	if(action == 'add'){
		ACAX(PID,'cartStatus');
		alert('The Item Has Been Added To Your Shopping Cart');
		//cartPopWin.style.top 	= document.body.clientHeight/2 - 150 + document.body.scrollTop + "px";
		//cartPopWin.style.left 	= document.body.clientWidth/2 - 55 + "px";		
		//cartPopWin.style.visibility = "visible";		
	};
	if(action == 'close'){cartPopWin.style.visibility = "hidden";};
	*/
}

function showModal( argObj ){		
	
	this.url = argObj.url || "";
	this.html = argObj.html || "";
	this.data = argObj.data || {};
	this.titleText = argObj.titleText || "";
	this.loadCallBack = argObj.loadCallBack || {};
	this.height = argObj.height || Math.max($(window).height()/2,300);
	this.width = argObj.width || Math.max($(window).width()/2,400);
	this.showOverlay = argObj.showOverlay || true;
	this.buttons = argObj.buttons || {};
	this.id = argObj.id || "GMPDialog";
	
	if (this.url.length + this.html.length == 0) return;
	
	
	if ($('#' + this.id).length == 0){
		var newDiv = $("<div>").attr("id",this.id).addClass('l');
		// Append it to the page
		$("body").append(newDiv);
	} else {
		var newDiv = $('#' + this.id);
	}
	
	var callback = this.loadCallBack; // need put in a variable since you can't call this.loadCallBack in the $.ajax success function

	if (this.html.length > 0 || this.url.length > 0){
		if (this.html.length > 0){
			$(newDiv).html(this.html);
		} else if (this.url.length > 0) {
			$.ajax({
				type: "POST"
				,url: this.url
				,data: this.data
				,success: function(html){
					// Append it to the page
					$(newDiv).html(html);
					callback();
				}
				,async : false
			 });
			 
		}
	
	
		// Create the Dialog box
		// ,overlay: {opacity: 0.50, background: "black"}
		if(!!this.showOverlay == true){
			$(newDiv).dialog({
				modal: true,
				title: this.titleText,
				width: this.width,
				height: this.height,
				overlay: {
					opacity: 0.50,
					background: "black"
				},
				buttons: this.buttons
			}); 				
		}else{
			$(newDiv).dialog({modal:true,title:this.titleText,width:this.width,height:this.height}); // ,overlay: {opacity: 0.50, background: "black"}
		}		
	}
}	

