/*
 * jQuery ifixpng plugin
 * (previously known as pngfix)
 * Version 3.1.2  (2008/09/01)
 * @requires jQuery v1.2.6 or above, or a lower version with the dimensions plugin
 * 
 * Based on the plugin by Kush M., http://jquery.khurshid.com
 *
 * Background position Fixed
 * Also fixes non-visible images
 * (c) Copyright Yereth Jansen (yereth@yereth.nl)
 * personal website: http://www.yereth.nl
 * Company website: http://www.wharf.nl
 * 
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 * For a demonstration of the background-position being fixed:
 * http://www.yereth.nl/bgpos.html
 *
 * Plugin page:
 * http://plugins.jquery.com/project/iFixPng2
 *
 */

/**
 *
 * @example
 *
 * optional if location of pixel.gif if different to default which is images/pixel.gif
 * $.ifixpng('media/pixel.gif');
 *
 * $('img[@src$=.png], #panel').ifixpng();
 *
 * @apply hack to all png images and #panel which icluded png img in its css
 *
 * @name ifixpng
 * @type jQuery
 * @cat Plugins/Image
 * @return jQuery
 * @author jQuery Community
 */
;(function($) {

	/**
	 * helper variables and function
	 */
	$.ifixpng = function(customPixel) {
		$.ifixpng.pixel = customPixel;
	};
	
	$.ifixpng.regexp = {
		bg: /^url\(["']?(.*\.png([?].*)?)["']?\)$/i,
		img: /.*\.png([?].*)?$/i
	},
	
	$.ifixpng.getPixel = function() {
		return $.ifixpng.pixel || 'images/pixel.gif';
	};
	
	var hack = {
		base	: $('base').attr('href'),
		ltie7	: $.browser.msie && $.browser.version < 7,
		filter	: function(src) {
			return "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true,sizingMethod=crop,src='"+src+"')";
		}
	};
	
	/**
	 * Applies ie png hack to selected dom elements
	 *
	 * $('img[@src$=.png]').ifixpng();
	 * @desc apply hack to all images with png extensions
	 *
	 * $('#panel, img[@src$=.png]').ifixpng();
	 * @desc apply hack to element #panel and all images with png extensions
	 *
	 * @name ifixpng
	 */
	 
	$.fn.ifixpng = hack.ltie7 ? function() {
		function fixImage(image, source, width, height, hidden) {
			image.css({filter:hack.filter(source), width: width, height: height})
			  .attr({src:$.ifixpng.getPixel()})
			  .positionFix();
		}
		
    	return this.each(function() {
			var $$ = $(this);
			if ($$.is('img') || $$.is('input')) { // hack image tags present in dom
				var source, img;
				if (this.src && this.src.match($.ifixpng.regexp.img)) { // make sure it is png image
					// use source tag value if set 
					source = (hack.base && this.src.substring(0,1)!='/' && this.src.indexOf(hack.base) === -1) ? hack.base + this.src : this.src;
					// If the width is not set, we have a problem; the image is not probably visible or not loaded
					// and we need a work around.
					if (!this.width || !this.height) {
						$(new Image()).one('load', function() {
							fixImage($$, source, this.width, this.height);
							$(this).remove();
						}).attr('src', source);
					// If the image already has dimensions (it's loaded and visible) we can fix it straight away.
					} else fixImage($$, source, this.width, this.height);
				}
			} else if (this.style) { // hack png css properties present inside css
				var imageSrc = $$.css('backgroundImage');
				// Background repeated images we cannot fix unfortunately
				if (imageSrc && imageSrc.match($.ifixpng.regexp.bg) && this.currentStyle.backgroundRepeat == 'no-repeat') {
					imageSrc = RegExp.$1;
					var x = this.currentStyle.backgroundPositionX || 0, y = this.currentStyle.backgroundPositionY || 0;
					if (x || y) {
						var css = {}, img;
						if (typeof x != 'undefined') {
							if (x == 'left') css.left = 0; 
							// if right is 0, we have to check if the parent has an odd width, because of an IE bug
							else if (x == 'right') css.right = $$.width() % 2 === 1 ? -1 : 0;
							else css.left = x;
						}
						if (typeof y != 'undefined') {
							// if bottom is 0, we have to check if the parent has an odd height, because of an IE bug
							if (y == 'bottom') css.bottom = $$.height() % 2 === 1 ? -1 : 0; 
							else if (y == 'top') css.top = 0;
							else css.top = y;
						}
						img = new Image();
						$(img).one('load', function() {
							var x,y, expr = {}, prop;
							// Now the image is loaded for sure, we can see if the background position needs fixing with an expression (in case of percentages)
							if (/center|%/.test(css.top)) {
								expr.top = "(this.parentNode.offsetHeight - this.offsetHeight) * " + (css.top == 'center' ? 0.5 : (parseInt(css.top) / 100));
								delete css.top;
							}
							if (/center|%/.test(css.left)) {
								expr.left = "(this.parentNode.offsetWidth - this.offsetWidth) * " + (css.left == 'center' ? 0.5 : (parseInt(css.left) / 100));
								delete css.left;
							}
							// Let's add the helper DIV which will simulate the background image
							$$.positionFix().css({backgroundImage: 'none'}).prepend(
								$('<div></div>').css(css).css({
									width: this.width,
									height: this.height,
									position: 'absolute',
									filter: hack.filter(imageSrc)
								})
							);
							if (expr.top || expr.left) {
								var elem = $$.children(':first')[0];
								for (prop in expr) elem.style.setExpression(prop, expr[prop], 'JavaScript');
							}
							$(this).remove();
						});
						img.src = imageSrc;
					} else {
						$$.css({backgroundImage: 'none', filter:hack.filter(imageSrc)});
					}
				}
			}
		});
	} : function() { return this; };
	
	/**
	 * positions selected item relatively
	 */
	$.fn.positionFix = function() {
		return this.each(function() {
			var $$ = $(this);
			if ($$.css('position') != 'absolute') $$.css({position:'relative'});
		});
	};

})(jQuery);


/* jquery.tbd-slideshow 0.5 :: developed by jsummers.at.techbydesign.com */
//todo: add jquery png fix
(function($) {
	$.fn.slideshow = function(options) {
		// build main options before element iteration
		var opts = $.extend({}, $.fn.slideshow.defaults, options);
		// iterate and reformat each matched element
		return this.each(function() {
			$this = $(this);
			var list = $this.find("ul:first");
			var active = null;
			// build element specific options
			var o = $.meta ? $.extend({}, opts, $this.data()) : opts;
			
			//if set height
			if(opts.slideH != null){
		  		$this.css({"height": opts.slideH+"px"});
	  		}
	  		//if set width
	  		if(opts.slideW != null){
	  			var totalW = (opts.slideW+opts.margin) * opts.show;
	  			if(opts.show > 1){
	  				totalW = totalW - opts.margin;
	  			}
		  		$this.css({"width": totalW+"px"});
	  		}
	  		//if we use js to strip styles
	  		if(opts.listStyles == false){
	  			$this.find("ul, li").css({"margin":"0px","padding":"0px","list-style-type":"none"});
	  		}
	  		$this.find("li").height(opts.slideH);
	  		//position element, children, 
		  	$this.css({"position": "relative","overflow": "hidden", "display":"block"}).children("ul").css({"top":"0px","position": "absolute", "left":"0px"}).children("li").each(function(obj,call){
		    		if(obj == 0){
						active = $(this).addClass(opts.activeSlide);
					}
					//position each children element
					if(opts.xslide == false){
						var place = (opts.slideH+opts.margin) * obj;
						jQuery(this).css({"top":place+"px", "position":"absolute", "left": "0px"}).show();
					}else{
						var leftPlace = (opts.slideW+opts.margin) * obj;
						jQuery(this).css({"top":"0px", "position":"absolute", "left": leftPlace+"px"}).show();
					}
					if(opts.slideW){
						jQuery(this).css({"width": opts.slideW+"px"});
					}
				});
			//add next and back buttons
			var positions = new Array();
			var shown = 0;
			var goback = $("."+opts.backBttn);
			var gonext = $("."+opts.nextBttn);
			var groups = Math.ceil(list.children().length/opts.show);
			var slideW = (opts.slideW+opts.margin);
			var listWidth = (slideW)*list.children().length;
			function slideItAll(num){
				shown = num;
				list.animate({"left": positions[num]+"px"},opts.animSpeed);
				$(opts.controls+" a").eq(shown).addClass("active").siblings(".active").removeClass("active");
			}
			//opts.controls
			if(opts.controls != null){
				for(i=0;i<groups;i++){
					var position = ((slideW*opts.show)*i)*-1;
					var placeingGuess = (opts.show+(opts.show*i));
					if(placeingGuess>list.children().length){
						position = position+(slideW*((placeingGuess-list.children().length)));
					}
					positions.push(position);
					$(opts.controls).append("<a href=\"#\"><span>&nbsp;"+i+"&nbsp;</span></a>");
				}
			}
			$(opts.controls+" a").eq(0).addClass("active");
			//add events to the buttons
			//todo: change all this so that we are not depending on the opts width / height
			//todo: maybe allow for items to vary in width
			$(opts.controls).children("a").click(function(){
				slideItAll($(opts.controls+" a").index($(this)));
				return false;
			});
			$(goback).click(function(){
				//goBack();
				if(shown>0){
					slideItAll(shown-1);
				}
				return false;
			}).css("cursor","pointer");
			$(gonext).click(function(){
				if(shown<positions.length-1){
					slideItAll(shown+1);
				}
				return false;
			}).css("cursor","pointer");
		});
		
	};
	$.fn.slideshow.defaults = {
		nextBttn: "goRight"
		,backBttn: "goLeft"
		,activeSlide: "curr"
		,timeout: 8000
		,autoScroll: false
		,animSpeed: "normal"// A string representing one of the three predefined speeds ("slow", "normal", or "fast") or the number of milliseconds to run the animation (e.g. 1000)
		,slideW: 736//the width
		,slideH: 280//the height
		,margin: 24//extra space between the slides
		,xslide: true // if false scroll vertical 
		,listStyles: false//if false takes off margnin padding and list-style-type from ul and li
		,controls: ".slideshowSectionControls"
		,show: 3
		,loop: false
		,slideNum: 3
	};
	//
	// end of closure
	//
})(jQuery);
function getURLParam(strParamName,caps){
  //alert('getURLParam');
  var strReturn = "";
  var strHref = window.location.href;
  if ( strHref.indexOf("?") > -1 ){
    if(caps!=false){
    	var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
	}else{
		var strQueryString = strHref.substr(strHref.indexOf("?"));
	}
    var aQueryString = strQueryString.split("&");
    for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
      if (
		aQueryString[iParam].indexOf(strParamName + "=") > -1 ){
        var aParam = aQueryString[iParam].split("=");
        strReturn = aParam[1];
        break;
      }
    }
   //alert(strReturn);
  }
  
  return strReturn;
}
//for production this file and the main js file should be one
$(document).ready(function(){
	// Set the transparent pixel for png fix
	//jQuery.ifixpng('blank.gif');
	//apply png fix
	//jQuery('img').ifixpng();
	//start the slideshow on the homepage
	$(".slideshow").slideshow({autoScroll: true, show: 1, slideW: 900, slideH: 480});
	
	////elem that we will load pages into
	//our work page
	function slideLoad(domElem,loadElem,slideBox,selectloadclass,slideSettings){
		var clicked = domElem;
		if($(slideBox).data("slider") != true){
			$(slideBox).slideshow(slideSettings).data("slider",true);
			$(".controlBox").fadeIn();			
			$("body").removeClass("main");
		}
		if($(domElem).data("curr") != true){
			$(domElem).data("curr",true).addClass("curr").siblings().data("curr",false).removeClass("curr");
			//make a div to put the conent we are going to load in
			$(loadElem).prepend("<div class=\"loader\"><!-- --></div>");
			//get the div we made and load the link we click on but we only would like the .clientInfo section
			$(loadElem).find(".loader").load($(domElem).find("a").attr("href")+(" ."+selectloadclass),function (responseText, textStatus, XMLHttpRequest) {
				if(textStatus != "success"){
					var errMssg = "<div class=\""+selectloadclass+"\">";
					var errMssg = errMssg + "	<h2>Whoops, we had a hard time loading this page!</h2>";
					var errMssg = errMssg + "	<p>You try going to <a href=\""+$(clicked).find("a").attr("href")+"\">"+$(clicked).find("a").attr("href")+"</a>, hope you have better luck then we did! The error message is below...</p><br /><br />";
					var errMssg = errMssg + "	<p>"+responseText+"</p>";
					var errMssg = errMssg + "</div>";
					$(loadElem).find(".loader").html(errMssg);
				}				
				//animate the placeholders height to equal the height of new content
				$(loadElem).animate({height: $($(this).find("."+selectloadclass)[0]).height()+"px"});
				//fade in our content
				$(loadElem+" > div:first").hide().fadeIn();
				//hide and remove any content that is not what we loaded in
				$(loadElem+ " > div:not(:first)").fadeOut(function(){
					$(this).remove();
				});
				$(".loader").removeClass("loader");
			});
		}
	}
	
	//click a item on the pages with sliding loading content
	$("#workThatWeDo li").click(function(){
		slideLoad(this,"#showWork","#workWeDoSlide","clientInfo",{autoScroll: false, show: 5, slideW: 170, slideH: 230, margin: 10});
		return false;
	});
	//fake click from other pages
	if($("body").is("#ourWork") && getURLParam('href') != ''){
		//throw slideLoad with the domElem that has the link passed in the url. This allows the site to work without javascript and is a easy way to make it fancy with javasript
		slideLoad($($("a[href="+getURLParam('href',false)+"]").parents("li")[0]),"#showWork","#workWeDoSlide","clientInfo",{autoScroll: false, show: 5, slideW: 170, slideH: 230, margin: 10});
	}
	//handle whatwedo links if users have javascript 
	$("a.workwedo").click(function(){
		//using javascript to link to the main page then adding href url param used to fake a click on the section they are interested in
		window.location = "ourWork.php?href="+$(this).attr("href");
		return false;
	});
	/*$("#thingsThatWeDo li").click(function(){
		slideLoad(this,"#showWork","#thingsThatWeDoSlide","wedo",{autoScroll: false, show: 3, slideW: 280, slideH: 330, margin: 30});
		return false;
	})
	*/;
	//fake click from other pages
	if($("body").is("#whatcanwedo") && getURLParam('href') != ''){
		//throw slideLoad with the domElem that has the link passed in the url. This allows the site to work without javascript and is a easy way to make it fancy with javasript
		slideLoad($($("a[href="+getURLParam('href',false)+"]").parents("li")[0]),"#showWork","#thingsThatWeDoSlide","wedo",{autoScroll: false, show: 3, slideW: 280, slideH: 330, margin: 30});
	}
	//handle whatwedo links if users have javascript 
	$("a.whatwedo").click(function(){
		//using javascript to link to the main page then adding href url param used to fake a click on the section they are interested in
		window.location = "whatcanwedo.php?href="+$(this).attr("href");
		return false;
	});
	
	
	
	
});