// JavaScript Document

$(document).ready(function(){

	var totalWidth = 0;
	var curFilterArray = new Array;
	var portfolioURL = 'xml/portfolio.xml';
	
	//DEBUG	
	function showFilter(filter){
		//$("#debug").empty().append(" The current Filter is: " + filter);
	}
	//AJAX CALLBACKS
	function start(){ 
		jQuery('#ajax_loader').html('<p>loading</p>');
	}
	function finish(){
		jQuery('#ajax_loader').remove();
		
		//Scroll Projects
		$(function(){
			//Get our elements for faster access and set overlay width
			var projList = $('#project_navigation');
			var projItem = $('.proj_item:visible');
			var margin = 20;
			
			projList.css({
				overflow: 'hidden'
			});
			
			var projItemHeight = margin + parseFloat(projItem.height());
			var listHeight = projItem.length * projItemHeight;
		
			projList.mousemove(function(e){
			
				var divHeight = projList.height();
				var pos = (e.pageY - projList.offset().top - 20) * (listHeight + 100 - divHeight) / divHeight;
				projList.scrollTop(pos);
			});
		});
	}	
	//TAG CLOUD
	
	$('#content').frontPage({
		ajaxOpts: { url: 'vita.html'}
	});
	$('.front_page').live('click',function(){
			$('#content').frontPage({
		ajaxOpts: { url: 'vita.html'}
	});
	});
    
	$('#tag_cloud').tagCloud(portfolioURL);
	
    $("#tag_filter").live('click', function(){
    	
		var testTag = $(this).text();
		
		if (curFilterArray.length == 0){
			curFilterArray[curFilterArray.length] = testTag;	
		}else{
        for(i = 0; i < curFilterArray.length; i ++){
			if (testTag == curFilterArray[i]){
				curFilterArray.splice(i, 1);
				break;
				}
			if(i == curFilterArray.length -1){
				curFilterArray.push(testTag);
				break;
				}
			}
		}
		
		$(this).toggleClass('filter_on');
		//showFilter(curFilterArray);
		$('#project_navigation').tagFilter({
			ajaxOpts: {url: portfolioURL},
			parentElement : 'project',
			curFilter: curFilterArray,
		    precallback: start,
            callback: finish
			});
		
});
	
    
	//PROJECT LIST
    $("#project_navigation").jParse({
         ajaxOpts: { url: portfolioURL},
		 precallback: start,
         callback: finish
    });

	$("div.proj_item").live('mouseenter', function() {
        	$(this).find("img").fadeTo("fast", 1);
		}).live('mouseleave',function(){
		    $(this).find("img").fadeTo("fast", 0.2);
  		});
				
	$("img.thumb").live('mouseenter', function() {
        	$(this).fadeTo("fast", 1);
					
		}).live('mouseleave',function(){
		    $(this).fadeTo("fast", 0.2);
  		});
	
	//LOAD PROJECT GALLERY AND THUMB NAV
    $('.proj_item').live('click',function(){	
		
		var index = ($(this).attr('id'));
		
		$('#thumbNavWrapper').thumbNav({
			ajaxOpts: { url: portfolioURL},
			parentElement : 'project',
			elementID : index,
			parentAttr: 'index'
		})
		
		$('#content').projectGallery({
			ajaxOpts: { url: portfolioURL},
			parentElement : 'project',
			elementID : index,
			parentAttr: 'index'
		})

	});
   
   //LOCAL SCROLL
   $('#thumbNavWrapper').localScroll({
	 	center : 'true',
	 	axis : 'x',
		margin :'false',
		duration : '1000',
		hash:'false',
		lazy : 'true'
		});
	$('#content').localScroll({
	 	center : 'true',
	 	axis : 'x',
		margin :'false',
		duration : '1000',
		hash:'false',
		lazy : 'true'
		});
		
	$('.styleswitch').live('click',function(){
			var color = $(this).attr('rel');
			
			if (color == 'white'){
			switchStylestyle(color,'black');
			
			$(this).attr('rel','black');
			
			}else if (color == 'black'){
			
			switchStylestyle(color,'white');
			
			$(this).attr('rel','white');
				
			}
			

			//return false;
		});
		
	$('.unlock').live('click',function(){
	
		portfolioURL = 'xml/portfolio.xml';
	
		 $("#project_navigation").jParse({
         ajaxOpts: { url: portfolioURL},
		 precallback: start,
         callback: finish
			
 	  	 });
		$('#tag_cloud').tagCloud(portfolioURL);
	});
	
	
		$('.lock').live('click',function(){
	
		portfolioURL = 'xml/portfolio_Selected.xml';
	
		 $("#project_navigation").jParse({
         ajaxOpts: { url: portfolioURL},
		 precallback: start,
         callback: finish
			
 	  	 });
		$('#tag_cloud').tagCloud(portfolioURL);
	});
	
	
	function switchStylestyle(styleOn,styleOff)
	{
		$('link[rel*=style][title]').each(function(){
			
			//this.disabled = true;
			if (this.getAttribute('title') == styleOn) this.disabled = false;
			if (this.getAttribute('title') == styleOff) this.disabled = true;
		});
		
	}

		
		
});

