/* absolute path to the "loading" image */
var loadingImageAbsolutePath = "http://www.smiley-tech.com/images/ajax-loader.gif";

/* get mouse position */
var mouseX = 0;
var mouseY = 0;
$(document).mousemove(function(e) { 
	mouseX = e.pageX;
	mouseY = e.pageY;
});
/* END :: get mouse position */

/* are we dealing with IE 6 or less? */
var isIElte6 = ($.browser.msie && parseFloat($.browser.version.substr(0, 3)) <= 6.0);

/* slideshow - global variables */
var itemWidth = "";
var itemCount = 0;
var slidingNow = false;

/* document ready function */
$(document).ready(function(){
	
	/* initialize all listeners on elements that could be added via AJAX as well */
	initializeListeners();
	
	/* enable "loading" functionality on a subset of HTML elements */
	$(".loadingOnClick").click(function(event) {
		var alreadyLoading = $(this).attr("alreadyLoading");
		if (!alreadyLoading) {
			$(this).attr("alreadyLoading", "true");
			var newElementId = $(this).attr("id") + $(this).attr("name") + $(this).attr("rel") + "LoadingElement";
			$(this).val("Please, wait ...").after("<span id='" + newElementId + "' style='display:none; margin-left:10px;'><img src='" + loadingImageAbsolutePath + "' align='absmiddle' /></span>");
			$("span#" + newElementId).fadeIn("fast");
		}
	});
	
	/* fix the flash elements for IE browsers */
	flashObjects = document.getElementsByTagName("object");
	for (var i = 0; i < flashObjects.length; i++) {
		flashObjects[i].outerHTML = flashObjects[i].outerHTML;
	}
	
	/* blink the error message if it is present */
	$("div.errorDiv").fadeIn().fadeOut().fadeIn();
	
	/* blink the success message if it is present */
	$("div.successDiv").fadeIn().fadeOut().fadeIn();
	
	/* slideshow */
	if ($("div#slideshowStrip").length != 0) {
		itemWidth = parseInt($("div#slideshowStrip > div.slideshowItem").eq(0).css("width").replace("px", ""));
		itemCount = $("div#slideshowStrip > div.slideshowItem").length;
		if (itemCount < 3) {
			for ($i = 0; $i < (3 - itemCount); $i++) {
				$("div#slideshowStrip > div.slideshowItem").eq(0).clone().appendTo($("div#slideshowStrip"));		
			}
			itemCount = 3;	
		}
		stripWidth = itemWidth * itemCount;
		$("div#slideshowStrip").css("width", stripWidth + "px").css("left", "-" + itemWidth + "px");
	}
	
	if (isIElte6) {
		$("img.serviceLogo").each(function() {
			$(this).attr("src", $(this).attr("src").replace(".png", ".gif"));								   
		});
	}
	
	var intervalPointer = setInterval("nextSlide(\"right\");", 5000);
	$("a.arrow").click(function(event) {
		event.preventDefault();
		clearInterval(intervalPointer);
		var direction = new String("");
		if ($(this).attr("id") == "leftArrow") direction = "left"; else direction = "right";
		nextSlide(direction);
	});

});

/* display the next slide on our slideshow */
function nextSlide(direction) {
	if (slidingNow == false) {
		slidingNow = true;
		if (direction == "right") {
			$("div#slideshowStrip > div.slideshowItem").eq(0).animate({ width: "0px" }, 400, function() {
				$("div#slideshowStrip > div.slideshowItem").eq(0).appendTo($("div#slideshowStrip"));
				$("div#slideshowStrip > div.slideshowItem").eq(itemCount - 1).css("width", itemWidth + "px");
				slidingNow = false;
			});
		} else {
			$("div#slideshowStrip > div.slideshowItem").eq(itemCount - 1).css("width", "0px");
			$("div#slideshowStrip > div.slideshowItem").eq(itemCount - 1).prependTo($("div#slideshowStrip"));
			$("div#slideshowStrip > div.slideshowItem").eq(0).animate({ width: itemWidth + "px" }, 400, function() {
				slidingNow = false;
			});
		}
	}
}

/* initialize all listeners - this function can be called even after insertion of HTML code via AJAX */
function initializeListeners() {
	
	/* show and hide hidden divs */
	$("a.toggleHiddenDiv").unbind("click").click(function(event) {
		event.preventDefault();
		var field = $(this).attr("rel");		// get ID of the appropriate DIV
		if (field == "") {
			if ($("div.hiddenDiv").css("display") == "none") {
				$("div.hiddenDiv").slideDown("slow");
			} else {
				$("div.hiddenDiv").slideUp("slow");
			}
		} else {
			if ($("div.hiddenDiv[rel='" + field + "']").css("display") == "none") {
				$("div.hiddenDiv[rel='" + field + "']").slideDown("slow");
			} else {
				$("div.hiddenDiv[rel='" + field + "']").slideUp("slow");
			}
		}
	});

}

function substr_count(haystack, needle, offset, length) {
    var pos = 0;
	var cnt = 0;
    haystack += "";
    needle += "";
    if (isNaN(offset)) offset = 0;
    if (isNaN(length)) length = 0;
    offset--;
 
    while ((offset = haystack.indexOf(needle, offset + 1)) != -1) {
        if (length > 0 && (offset + needle.length) > length) {
            return false;
        } else {
            cnt++;
        }
    }
 
    return cnt;
}

function getWindowSize() {
	var dimensions = new Array(2);
	var myWidth = 0, myHeight = 0;
	if (typeof(window.innerWidth) == "number") {
		/* non-IE */
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight )) {
		/* IE 6+ in "standards compliant mode" */
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if (document.body && (document.body.clientWidth || document.body.clientHeight )) {
		/* IE 4 compatible */
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
	dimensions[0] = myWidth;
	dimensions[1] = myHeight;
	return dimensions;
}
