/*
 * put jQuery into noconflict mode
 * if conflicts with other Javascript libaries, e.g. Prototype on GoDaddy PreviewDNS
 */
	//jQuery.noConflict();
		
/*
 * Console logger
 *  %s	String
 *  %d, %i	Integer (numeric formatting is not yet supported)
 *  %f	Floating point number (numeric formatting is not yet supported)
 *  %o	Object hyperlink
 * 
 *  only logs if globalVars.log = true
 */

	$.log = function() {
	  	if(window.console) {
			if($.browser.safari){
				// Safari console
				var args = "";
				$.each(arguments,function(i,val){
					args += " " + val;
				});
				window.console.log(args); // fix to show args, Safari doesn't like .apply
			}
			if ($.browser.mozilla) {
				// Firefox with firebug
				window.console.log.apply(this, arguments);
			}
	  	} else {
	  		// no Firebug
	    	//alert(message);
		}
	};
	
	
	
		
	/* 
	 *  fix IE6 link background image flicker bug
	 */
	try {  document.execCommand("BackgroundImageCache", false, true); } catch(err) {} 
		
		
	
/* 
 * jQuery actions when DOM is loaded
 */ 

$(document).ready(function(){
	
	/*
	 *  De-obfuscate email addresses
	 * 
	 *  replaces email address @ in "mailto" links and link text
	 * 
	 */
		function deobfuscate(x){
			return x.replace("--@--","@");
		}
		$("a").each(function (i){
			var h = this.href;
			if (h.indexOf("mailto")==0) {
				var t = deobfuscate($(this).html());
				this.href = deobfuscate(h);
				$(this).html(t);
			}
		});
		
	/* add util links divider */
	$(".util li").eq(0).after("<li class='divider'></li>");
	
	/* expandable sections */
	$("div.expandable").hide();
	$("a.expand").click(function(){
		$(this).parent().hide().next().slideDown();
		return false;
	})
	
	/* animated nav slider */
		if(navCurrentId == undefined){ navCurrentId = "";}
		function navGoto(id){
			var items = {
				home: -872,
				services: -718,
				portfolio: -565,
				about: -408,
				contact: -251
			};
			// get position
			var left = false;
			$.each(items,function(k,v){
				if(v != undefined){
					if (k == id) {
						left = v;
					}
				}
			});
			// remove hover class from all others
			$(".nav li").removeClass("hover");
			// set position
			if (left) {
				$(".nav ul")
					.stop()
					.animate({"backgroundPosition": left + "px 0px"},500,"easeOutQuad",function(){
						// add hover class to selected
						$(".nav #"+id).addClass("hover");
					});
			}
		}
		// hover/click event
		$(".nav a")
			.hover(
				function(){
					var id = $(this).parent().attr("id");
					if (id != navCurrentId) {
						navGoto(id);
					}	
				},
				function(){
					var id = $(this).parent().attr("id");
					if (id != navCurrentId) {
						navGoto(navCurrentId);
					}
				})
			.click(function(){
				navCurrentId = $(this).parent().attr("id");
			});
		// set starting position based on current nav
		if(
			navCurrentId == "home" ||
			navCurrentId == "services"
		){
			left = 75;
		} else {
			left = -1050;
		}
		$(".nav ul").css("background-position",left+"px 0px");
		// goto current nav when page loads
		navGoto(navCurrentId);
		
	/* fancybox popups */
	$(".fancybox").fancybox({
		'hideOnContentClick': true,
		'overlayOpacity'		:	0.5,
		'zoomOpacity'			: true,
		'overlayShow'			: true,
		'zoomSpeedIn'			: 500,
		'zoomSpeedOut'			: 500
	});
	
	/* Google Analytics tracker 
	 *  waits until DOM is loaded to fire tracker event
	 */
		//var pageTracker = _gat._getTracker("UA-5475261-1");
		//pageTracker._trackPageview();

	/* Google Analytics external links
	 *  tags with class="external" target="_blank"
	 *  optional Google Analytics tracking code trigger
	 */
		// (add https and don't include urls that start with site domain
		var pre = "/LINK/";				// prefix to add to URL for GA tracking code
		$('a[href^="http://"]')
			.addClass('external')			// add CSS class for styling
			.attr('target', '_blank')		// open in popup window
			.click(function(){				// GA tracking code
				var href = $(this).attr("href");
				href = href.substr(7,9999);	// trim off http://
				var i = href.indexOf("www.");	// trim off www.
				if (i===0){
					href = href.substr(4,9999);
				}
				href = encodeURI(href);		// encode URL
				$.log(pre + href);			// log href to console
//pageTracker._trackPageview(pre + href)	// GA tracker code
				//return false;				// cancel link action
			})
		;
		
	/* fix clearing on portfolio items
	 */
	
	$(".portfolio-items .portfolio-group ul").append("<li class='clear'/>");



	/* 
	 *   prevent right click on images
	 */
		/*
		$("img").bind("contextmenu",function(e){
			$.log("right-clicked on image");
			//return false;
		});
		*/
		
	/* contact form */
	/*
	$("#form-contact #form-submit").click(function(){
		$.log("contact form submitted");
		var ok = true;
		$("#form-contact .required").each(function(){
			var val = $(this).val();
			if (val == "") {
				ok = false;
			}
		});
		if (ok) {
			if(confirm("Are you sure you want to send your comment?")){
				return true;
			}else {
				return false;
			}
		}else {
			alert("Please complete the required information");
			return false;
		}
	});
	*/
		
	/*
	 *  preload images
	 */
	/*
	var preloads = [];
	$.each(preloads,function(i,img){
		var x = new Image;
		x.src = img;
	});
	*/	
	
	/*
	 *  PNG fixer for IE6
	 *  requires jquery_pngfix.js to be loaded before this script
		, use inline IMG width and height	 */
		// $("img[@src$=png]").pngFix();

	/* alernateive PNG fixer
	 * no included script required
	 */
	/*
	function fix_pngs() {
		 if (navigator.appVersion.match(/MSIE [0-6]\./)) {
			 $('.png').each(function () {
		 		if (this.currentStyle.backgroundImage != 'none') {
		 			var image = this.currentStyle.backgroundImage;
		 			image = this.currentStyle.backgroundImage.substring(5, image.length - 2);
			 		if(image.indexOf(".png") !=-1) {
				 		$(this).css({
			 			'backgroundImage': 'none',
		 				'filter': "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=crop, src='" + image + "')"
		 				});
		 			}
			 	}
		 	});
		 }
		}
	*/
	
	/*
	 *  rounded Nifty corners
	 *  docs: http://www.html.it/articoli/niftycube/index.html
	 */
		// $("#page_wrap").nifty("normal");
		
	/*
	 *  Innerfade slideshows
	 *  docs: http://medienfreunde.com/deutsch/weblog/aus_der_praxis.html?nid=162
	 *  in CSS set the following to prevent images from showing while page loads:
	 * 	 #container img { display: none; }
	 */ 
		/*
		$(".slideshow").innerfade({
		    animationtype: 'fade',
		    speed: 1500,
		    timeout: 6000,
		    type: 'sequence',
		    containerheight: '200px'
	    });
	    */

	/* BIG BUTTONS 
	 * wraps <input type="submit"> and adds functionality
	 */

	/*
	$("input[type='submit']").each(function(index){
		$(this).wrap("<div class='buttonBig'></div>");
		var label = $(this).attr("value");
		$(this).before("<span class='spacer'>" + label + "</span>");
		// setup hover
		$(this).hover(
			function(){
				// add hover in
				$(this).addClass("active");
			},
			function (){
				// add hover out
				$(this).removeClass("active");
			}
		);
	});
	*/	

	/*
	 *  Disable form submit buttons on click
	 * 
	 *  adds class="form-submit-disabled" 
	 *  unbinds click event
	 *  appends a status message
	 * 
	 */
	 
	/*
	 var sel = "#content_main input.form-submit";
		$(sel).click(function(){
			$(this)
				.parent().append("<span id='form-submit-loading'>Please wait...</span>")
			;
			$(sel)
				.addClass("form-submit-disabled")
				.fadeTo('fast',.25)	// make button look disabled
				.unbind("click")	// prevent clicking again
				.click(function(){return false;})	// disable click event
			;
		});
	*/	 
});
