/* Author: Jan Ludwig



*/

jQuery(document).ready(function() {
  
  // Init Objects
  window.$global = {
	GreyBox : jQuery('.greyBox'),
	Fades   : jQuery('.fadein'),
	LForm   : jQuery("#user-login-form"),
	Logout  : jQuery("#logout"),
	Videos  : jQuery(".video-js-box"),
	WDetail	: jQuery("#WorkDetail"),
	WVThumbs: jQuery("#WorkVideoThumbs")
  };								
  
  jQuery('.workItem .MOver').bind('mouseover mouseout', function(event){
    if(event.type == 'mouseover'){
	  jQuery('.workItemHover',jQuery( this ).parent()).fadeIn(200);
	}else{
	  jQuery('.workItemHover',jQuery( this ).parent()).fadeOut(200);
	}
  });
  
  /*jQuery('.workVideoThumb a').bind('mouseover mouseout', function(event){
    if(event.type == 'mouseover'){
	  jQuery('.workvideoThumbHeadline',jQuery( this )).fadeIn(200);
	}else{
	  jQuery('.workvideoThumbHeadline',jQuery( this )).fadeOut(200);
	}
  });*/
  
  /*jQuery( ".workItem a" ).hover(
    function() {console.log('test')},
	function() {jQuery('div.ItemText',jQuery( this )).animate({backgroundColor: "#00CC99",color: "#FFF"}, 100 );},
    function() {jQuery('.ItemText',jQuery( this )).animate({backgroundColor: "#ECECEC",color: "#444444"}, 100 );}
  );*/
  
  if($global.WDetail.length > 0 && $global.WVThumbs.length > 0)
  	if($global.WDetail.height()+26 > $global.WVThumbs.height())
	  jQuery('#CCSpacer').height($global.WDetail.height()+40 - $global.WVThumbs.height());
  	//$global.WVThumbs.height($global.WDetail.height());
});


// Bind Resize Function

jQuery(window).load(function(){
  jQuery(window).bind("resize", function() {resizeWindow();});
    posWItems();
	if(document.location.hash != ''){
		//alert('set A');
		window.location.href = window.location.href;
	}
	$global.Fades.each(function(index, item) {
    fadeInObj(index, item);
  });
  resizeWindow();
});

// Resize Function

function resizeWindow() {
  if($global.GreyBox.length > 0){
    var Pos = $global.GreyBox.position();
    var Top = Pos.top;
    var WHeight = jQuery(document).height()+15+12;
    var THeight = WHeight - Top;
	if(THeight > $global.GreyBox.height())
		$global.GreyBox.height(THeight);
  }
}
// FadeIn Function

function fadeInObj(index, item){
  if(document.location.hash == '')
  	jQuery(item).delay(index*100).removeClass('fadein').hide().fadeIn(100);
  else
    jQuery(item).removeClass('fadein').hide().fadeIn(100);
}

// LogIn Functions

function showLogIn(){
	$global.LForm.removeClass('hidden');
	//jQuery('#NameInput').focus();
	$global.Logout.hide();
}

function onFocusLogInForm(){
  $Name = jQuery('#NameInput');
  $Pass = jQuery('#PassInput');
  if($Name.val() == 'NAME' && $Pass.val() == 'PASSWORD' ){
    $Name.val('');
    $Pass.val('');
  }
}

// position WorkItems

function posWItems(){
  var WIL = jQuery('#WItemList');
  var GB = jQuery('.greyBox');
  var IColCount = (GB.length > 0)?5:6;
  var DivHeight = GB.height();
  if(WIL){
	var ColHeights = new Array(0,0,0,0,0,0);
	Gaps = new Array();
	WIL.children('.workItem').each(function(index, item){
	  var MaxYGap = -100;
	  var el = jQuery(item);
	  var CAdd = getElementColWidth(el);
	  
	  //console.log('Before '+Gaps.length);
	  var Gap = findGap(Gaps,el,CAdd);
	  //console.log('After '+Gaps.length);
	  if(Gap){
		//console.log('Gap (Col:'+Gap.col+'Cols:'+Gap.cols+' Pos:'+Gap.pos+') used for:'+jQuery('a',el).attr('name'));
	    var SelCol = Gap.col;
		var MaxYGap = Gap.pos;
		Gap.pos += el.height()+12;
		Gap.height = Gap.height-el.height()-12;
	  }else{
	    var SelCol = findBestCol(IColCount, ColHeights, CAdd, index);
	    var MaxY = findMaxY(ColHeights,SelCol,CAdd);
	  }
	  var MaxY = findMaxY(ColHeights,SelCol,CAdd);
	  
	  if(MaxYGap > -100)
	    el.css({ position: "absolute",top: (-12+MaxYGap)+'px', left: SelCol*177+'px' });
	  else{
		  el.css({ position: "absolute",top: (-12+MaxY)+'px', left: SelCol*177+'px' });
	  var NHeight = -12+MaxY+el.height();
	  if(NHeight > DivHeight)DivHeight = NHeight;
	  
	  addNewMaxHeights(ColHeights, MaxY, el.height()+12, SelCol, CAdd, Gaps);
	  }
	});
	if(IColCount == 5)
	 GB.height(DivHeight);
  }
}

