
$(function(){

	$(window).load(function() {
		registerWindowHasLoaded();
	});
	
});

$.fn.centerDialogToElement = function(arguments) {
	arguments = $.extend({
		horizontal: "",
		vertical: "",
		relativeTo: "document",
		top: 0,
		right: 0,
		bottom: 0,
		left: 0
	}, arguments);
	
	if (!$(this).exists()) {
		return false;
	}
	
	if (arguments.horizontal.length > 0) {
		
		if (typeof(arguments.right) == "string") {
			arguments.right = arguments.right.replace(/px/g,"");
		}
		if (typeof(arguments.left) == "string") {
			arguments.left = arguments.left.replace(/px/g,"");
		}
		
		var elementWidth = arguments.horizontal.getTotalWidth({margin:false});
		var elementLeftOffset = (arguments.relativeTo.substr(0,1)=="p")?arguments.horizontal.position().left:arguments.horizontal.offset().left;
		
		var dialogWidth = $(this).getTotalWidth({margin:false});
		
		if (dialogWidth > 0) {
			var dialogOffset = elementLeftOffset + ((elementWidth - dialogWidth) / 2.0) + parseInt(arguments.left) - parseInt(arguments.right);
			$(this).css({
				position: "absolute",
				left: dialogOffset.toFixed(0) + "px"
			});
		}
		
	}
	
	if (arguments.vertical.length > 0) {
        
		if (typeof(arguments.top) == "string") {
			arguments.top = arguments.top.replace(/px/g,"");
		}
		if (typeof(arguments.bottom) == "string") {
			arguments.bottom = arguments.bottom.replace(/px/g,"");
		}
		
		var elementHeight = 0;
		var elementTopOffset = 0;
        
        if (arguments.vertical == 'viewport') {
            elementHeight = $(window).height();
        } else {
            elementHeight = arguments.vertical.getTotalHeight({margin:false});
            elementTopOffset = (arguments.relativeTo.substr(0,1) == "p") ? arguments.vertical.position().top : arguments.vertical.offset().top;
        }
        
		var dialogHeight = $(this).getTotalHeight({margin:false});
		
		if (dialogHeight > 0) {
			var dialogOffset = elementTopOffset + ((elementHeight - dialogHeight) / 2.0) + parseInt(arguments.top) - parseInt(arguments.bottom);
			$(this).css({
				position: "absolute",
				top: dialogOffset.toFixed(0) + "px"
			});
		}
		
	}
	
	return true;
	
}

$.fn.centerTo = function(arguments) {
	arguments = $.extend({
		horizontal: "",
		vertical: "",
		relativeTo: "document",
		top: 0,
		right: 0,
		bottom: 0,
		left: 0
	}, arguments);
	
	if (!$(this).exists() || (arguments.horizontal.length == 0 && arguments.vertical.length == 0)) {
		return false;
	}
	
	var dialog = $(this);
	dialog.centerDialogToElement(arguments);
	
	var resizeArgs = arguments;
	
	// Center on window resize
	$(window).bind("resize", function() {
		dialog.centerDialogToElement(resizeArgs);
	});
	
	return true;
	
}

/**
 *    src: http://www.mail-archive.com/discuss@jquery.com/msg02537.html
 * author: Klaus Hartl
 */

$.clientCoords = function() {
	
	var dimensions = {width: 0, height: 0};
	
	if (document.documentElement) {
		dimensions.width = document.documentElement.offsetWidth;
		dimensions.height = document.documentElement.offsetHeight;
	} 
	else if (window.innerWidth && window.innerHeight) {
		dimensions.width = window.innerWidth;
		dimensions.height = window.innerHeight;
	}
	return dimensions;
	
}

$.fn.draggable = function() {
	if (!$(this).exists()) {
		return false;
	}
	
	$(this).bind("drag", function(event) {
		$(this).css({   
			top:(event.offsetY-document.body.scrollTop),   
			left:(event.offsetX-document.body.scrollLeft)   
		})
	});
	
	return true;
	
}

$.fn.exists = function() { return ($(this).length > 0) ? true : false; }

$.fn.fixPNGs = function() {
	if (!$(this).exists()) {
		return false;
	}
	
	$(this).supersleight({shim: "/images/blank.gif"});
	
	return true;
	
}

function formatPhoneNumber(phoneNumber) {
	
	return phoneNumberStripCountryCode(phoneNumberStripNonNumericals(phoneNumber));
	
}

function keyCode(event){
	return event.keyCode ? event.keyCode : event.which;
}

function getPageVisitId() {
	
	return pageVisit.id;
	
}

$.fn.getSelector = function() {

	var tagName = $(this).tagName().toLowerCase();
	var selector = "#" + $(this).attr("id");
	if (typeof selector == undefined || selector == null || selector == "#") {
		var classes = $(this).attr("class");
		var split = classes.split(" ");
		selector = "." + split.join(".");
	}
	return tagName + selector;
	
}

