/*
 *  error trapping code
 *  
 */
	/*
		onerror=handleErr
		
		function handleErr(msg,url,l)
		{
		//Handle the error here
		return true or false
		}
	*/
	
		
	/*
	 * Console logger
	 * 
	 * Message variables
	 *  %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 = arguments[0];
				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);
		}
	};
	
	
// load jQuery when DOM is ready
$(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);
			}
		});
		
	/*
	 *  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
			})
		;

	/*
	 *  PNG fixer for IE6
	 *  requires jquery.pngfix.js to be loaded before this script
, use inline IMG width and height	 */
		// $("img[@src$=png]").pngFix();

	/*
	 *  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>")
				/* for big buttons: 
				.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
			;
		});

	/*
	 * Remove Simplenews Template header, footer, heading
	 */
	// remove Simplenews header and footer
	//$(".simplenews-template-header, .simplenews-template-footer").remove();
	//$(".simplenews-template-body h1").remove();

	/* 
	 * Thickbox image viewer
	 * docs: http://jquery.com/demo/thickbox/
	 * (this script should run before thickbox.js is loaded)
	 * create links where: href="large-image-url.jpg" class="thickbox" rel="group-id"
	 */
		tb_pathToImage = "/sites/all/themes/template/images/loading_thickbox.gif";
		var log_thickbox = false;
		// select each group of images
			// category overview pictures
				//$("a[img.image.image-thumbnail]").addClass("category");
			// node inline
				$(".node .content").each(function(i) {
				  var gid = "node-g";
				  var group = gid + i;
				  if (log_thickbox){$.log("tagging group %s: %o",gid,$(this));}
				  var items = $(this).find(".inline a img.image");
				  TB_drupal_rewrite(items, group);
				});
			// gallery images
				$("ul.images a img.image").each(function(i) {
				  var gid = "gallery-g";
				  var group = gid + i;
				  if (log_thickbox){$.log("tagging group %s: %o",gid,$(this));}
				  var items = $(this).find("a img.image");
				  TB_drupal_rewrite(items, group);
				});
			// view items
				/*
				  $(".view .item-list a img.image").each(function(i) {
				  var gid = "view-g";
				  var group = gid + i;
				  if (log_thickbox){$.log("tagging group %s: %o",gid,$(this));}
				  var items = $(this).find("a img.image");
				  TB_drupal_rewrite(items, group);
				});
				*/				
		// rewrite each item
		function TB_drupal_rewrite(context, group) {
		  // Process only images, that have not been rewritten (.thickbox) and that are not categories.
		  //$(context).not(".thickbox").not(".category").each(function(i) {
		  $(context).each(function(i) {
		    var img = $(this);
			var link = $(this).parent();
			var title = $(this).attr("title") || img.attr("title") || img.attr("alt") || null;
			/*
			 * ATTENTION: Until the derivate Bug (http://drupal.org/node/125610) is fixed,
	 		* the script should allways use the original picture ("true || ").
	 		*/
			// change image filename
			thickbox_derivative = "original";
			if (thickbox_derivative == "original") {
				var href = img.attr("src")
					.replace(".thumbnail", "")
					.replace(".preview", "")
				;
			}
			else {
				var href = img.attr("src")
					.replace(".thumbnail", "." + thickbox_derivative)
					.replace(".preview", "." + thickbox_derivative)
				;
			}
			// Finally rewrite the link and wait for thickbox.js to apply the effects.
			link.attr({
				href: href,
				title: title,
				rel: group
			});
			link.addClass("thickbox");
			if (log_thickbox) {
				$.log(" image %o %s %s %s", img, href, title, group);
			}
		  });	
	}

});