function findGap(Gaps,Element,Cols){
  for(var i=0; i<Gaps.length; i++){
    var Gap = Gaps[i];
    if(Gap.height >= Element.height()+12 && Cols <= Gap.cols){
	  Gap.cols = Cols;
	  //console.log('Splice'+(i+1),' '+(Cols-1));
	  Gaps.splice(i+1,Cols-1);
	  return Gap;
	}
  }
  return false;
}

function GapObj(c,cs,h,p) {
  this.col = c;
  this.cols = cs;
  this.height = h;
  this.pos = p;
}

function addNewMaxHeights(Heights, Height, AddHeight, index, count, GapArr){
	for (i=0;i<count;i++){
		if(Heights[index+i] < Height){
			var diff = Height-Heights[index+i];
			var GCols = 1
			while(Height-Heights[index+i+GCols] == diff && i+GCols < count){
				GCols++;
			}
			//console.log(GCols+' Col(s) Gap at Col #'+(index+i)+'(Height '+diff+' px) '+Heights[index+i+GCols-1]);
			GapArr.push(new GapObj(index+i, GCols, diff, Heights[index+i+GCols-1])); 
			Heights[index+i] = Height+AddHeight;
			//i+=GCols;
		}else
		Heights[index+i] = Height+AddHeight;
	  }	
}

function getElementColWidth(Element){
	Cols = 1;
	  if(Element.children('div.WI2').length > 0)Cols = 2;
	  else if(Element.children('div.WI3').length > 0)Cols = 3;
	return Cols;
}

function findMaxY(heights, index, count){
	var maxY = 0;
	for (var i=0;i<count;i++){
		if(maxY<heights[index+i])maxY = heights[index+i];
	 }
	 return maxY;
}

function findBestCol(TotalCols, Heights, ItemWidth, ItemIndex){
	var SelCol = 0;
	var minHeight = findMaxY(Heights, 0, ItemWidth);
	for (var i=1;i<=TotalCols-ItemWidth;i++){
		newHeight = findMaxY(Heights, i, ItemWidth);
		//console.log(ItemIndex % (TotalCols-i));
		if(newHeight < minHeight || (newHeight == minHeight && ItemIndex % (TotalCols-i) > 1)){
			minHeight = newHeight;
			SelCol = i;
		}
	}
	return SelCol;
}

/*function posWItems(){
  var WIL = jQuery('#WItemList');
  var IColCount = (jQuery('.greyBox').length > 0)?5:6;
  if(WIL){
	var ColHeights = new Array(0,0,0,0,0);
	var Col = 0;
	var CAdd = 1;
	var MaxY = 0;
	WIL.children('.workItem').each(function(index, item){
	  var el = jQuery(item);
	  CAdd = 1;
	  if(el.children('div.WI2').length > 0)CAdd = 2;
	  else if(el.children('div.WI3').length > 0)CAdd = 3;
	  MaxY = 0;
	  console.log(ColHeights);
	  console.log(Col+CAdd);
	  if(Col+CAdd > IColCount){Col = 0;}
	  for (i=0;i<CAdd;i++){
		if(MaxY<ColHeights[Col+i])MaxY = ColHeights[Col+i];
	  }
	  el.css({ position: "absolute",top: (-12+MaxY)+'px', left: Col*177+'px' });
	  for (i=0;i<CAdd;i++){
		ColHeights[Col+i] = MaxY+el.height()+12;
	  }
	  Col = (Col+CAdd)%IColCount;
	});
  }
}*/

// show Video

/*function showVideo(VID){
	$VisVideo.pause();
	jQuery('.video-js-box').each(function(index, item){
		jQuery(item).hide();								
	});
	jQuery('#videoBox-'+VID).removeClass('hidden').show();
	$VisVideo = VideoJS.setup('vid-'+VID);
	$VisVideo.currentTime(0);
	$VisVideo.play();
}*/

function showMOImg(src){
	jQuery('#MOImg').attr("src", src);
}
function nextMOImg(){
	MOImg = jQuery('#MOImg');
	IID = parseInt(MOImg.attr("IID"))+1;
	//console.log(IID);
	
	TImg = jQuery('#'+IID);
	if(TImg.length == 0){
		IID = 1;
		TImg = jQuery('#'+IID);
	}
	SRC = TImg.attr("TURI");
	MOImg.attr("IID",IID);
	jQuery('#MOImg').attr("src", SRC);
}



;

