p10(jQuery);

function imgloaded(img) {
	jQuery(img).addClass('loaded');
}

function grayscale(image, bPlaceImage)
{
  var myCanvas=document.createElement("canvas");
  var myCanvasContext=myCanvas.getContext("2d");

  var imgWidth=image.width;
  var imgHeight=image.height;
  // You'll get some string error if you fail to specify the dimensions
  myCanvas.width= imgWidth;
  myCanvas.height=imgHeight;
  //  alert(imgWidth);
  myCanvasContext.drawImage(image,0,0);

  // This function cannot be called if the image is not rom the same domain.
  // You'll get security error if you do.
  var imageData=myCanvasContext.getImageData(0,0, imgWidth, imgHeight);

  // This loop gets every pixels on the image and
    for (j=0; j<imageData.height; i++)
    {
      for (i=0; i<imageData.width; j++)
      {
         var index=(i*4)*imageData.width+(j*4);
         var red=imageData.data[index];
         var green=imageData.data[index+1];
         var blue=imageData.data[index+2];
         var alpha=imageData.data[index+3];
         var average=(red+green+blue)/3;
   	    imageData.data[index]=average;
         imageData.data[index+1]=average;
         imageData.data[index+2]=average;
         imageData.data[index+3]=alpha;
       }
     }

    if (bPlaceImage)
	{
	  var myDiv=document.createElement("div");
	     myDiv.appendChild(myCanvas);
	  image.parentNode.appendChild(myCanvas);
	}
	return myCanvas.toDataURL();
  }
  
