// bd.js
// Portfolio Functions
// portfolio.php, portfolio/index.php

// for filter - store which ones are checked.
// portfolio.php, portfolio/index.php
function setCookie(c_name,value,expiredays){
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString()) + "; path=/";
}

function getCookie(c_name){
	if (document.cookie.length>0){
		c_start=document.cookie.indexOf(c_name + "=");
  
  		if (c_start!=-1){
			c_start=c_start + c_name.length+1; 
    		c_end=document.cookie.indexOf(";",c_start);
    		if (c_end==-1) c_end=document.cookie.length;
    
			return unescape(document.cookie.substring(c_start,c_end));
    	} 
  }
	return "";
}

window.addEvent('domready', function(){
	// client thumbs on main portfolio page - mouse over effect
	// portfolio.php
	if($('clientThumbs')){
		$$('#clientThumbs div').each(function(e){
			e.morph({
				opacity: [0,.4]
			});
		});
		$$('#clientThumbs div').addEvents({
			mouseenter: function(e){
				this.morph({
					opacity: [.4,1]
				});
			},
			mouseleave: function(e){
				this.morph({
				 opacity: .4
				});
			}
		});
	}
	
	// filter dropdown
	// portfolio.php, portfolio/index.php
	if($('myForm')){
		if( getCookie('filterCookie')){
			//cookie indicating which filter values were checked.
			var filter = getCookie('filterCookie');
			var temp = new Array(); 
			temp = filter.slice(0,filter.length - 1).split(',');
			var projectTags;
			var found = false;
			var checkedTagsStrS = "";
		
			boxes = $('myForm').filterbox.length;
			
			//check correct boxes
			for (i = 0; i < boxes; i++) {
				if ((filter.indexOf($('myForm').filterbox[i].value)) >= 0) {
					if(checkedTagsStrS != ""){checkedTagsStrS += ", ";}
					checkedTagsStrS += $('myForm').filterbox[i].id;
					$('myForm').filterbox[i].checked = true;
				}
			}	
			
			if($('breadcrumbs')){
				$('breadcrumbs').innerHTML = "filters: " + checkedTagsStrS;				
			}
			
			// for all hidden input fields
			$$('#portfolioNav li input').each(function(e){
				projectTags = e.value;
				found = false;
						
				//for all checked tags
				for(var i=0; i<temp.length; i++) {
					if (projectTags.indexOf(temp[i]) >= 0)
					{
						found = true;
						break;
					}
				}
				if(found) {
					e.getParent().addClass('found');			  
					
					if(!(e.getParent().hasClass('hidden'))){
						e.getParent().set({'styles': {'display': 'block'}});
					}
				}else{
					e.getParent().removeClass('found');
					e.getParent().set({'styles': {'display': 'none'}});	
				}
			});
			
			//clients in nav.
			$$('#portfolioNav .sitem').each(function(e){
				if (e.hasClass('found')){
					var client = e.getFirst('a').innerHTML;
					var spaceFix = / /gi;
					client = client.replace(spaceFix,'');
					spaceFix = '\n';
					client = client.replace(spaceFix,'').toLowerCase();
					spaceFix = 'amp;';
					client = client.replace(spaceFix,'').replace('&','');
	
					$$('#clientThumbs div').each(function(f){
						var fid = f.id.toLowerCase();
						fid = fid.replace(spaceFix,'');
						if(fid == client){
							f.addClass('show');
							f.addEvent('click',function(g){
								location.href=e.getNext('li .found').getFirst('a').href;
							});
						}
					});
					e.getFirst('a').href = e.getNext('li .found').getFirst('a').href;
				}
						
				$$('#clientThumbs div').each(function(f){
					if(f.hasClass('show')){
						f.set({'styles': {'display': 'block'}});	
					}else{
						f.set({'styles': {'display': 'none'}});	
					}
				});
			});
		}else{
			boxes = $('myForm').filterbox.length;
			
			for (i = 0; i < boxes; i++) {
				$('myForm').filterbox[i].checked = true;
			}
			$$('#portfolioNav .sitem').each(function(e){
				var client = e.getFirst('a').innerHTML;
				var spaceFix = / /gi;
				client = client.replace(spaceFix,'');
				spaceFix = '\n';
				client = client.replace(spaceFix,'').toLowerCase();
				spaceFix = 'amp;';
				client = client.replace(spaceFix,'').replace('&','');
	
				$$('#clientThumbs div').each(function(f){
					var fid = f.id.toLowerCase();
					fid = fid.replace(spaceFix,'');
					if(fid == client){
						f.addEvent('click',function(g){
							location.href=e.getNext('li').getFirst('a').href;
						});
					}
				});
						
			});
		}
	}
	

	if($('portfolioSelect1')){
		$('portfolioSelect1').addEvent('click',function(e){
			if(this.hasClass('active')){
				
				// for all checkboxes.  get if clicked and put value in array.
				var checkedTags = new Array();
				var checkedTagsStr = "";
				var checkedTagsStrS = "";
				var projectTags;
				var found;
				
				boxes = $('myForm').filterbox.length;
					
				for (i = 0; i < boxes; i++) {
					if ($('myForm').filterbox[i].checked) {
						checkedTags.push($('myForm').filterbox[i].value);
						checkedTagsStr = checkedTagsStr + $('myForm').filterbox[i].value + ",";
						if (checkedTagsStrS != ""){
							checkedTagsStrS = checkedTagsStrS + ", ";
						}
						checkedTagsStrS = checkedTagsStrS + $('myForm').filterbox[i].id;					
						setCookie('filterCookie', checkedTagsStr, 4);
					}
				}
				
				if($('breadcrumbs'))
				{
					$('breadcrumbs').innerHTML = "filters: " + checkedTagsStrS;
				}
				
				$$('#portfolioNav li input').each(function(e){
					projectTags = e.value;
					found = false;
					
					//for all checked tags
					for(var i=0; i<checkedTags.length; i++) {
						if (projectTags.indexOf(checkedTags[i]) >= 0)
						{
							found = true;
							break;
						}
					}
					if(found)
					{
						if(e.getParent().hasClass('hidden')){
							e.getParent().addClass('found');			  
						}else{
							e.getParent().addClass('found');
							e.getParent().set({'styles': {'display': 'block'}});
						}
						//e.getNext().getFirst('a').href = e.getNext().getFirst('a').href.substring(0,e.getNext().getFirst('a').href.indexOf("?")) + "?" + checkedTagsStr;
					}else{
						e.getParent().removeClass('found');
						e.getParent().set({'styles': {'display': 'none'}});	
					}
				});
				
				$$('#clientThumbs div').each(function(f){
					f.removeClass('show');
				});
				
				$$('#portfolioNav .sitem').each(function(e){
					
					if (e.hasClass('found')){
						var client = e.getFirst('a').innerHTML;
						var spaceFix = / /gi;
						client = client.replace(spaceFix,'');
						spaceFix = '\n';
        				client = client.replace(spaceFix,'').toLowerCase();
						spaceFix = 'amp;';
						client = client.replace(spaceFix,'').replace('&','');
						$$('#clientThumbs div').each(function(f){
							var fid = f.id.toLowerCase();
							fid = fid.replace(spaceFix,'');
							if(fid == client){
								f.addClass('show');
								f.addEvent('click',function(g){
									location.href=e.getNext('li .found').getFirst('a').href;
								});
							}
						});
						e.getFirst('a').href = e.getNext('li .found').getFirst('a').href;
					}
					
					$$('#clientThumbs div').each(function(f){
						if(f.hasClass('show')){
							f.set({'styles': {'display': 'block'}});	
						}else{
							f.set({'styles': {'display': 'none'}});	
						}
					});
				});
					
				this.removeClass('active');
				this.set({'styles': {'background-position' : '0 0'}});
				$('filterContainer').set({'styles': {'display': 'none'}});
			}else{
				this.addClass('active');
				this.set({'styles': {'background-position' : '-262px 0'}});
				$('filterContainer').set({'styles': {'display': 'block'}});
			}
			return false;
		});	
	}								 
							
	$$('#portfolioNav li').addEvents({
		mouseenter: function(e){
			if((this.getFirst('a').className != 'active') && (!(this.hasClass('current-cat')))){
				this.getFirst('a').morph({
					'background-color': '#060607'
				});
			}
		},
		mouseleave: function(e){
			if((this.getFirst('a').className != 'active') && (!(this.hasClass('current-cat')))){
				this.getFirst('a').morph({
				 'background-color': '#0e0f0e'
				});
			}
		}
	});
	
	
	// portfolio images by project.
	// portfolio/index.php
	$$('#thumbs li').addEvents({
		click: function(e){
			e.stop();
			if($('oldImg')){
				$('oldImg').destroy();
			}
			if($('newImg')){
				$('newImg').id = 'portfolioImg';
			}
			
			var newImgPath = this.getFirst('a').getFirst('img').src;
			newImgPath = newImgPath.substring(0,newImgPath.lastIndexOf('/')-2) + newImgPath.substring(newImgPath.lastIndexOf('/'),newImgPath.length); 
		
			if(newImgPath.lastIndexOf('_sm.jpg') > 0){
				newImgPath = newImgPath.replace('_sm.jpg','_lg.jpg');
			}
		
			var imgPath = newImgPath;
			var myDiv = $('portfolioMain');		
			
			var morph = new Fx.Morph('portfolioImg', {link: 'chain'}, {duration: 1000, wtransition: Fx.Transitions.Sine.easeOut});
		   	
				var oldImage = $('portfolioImg');
				oldImage.set('styles', {
					'z-index': '3'
				});

				var myImage = new Asset.images(imgPath, {
         			onProgress: function() {
						morph.start({'opacity': .5});
					},
         			onComplete: function(){
						myImage.id = 'newImg';
             			myImage.inject(myDiv);
						$('portfolioMain').getFirst().getNext().id = 'newImg';
             			morph.start({'opacity': 0});
						$('newImg').morph({'opacity' : [0,1]});
            			$('portfolioImg').id = 'oldImg';
						
					}
         		});
		}
	});
	
});




