siteRoot_str = "http://www.juiceinteractive.com/";

addComment={moveForm:function(d,f,i,c){var m=this,a,h=m.I(d),b=m.I(i),l=m.I("cancel-comment-reply-link"),j=m.I("comment_parent"),k=m.I("comment_post_ID");if(!h||!b||!l||!j){return}m.respondId=i;c=c||false;if(!m.I("wp-temp-form-div")){a=document.createElement("div");a.id="wp-temp-form-div";a.style.display="none";b.parentNode.insertBefore(a,b)}h.parentNode.insertBefore(b,h.nextSibling);if(k&&c){k.value=c}j.value=f;l.style.display="";l.onclick=function(){var n=addComment,e=n.I("wp-temp-form-div"),o=n.I(n.respondId);if(!e||!o){return}n.I("comment_parent").value="0";e.parentNode.insertBefore(o,e);e.parentNode.removeChild(e);this.style.display="none";this.onclick=null;return false};try{m.I("comment").focus()}catch(g){}return false},I:function(a){return document.getElementById(a)}};

function checkEmail(val_str) {
	email_exp = /^[a-zA-Z0-9._+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
	if(email_exp.test(val_str)==true) {
		return true;
	} else {
		return false;
	}
}

function validForm() {
	// Set flag
	var flag_bln = false;
	var alert_str = "";
	
	// Clear Errors (done here because Email can error two ways
	$("label").removeClass("error");
	
	// Check all .req fields
	$(".req").each(function() {
		if( ($("input",this).length>0 && $("input",this).attr("value") == $("input",this).attr("defaultValue")) || ( $("select",this).length>0 && $("select",this).attr("value") == "" ) || ( $("textarea",this).length>0 && $("textarea",this).attr("value") == "") ) {
			$(this).children("label").addClass("error");
			if(flag_bln==false) {
				alert_str = "Please complete all required fields."
			};
			flag_bln = true;
		}
	});
	// Check Email field
	if(!checkEmail($("#email").attr("value"))) {
		$("#email-grp label").addClass("error");
		if(flag_bln==false) {
			alert_str = "Please enter a valid Email address."
		};
		flag_bln = true;
	}
	if(flag_bln) {
		// NOOoo..
		return alert_str;
	} else {
		return true;
	}
}

// DOM Load:
$(function() {

	$("body").addClass("jsEnabled");

	// Body id will override class when identifying a page
	pageID_str = $('body').attr('id');
	if(!pageID_str) {
		pageID_str = $('body').attr('class');
	};
/*
	$("#primary-nav a").css({
		color: "#999999 !important"
	});

	$("#primary-nav a").hover(function() {
		$(this).animate({color:"#e17000"});
	},function(){
		$(this).animate({color:"#999"});
	});
*/
	// Page specific JS:
	switch(pageID_str) {

		case "portfolio" :

			$("body").append("<div id='modal'>This is the modal window</div>");
			$("#modal").css("display","none");

			$(".item").each(function() {
				parentDL = $("dl",this);

				typeDL = jQuery("<dl class='type'></dl>")
				typeDL.prepend($("dd.type",this));
				typeDL.prepend($("dt.type",this));
				parentDL.prepend(typeDL);

				clientDL = jQuery("<dl class='client'></dl>")
				clientDL.prepend($("dd.client",this));
				clientDL.prepend($("dt.client",this));
				parentDL.prepend(clientDL);

				grabsDL = jQuery("<dl class='grabs'></dl>")
				grabsDL.prepend($("dd.grabs",this));
				grabsDL.prepend($("dt.grabs",this));
				grabsDL.insertBefore(clientDL);
				
				//alert($("dd.grabs", this).html());
				if($("dd.grabs", this).length>0) {
					jQuery("<dd class='preview'><a class='modal-pop'>View Preview</a></dd>").insertBefore($("dd.preview", this));
					$("dd.grabs > ul").wrap("<div class='modalwrap''></div>");
					$(".modalwrap", this).prepend("<span class='image' style='display:block;width:612px;height:300px'></span>");
					$(".modal-pop",this).click(function() {
						$("#modal").html($(this).parent().parent().find(".modalwrap").clone(true)).modal({
//						$("#modal").html($(this).parent().parent().find(".modalwrap").clone(true)).prepend("<img src="+($(this).parent().parent().find("ul li:first-child a").attr("href"))+" />").modal({
//						$("#modal").html($(this).parent().parent().find("ul").clone(true)).modal({
							closeClass: "modal-close"
							,onOpen: function (dialog) {
							  dialog.overlay.fadeIn('slow', function () {
							    /*dialog.container.fadeIn('slow', function () {
							      dialog.data.fadeIn('slow');
							    });
							    */
							    dialog.container.fadeIn('normal')
							    dialog.data.fadeIn('slow');
							  });
							 }
							 
							 ,onClose: function (dialog) {
							  	dialog.overlay.fadeOut('slow');
							  	dialog.container.fadeOut('slow', function () {
						        $.modal.close(); // must call this!
						      });
							 }
							 
						});
						$(".simplemodal-wrap .image").html("<img src="+($(this).parent().parent().find("ul li:first-child a").attr("href"))+" />");
						$(".modal-close").text("Close");
						$(".modalwrap ul a").click(function() {
							$(".simplemodal-wrap .image img").attr("src",$(this).attr("href"));
							$(".modalwrap ul a").removeClass("selected");
							$(this).addClass("selected");
							return false;
						});
						$(".modalwrap ul a").each(function() {
							$(this).attr("title",$(this).text());
							});
						$(".modalwrap ul li:first-child a").click();
					});
//					$(".modalpop").attr("rel","prettyPhoto[galery]").attr("title","View the image gallery");
//					$(".grabs a").prettyPhoto();
				}
				grabsDL.css("display","none");
			});

			/* ============================== Scroller: ============================== */

			$("#primary-content > ul").wrap("<div class='scroll'></div>");
			$("#primary-content").prepend("<ul id='scroll-nav'></ul>");

			var $panels = $('#primary-content .scroll > ul > li');
			var $container = $('#primary-content .scroll > ul');

			$panels.each(function() {
				$("#scroll-nav").append("<li><a href=\"#"+$(this).attr("id")+"\" title=\""+$("h2 a",this).text()+"\">"+$("h2 a",this).text()+"</a></li>");
			});

			// if false, we'll float all the panels left and fix the width 
			// of the container
			var horizontal = true;

			// float the panels left if we're going horizontal
			if (horizontal) {
			  $panels.css({
			    'float' : 'left',
			    'position' : 'relative' // IE fix to ensure overflow is hidden
			  });

			  // calculate a new width for the container (so it holds all panels)
			  $container.css('width', $panels[0].offsetWidth * $panels.length);
			}

			// collect the scroll object, at the same time apply the hidden overflow
			// to remove the default scrollbars that will appear
			var $scroll = $('#primary-content .scroll').css('overflow', 'hidden');

			// apply our left + right buttons
			$scroll
			  .before('<img class="scrollButtons left" src="'+siteRoot_str+'images/scroll_left.png" />')
			  .after('<img class="scrollButtons right" src="'+siteRoot_str+'images/scroll_right.png" />');

			// handle nav selection
			function selectNav() {
			  $(this)
			    .parents('ul:first')
			      .find('a')
			        .removeClass('selected')
			      .end()
			    .end()
			    .addClass('selected');
			}

			$('#scroll-nav').find('a').click(selectNav);

			// go find the navigation link that has this target and select the nav
			function trigger(data) {
			  var el = $('#scroll-nav').find('a[href$="' + data.id + '"]').get(0);
			  selectNav.call(el);
			}

			if (window.location.hash) {
			  trigger({ id : window.location.hash.substr(1) });
			} else {
			  $('ul#scroll-nav a:first').click();
			}

			// offset is used to move to *exactly* the right place, since I'm using
			// padding on my example, I need to subtract the amount of padding to
			// the offset.  Try removing this to get a good idea of the effect
			var offset = parseInt((horizontal ? 
			  $container.css('paddingTop') : 
			  $container.css('paddingLeft')) 
			  || 0) * -1;

			var scrollOptions = {
			  target: $scroll, // the element that has the overflow

			  // can be a selector which will be relative to the target
			  items: $panels,

			  navigation: '#scroll-nav a',

			  // selectors are NOT relative to document, i.e. make sure they're unique
			  prev: 'img.left', 
			  next: 'img.right',

			  // allow the scroll effect to run both directions
			  axis: 'xy',

			  onAfter: trigger, // our final callback

			  offset: offset,

			  // duration of the sliding effect
			  duration: 750,

			  // easing - can be used with the easing plugin: 
			  // http://gsgd.co.uk/sandbox/jquery/easing/
			  easing: 'swing'
			};

			// apply serialScroll to the slider - we chose this plugin because it 
			// supports// the indexed next and previous scroll along with hooking 
			// in to our navigation.
			$('#primary-content').serialScroll(scrollOptions);

			// now apply localScroll to hook any other arbitrary links to trigger 
			// the effect
			$.localScroll(scrollOptions);

			// finally, if the URL has a hash, move the slider in to position, 
			// setting the duration to 1 because I don't want it to scroll in the
			// very first page load.  We don't always need this, but it ensures
			// the positioning is absolutely spot on when the pages loads.
			scrollOptions.duration = 1;
			$.localScroll.hash(scrollOptions);

			/* ============================ End Scroller: ============================ */

			break;

		case "contact" :
			$("#contact-form #submit").click(function() {

				var alert_txt = validForm()
				// Validate that thing:
				if(alert_txt===true) {

					// Add container element for returned data:
					jQuery("<div id='returned' style='margin:0;padding:0;'></div>").appendTo("body").hide();
					$("#returned").css("height",$('#contact-form').innerHeight());
		
					// Serialze form data to pass to .post():
					data_str=$("#contact-form").serialize();
					$.post(
						 // Post to:
						 siteRoot_str+"scripts/contact/"
						 // Data:
						,data_str
						 // Callback:
						,function(data) {
							// alert(data);
							if(data=="SUCCESS") {
								$('#contact-form').replaceWith($("#returned"));
								$('#returned').html("<p>Thank you for contacting us! We&#8217;ll get back to you ASAP!</p>")  
								.hide()  
								.fadeIn(1500);
							} else {
								$('#contact-form').html("<div id='message'></div>");  
								$('#message').html("<h2>FAIL!</h2>")  
								.append("<p>We will be in touch soon.</p>")  
								.hide()  
								.fadeIn(1500);
							}
						}
					)

				} else {

					if ($('#contact-form p.alert').length>0) {
						$('#contact-form p.alert').fadeOut(500, function() {
							$('#contact-form p.alert').remove();
							jQuery("<p class='alert'>"+alert_txt+"</p>").prependTo($('#contact-form')).hide().fadeIn(500);
						});
					} else {
						jQuery("<p class='alert'>"+alert_txt+"</p>").prependTo($('#contact-form')).hide().slideDown(500);
					}

				}
				return false;

			});
		break;

	}

	// Add target='_blank' to all .external links
	$("a.pop").click(function(){
		$(this).attr("target","_blank");
	});

	try {
	var pageTracker = _gat._getTracker("UA-2338361-1");
	pageTracker._trackPageview();
	} catch(err) {}

});

/*
 * jQuery Color Animations
 * Copyright 2007 John Resig
 * Released under the MIT and GPL licenses.
 */

(function(jQuery){

	// We override the animation for all of these color styles
	jQuery.each(['backgroundColor', 'borderBottomColor', 'borderLeftColor', 'borderRightColor', 'borderTopColor', 'color', 'outlineColor'], function(i,attr){
		jQuery.fx.step[attr] = function(fx){
			if ( fx.state == 0 ) {
				fx.start = getColor( fx.elem, attr );
				fx.end = getRGB( fx.end );
			}

			fx.elem.style[attr] = "rgb(" + [
				Math.max(Math.min( parseInt((fx.pos * (fx.end[0] - fx.start[0])) + fx.start[0]), 255), 0),
				Math.max(Math.min( parseInt((fx.pos * (fx.end[1] - fx.start[1])) + fx.start[1]), 255), 0),
				Math.max(Math.min( parseInt((fx.pos * (fx.end[2] - fx.start[2])) + fx.start[2]), 255), 0)
			].join(",") + ")";
		}
	});

	// Color Conversion functions from highlightFade
	// By Blair Mitchelmore
	// http://jquery.offput.ca/highlightFade/

	// Parse strings looking for color tuples [255,255,255]
	function getRGB(color) {
		var result;

		// Check if we're already dealing with an array of colors
		if ( color && color.constructor == Array && color.length == 3 )
			return color;

		// Look for rgb(num,num,num)
		if (result = /rgb\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*\)/.exec(color))
			return [parseInt(result[1]), parseInt(result[2]), parseInt(result[3])];

		// Look for rgb(num%,num%,num%)
		if (result = /rgb\(\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*\)/.exec(color))
			return [parseFloat(result[1])*2.55, parseFloat(result[2])*2.55, parseFloat(result[3])*2.55];

		// Look for #a0b1c2
		if (result = /#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})/.exec(color))
			return [parseInt(result[1],16), parseInt(result[2],16), parseInt(result[3],16)];

		// Look for #fff
		if (result = /#([a-fA-F0-9])([a-fA-F0-9])([a-fA-F0-9])/.exec(color))
			return [parseInt(result[1]+result[1],16), parseInt(result[2]+result[2],16), parseInt(result[3]+result[3],16)];

		// Otherwise, we're most likely dealing with a named color
		return colors[jQuery.trim(color).toLowerCase()];
	}
	
	function getColor(elem, attr) {
		var color;

		do {
			color = jQuery.curCSS(elem, attr);

			// Keep going until we find an element that has color, or we hit the body
			if ( color != '' && color != 'transparent' || jQuery.nodeName(elem, "body") )
				break; 

			attr = "backgroundColor";
		} while ( elem = elem.parentNode );

		return getRGB(color);
	};
	
	// Some named colors to work with
	// From Interface by Stefan Petre
	// http://interface.eyecon.ro/

	var colors = {
		aqua:[0,255,255],
		azure:[240,255,255],
		beige:[245,245,220],
		black:[0,0,0],
		blue:[0,0,255],
		brown:[165,42,42],
		cyan:[0,255,255],
		darkblue:[0,0,139],
		darkcyan:[0,139,139],
		darkgrey:[169,169,169],
		darkgreen:[0,100,0],
		darkkhaki:[189,183,107],
		darkmagenta:[139,0,139],
		darkolivegreen:[85,107,47],
		darkorange:[255,140,0],
		darkorchid:[153,50,204],
		darkred:[139,0,0],
		darksalmon:[233,150,122],
		darkviolet:[148,0,211],
		fuchsia:[255,0,255],
		gold:[255,215,0],
		green:[0,128,0],
		indigo:[75,0,130],
		khaki:[240,230,140],
		lightblue:[173,216,230],
		lightcyan:[224,255,255],
		lightgreen:[144,238,144],
		lightgrey:[211,211,211],
		lightpink:[255,182,193],
		lightyellow:[255,255,224],
		lime:[0,255,0],
		magenta:[255,0,255],
		maroon:[128,0,0],
		navy:[0,0,128],
		olive:[128,128,0],
		orange:[255,165,0],
		pink:[255,192,203],
		purple:[128,0,128],
		violet:[128,0,128],
		red:[255,0,0],
		silver:[192,192,192],
		white:[255,255,255],
		yellow:[255,255,0]
	};
	
})(jQuery);