$.fn.getTotalHeight = function(arguments) {
	arguments = $.extend({
		border: true,
		margin: true,
		padding: true
	}, arguments);
	
	if (!$(this).exists()) {
		return 0;
	}

	var totalHeight = ($(this).height()>0)?$(this).height():$(this).css("height").replace(/px/g,"");

	var borderHeight = parseInt($(this).css("borderTopWidth")) + parseInt($(this).css("borderBottomWidth"));
	var marginHeight = parseInt($(this).css("margin-top")) + parseInt($(this).css("margin-bottom"));
	var paddingHeight = parseInt($(this).css("padding-top")) + parseInt($(this).css("padding-bottom"));

	if (arguments.border && borderHeight > 0) {
		totalHeight += borderHeight;
	}
	if (arguments.margin && marginHeight > 0) {
		totalHeight += marginHeight;
	}
	if (arguments.padding && paddingHeight > 0) {
		totalHeight += paddingHeight;
	}
	
	return totalHeight;
	
}

$.fn.getTotalWidth = function(arguments) {
	arguments = $.extend({
		border: true,
		margin: true,
		padding: true
	}, arguments);
	
	if (!$(this).exists()) {
		return 0;
	}

	var totalWidth = ($(this).width()>0)?$(this).width():$(this).css("width").replace(/px/g,"");

	var borderWidth = parseInt($(this).css("borderLeftWidth")) + parseInt($(this).css("borderRightWidth"));
	var marginWidth = parseInt($(this).css("margin-left")) + parseInt($(this).css("margin-right"));
	var paddingWidth = parseInt($(this).css("padding-left")) + parseInt($(this).css("padding-right"));

	if (arguments.border && borderWidth > 0) {
		totalWidth += borderWidth;
	}
	if (arguments.margin && marginWidth > 0) {
		totalWidth += marginWidth;
	}
	if (arguments.padding && paddingWidth > 0) {
		totalWidth += paddingWidth;
	}
	
	return totalWidth;
	
}

/**
 *    src: http://gregwolejko.com/ie-detection-in-javascript/
 * author: Greg Wolejko
 *
 * "Method that I proposed is based only on the version of JavaScript implemented in particular browser 
 * and that won’t change."
*/

function isIE() { return /*@cc_on!@*/false; }
function isIE6(){ return false /*@cc_on || @_jscript_version < 5.7 @*/; }
function isIE7(){ return false /*@cc_on || @_jscript_version >= 5.7 @*/; }

/**
 *    src: http://roshanbh.com.np/2008/09/get-random-number-range-two-numbers-javascript.html
 * author: Roshan Bhattarai
 * 
 * function to get random number up to m
 */

function randomXToY(minVal,maxVal,floatVal) {
	
  var randVal = minVal+(Math.random()*(maxVal-minVal));
  
  return typeof floatVal=='undefined'?Math.round(randVal):randVal.toFixed(floatVal);
  
}

$.fn.registerResizeToFitPage = function(arguments) {
	arguments = $.extend({
		stretch: ""
	}, arguments);
	
	if (!$(this).exists()) {
		return false;
	}

	var element = $(this);
	var resizeArgs = arguments;
	
	if (element.exists()) {
		if (windowHasLoaded()) {
			element.resizeToFitPage(resizeArgs);
		}
		else {
			$(window).load(function() {
				element.resizeToFitPage(resizeArgs);
			})
		}
		$(window).bind("resize", function(){
			element.resizeToFitPage(resizeArgs);
		});
	}
	
	return true;
	
}

windowLoaded = false;
function registerWindowHasLoaded() {
	windowLoaded = true;
}

$.fn.resizeToFitPage = function(arguments) {
	arguments = $.extend({
		stretch: "",
        containerSelector: '#Container'
	}, arguments);

	if (!$(this).exists()) {
		return false;
	}
	
	var stretch = arguments.stretch.substr(0,1); 
    var containerSelector = arguments.containerSelector;
    
	if (stretch == "h" || stretch == "b") {
		
		var ch = $(containerSelector).getTotalHeight();
		var wh = $(window).height();
		
		$(this).height(ch);
		if (ch < wh) {
			$(this).height(wh);
		}
		
	}
	
	if (stretch == "w" || stretch == "b") {
		
		var cw = $(containerSelector).getTotalWidth();
		var ww = $(window).width();
		
		$(this).width(ww);
		if (cw > ww) {
			$(this).width(cw);
		}
		
	}
	
	return true;
	
}

/**
 *    src: http://stackoverflow.com/questions/411688/how-to-extend-jquery-to-make-it-easier-to-retrieve-the-tagname
 * author: Dan Herbert
 */

$.fn.tagName = function() {
    return this.get(0).tagName.toLowerCase();
}

function windowHasLoaded() {
	return windowLoaded;
}
