// JavaScript Document
var jp = {
		/* Welcome section */
		/* Bind img hover event */
		imgSelector : {
			id : "#Section_Welcome #CMYK_Lines #ImgSelector a",
			active : "",
			bind : function(){
				/* Find current active image */
				$(this.id).children('img').each(function(index){
          var r = /_on.jpg/;
          var s = $(this).attr('src');
          if(r.test(s)){
						jp.imgSelector.active = index;
					}
			  }).end().hover(function(){
					/* Check to see if we need to hover*/
					if($(this).index()!=jp.imgSelector.active){
						var s = $(this).children('img').attr('src');
						
						/* Set image src to jquery data once */
						if(!$(this).data("onImg")){
							$(this).data("onImg",s.replace("off.jpg","on.jpg"));
							$(this).data("offImg",s);
						}
						
						/* Set wrapping 'a' element background to 'on' image */
						$(this).css('background-image','url('+ $(this).data("onImg") +')');
						
						/* Fade out child img element */
						$(this).children('img').stop().animate({
							opacity : 0																
						},400);
						$(this).data("hoverFalse",false);
					} else {
						$(this).data("hoverFalse",true);
					}
				}, function(){
						if($(this).data("hoverFalse")!=true){
							/* Fade in child img element */
							if(($(this).data('imgClicked'))&&($(this).data('imgClicked')[0]==true)){
								var src = $(jp.imgSelector.id).eq($(this).data('imgClicked')[1]).children('img').attr('src');
								src = src.replace("off.jpg","on.jpg");
								$(jp.imgSelector.id).eq($(this).data('imgClicked')[1]).children('img').attr('src',src);
								$(jp.imgSelector.id).eq($(this).data('imgClicked')[1]).children('img').removeAttr("style");
								$(jp.imgSelector.id).eq($(this).data('imgClicked')[1]).removeAttr("style");
								$(this).data('imgClicked')[0]=false;
								return;
							}
							$(this).children('img').stop().animate({
								opacity: 1
							},400,function(){
								$(this).removeAttr('style');
								$(this).parent().removeAttr('style');
							});
						}
						$(this).removeData('hoverFalse');		
				});
				$(this.id).bind("click",function(e){
					/* Check to see if this is currently active - return if true */
					if(jp.imgSelector.active == $(this).index()) return false; /* Prevent default behavior */
					
					/* If user clicks "img" (anchor tag) capture event and change background */
					var className = $(this).attr('href');
					className = className.split("=");
					$("#Section_Welcome").data("class",className[1]);
					
					/* Create a global temp image */
					window['tempImg'] = new Image();
					/* Load new image to source */
					window['tempImg'].src = "includes/images/welcome_"+className[1]+"_bg.jpg";
					/* Call onload event to fire when image loads */
					window['tempImg'].onload = jp.imgSelector.changeImg();

					/* Let's update our thumbnails now */
					var src = $(jp.imgSelector.id).eq(jp.imgSelector.active).children('img').attr("src");
					src = src.replace("on.jpg","off.jpg");
					$(jp.imgSelector.id).eq(jp.imgSelector.active).css('background-image','url('+ src + ')');
					
					
					var active = $(this).index();
					
					/* Animate our fade out */
					$(jp.imgSelector.id).eq(jp.imgSelector.active).children('img').animate({
						opacity: 0
					}, 200, function(){
					/* Update SRC of thumbnail image */
						$(jp.imgSelector.id).eq(jp.imgSelector.active).children('img').attr('src',src);
						$(jp.imgSelector.id).eq(jp.imgSelector.active).children('img').removeAttr("style");
						$(jp.imgSelector.id).eq(jp.imgSelector.active).removeAttr("style");
					/* Update active img */
						jp.imgSelector.active = active;
					})
					
					/* Prevent current hover from fading out */
					$(this).data('imgClicked',[true,active]);
					
					/* Remove focus */
					$(this).blur();
	
					/* Prevent default */
					return false;
				})
			},
			changeImg : function(){
				if(window['tempImg']){
					$("#Section_Welcome").attr("class",$("#Section_Welcome").data("class"));
				}
			}
		},
		imgPortfolio : {
			bind : function(){
				$("#Section_Portfolio dl dd img").bind("click",function(){
					var src = $(this).attr('src');
					var title = $(this).attr('alt');
					src = src.replace("tb","full");
					$.fn.colorbox({href:src,opacity:.7,title:title})
				})
				$("#Section_Portfolio dl dt img").bind("click",function(){
					$("#Section_Portfolio .Summary").css("display","none");
					$(this).parent().parent().children('dd').each(function(){
						$(this).css('display','none');
					})
					var h = $(this).parent().outerHeight();
					var i = $(this).parent().index();
					var mt = (-(((h+10)*i)/2))+(-65);
					$(this).parent().next().css({
            'position':'absolute',
            'display':'block',
            'margin-top':mt
            });
				})
			}
		}
	}
	
/* jQuery ready event */
$(document).ready(function(){
		/* Initialize img hover event */
		jp.imgSelector.bind();
		jp.imgPortfolio.bind();
});