function p10 ($) {
	$(document).ready(init);
	
	function init() {
	
		var neckHeight = $('.neck').height();
		
		
		$('.lightbox, .thickbox').fancybox({
			'overlayColor': '#FFFFFF',
			'overlayOpacity': .85,
			'centerOnScroll': true,
			'titlePosition'	: 'inside'
		});
		
		
		// hide header
		function toggleUI() {
			if ($('#head').hasClass('isminimized')) {
				maximizeUI();
			} else {
				minimizeUI(300);
			}
		}
		
		function minimizeUI(speed) {
			$('#head').addClass('isminimized').stop().animate({'top':'-80px', 'opacity': .9},speed);
			$('#projecthead').stop().animate({'top':30},speed);
			$('#foot').stop().animate({'height':'0px'},speed, function() {
				$('.scaleBG.Fit').each(function() {
					var bg = $(this).find('img.bgIMG');
					if (bg.hasClass('loaded')) {
						resizeBG(bg, $(this))
					} else {
						var bgOf = $(this);
						bg.load(function() {
							resizeBG(bg, bgOf);
						});
					}
				});
			});
			
		}
		
		function maximizeUI() {
			$('#head').removeClass('isminimized').stop().animate({'top':'0px', 'opacity': 1},300);
			$('#projecthead').stop().animate({'top':'110px'},300);
			$('#foot').stop().animate({'height':'100px'},300, function() {
				$('.scaleBG.Fit').each(function() {
					var bg = $(this).find('img.bgIMG');
					if (bg.hasClass('loaded')) {
						resizeBG(bg, $(this))
					} else {
						var bgOf = $(this);
						bg.load(function() {
							resizeBG(bg, bgOf);
						});
					}
				});
			});
		}
		
		$('body').keydown(function(e){
			if (e.keyCode==72) {
				toggleUI();
			}
		});
		
		
		
		$('.projectframe').each(function() {
			$(this).load(function() {
				minimizeUI(600);
			});
		});
		
		
		// contact page functions
		$('#contactForm').each(function() {
			//$(this).addClass('js');
			$('input[type=text], textarea').each(function() {
				var def = $(this).prev('label').hide().text();
				$(this).addClass('default').val(def);
				$(this).focus(function() {
					if($(this).hasClass('default')){
						$(this).val('');
						$(this).removeClass('default');
					}
				});
				$(this).blur(function() {
					if ($(this).val()==''){
						$(this).addClass('default');
						$(this).val(def);
					} else
					if ($(this).val()==def) {
						$(this).addClass('default');
					}
				});
			});
			
			//$('#robot input').checkBox();
			//$('#subjects input').checkBox();
			
			$(this).submit(function() {
				$('#submitButton').val('sending...');
				$('input[type=text], textarea').each(function(N) {
					if ($(this).hasClass('default')) {
						$(this).val('');
					}
				});
			});
		});
		
		
		
		// blog functions
		
		// blog scroll meta info
		/*//if ($.browser.webkit) {

			$('.blog').each(function() {
				$(window).scroll(function() {
					var st = $(window).scrollTop()+neckHeight;
					
					$('.post').each(function() {
					
						var pt = $(this).position().top;
						var meta = $(this).find('.meta');
						if (st>pt && st<pt+$(this).height()){
							//$(this).addClass('toppost');
							var pad = st-pt;
							pad = Math.round(pad);
							if (pad>$(this).height() - $(meta).height() - 30) {
								pad = $(this).height() - $(meta).height() - 30;
							}
							meta.css({'padding-top': pad + 'px'});
						} else {
						
							//$(this).removeClass('toppost');
							$(meta).css({'padding-top': '0px'});
							//alert('st='+st+'...pt='+pt+'   pad='+pad);
							
						}
					});
				});
			});
		//}*/
		
		// blog thumbnail hovers
		$('.body.thumb').each(function() {
			$('.post').each(function() {
				if ($(this).find('img').length!=0){
					$(this).find('.title').animate({'opacity':0},0);
					$(this).hover(
					function() {
						$(this).find('.title').stop().animate({'opacity':1},300);
					},
					function() {
						$(this).find('.title').stop().animate({'opacity':0},300);
					});
				}
			})
		})
		
		// header tabs
		$('#head .tab').each(function() {
			if ($(this).hasClass('current')) {
				var tabID = $(this).attr('rel');
				$('#head .tabwindow#'+tabID).show();
			}
			$(this).click(function() {
				if ($(this).hasClass('current')) {
					var tabID = $(this).attr('rel');
					$('#head .tabwindow#'+tabID).hide();
					$(this).removeClass('current');
				} else {
					var c = $('#head .tab.current');
					if ($(c).length!=0) {
						$('#head .tabwindow#'+$(c).attr('rel')).hide();
						$(c).removeClass('current');
					}
					var tabID = $(this).attr('rel');
					$('#head .tabwindow#'+tabID).show();
					$(this).addClass('current');
				}
				return false;
			});
		});
		
		// big links
		$('.fuckinga').each(function() {
			$(this).click(function() {
				window.location.href = $(this).find('a:first').attr('href');
			});
		});
		$('.inactive').click(function() {
			return false;
		})
		
		
		
		// work page functions
		$('#work').each(function() {
			$(this).find('.project').each(function() {
				$(this).hover(
					function() {
						$(this).stop().addClass('shadow');
					},
					function() {
						$(this).stop().removeClass('shadow');
					}
				);
			});
			$('#workhead #cats ul a').each(function() {
				$(this).click(function() {
					if (!$(this).hasClass('current')) {
						$('#workhead #cats a.current').removeClass('current');
						$(this).addClass('current');
						var cat = $(this).attr('rel');
						$('#work .project').each(function() {
							if ($(this).hasClass(cat)) {
								$(this).removeClass('dim');
							} else {
								$(this).addClass('dim');
							}
						});
					}
					return false;
				});
			});
		});
		
		
		
		
		
		// slide set
		$('.slides').each(function() {
			var currentIndex = 0;
			var container = $(this);
			var slides = $(this).find('.slide');
			if (!$(this).hasClass('fullwindow')) {
				slides.each(function() {
					$(this).height(container.height());
				});
			}
			slides.first().addClass('current');
			
			$('#slidenav a.next, a#nextpage').click(function() {
				switchToPost(currentIndex+1);
				return false;
			});
			$('#slidenav a.prev, a#prevpage').addClass('inactive').click(function() {
				switchToPost(currentIndex-1);
				return false;
			});
			
			if (slides.length<=1) {
				$('#slidenav a.next, a#nextpage').addClass('inactive');
			}
			
			$('a#nextpage, a#prevpage').hover(
				function() {
					$(this).animate({'opacity':1},200);
				},
				function() {
					$(this).animate({'opacity':0},200);
				}
			);
			
			if ($(this).hasClass('onhome')) {
				switchToPost(Math.floor(Math.random()*slides.length));
			}
		
			function switchToPost(i) {
				if (i<0) {
					currentIndex=0;
				} else
				if (i>=$(slides).length) {
					currentIndex = slides.length-1;
				} else {
					currentIndex = i;
				}
				$('.slidenum').text(currentIndex+1);
				$('.slide.current').removeClass('current');
				$('.slide:eq('+currentIndex+')').addClass('current');
				
				if (currentIndex==0) {
					$('#slidenav a.prev, a#prevpage').addClass('inactive');
				} else {
					$('#slidenav a.prev, a#prevpage').removeClass('inactive');
				}
				if (currentIndex==slides.length-1){
					$('#slidenav a.next, a#nextpage').addClass('inactive');
				} else {
					$('#slidenav a.next, a#nextpage').removeClass('inactive');
				}
			}
			
			
			// project page keyboard navigation
			$(document).keydown(function(e){
				if (e.keyCode==37) {
					// left
					switchToPost(currentIndex-1);
				} else
				if (e.keyCode==39) {
					// right
					switchToPost(currentIndex+1);
				}
			});
		});
		
		$('#project, .project').each(function() {
			// project page keyboard navigation
			$(document).keydown(function(e){
				if (e.keyCode==38) {
					//up
					var linkurl = $('#projecthead #projectsnav a.prev').attr('href');
					if (linkurl!='#' && linkurl!='' && linkurl!=undefined) {
						window.location.href = linkurl;
						return false;
					}
				} else
				if (e.keyCode==40) {
					//down
					var linkurl = $('#projecthead #projectsnav a.next').attr('href');
					if (linkurl!='#' && linkurl!='' && linkurl!=undefined) {
						window.location.href = linkurl;
						return false;
					}
				}
			});
		});
		
		
		// scaling background images
		var scaleBGs = $('.scaleBG');
		if (scaleBGs.length>0) {
			scaleBGs.each(function() {
				var bg = $(this).find('img.bgIMG');
				if (bg.hasClass('loaded')) {
					resizeBG(bg, $(this))
				} else {
					var bgOf = $(this);
					bg.load(function() {
						resizeBG(bg, bgOf);
					});
				}
			});
			
			$(window).resize(function() {
				scaleBGs.each(function() {
					var bg = $(this).find('img.bgIMG');
					if (bg.hasClass('loaded')) {
						resizeBG(bg, $(this))
					} else {
						var bgOf = $(this);
						bg.load(function() {
							resizeBG(bg, bgOf);
						});
					}
				});
			});
		}
		
	}
	
	function resizeBG(bg, bgOf) {
		if (!bg.attr('rel') || bg.attr('rel') == 'NaN') {
			bg.attr('rel', bg.width()/bg.height())
		}
		var bgRatio = bg.attr('rel');
		if (bgOf.hasClass('Fit')){
			var H = bgOf.height()-$('#foot').height()-40;
			if (bgRatio > bgOf.width()/H) {
				bg.width(bgOf.width());
				bg.height(Math.ceil(bgOf.width()/bgRatio));
			} else {
				bg.height(H);
				bg.width(Math.ceil(H*bgRatio));
			}
		} else {
			if (bgRatio < bgOf.width()/bgOf.height()) {
				bg.width(bgOf.width());
				bg.height(Math.ceil(bgOf.width()/bgRatio));
				bg.css('left', 0);
			} else {
				bg.width(Math.ceil(bgOf.height()*bgRatio));
				bg.height(bgOf.height());
				if (bg.width()>0){
					bg.css('left', -.5 * (bg.width()-bgOf.width()));
				}
			}
		}
	}
}
