click = 0;
xml_path = "./xml/projects.xml";
var  xhr_thumbs;

$(document).ready(function(){
	basicFunction.setup();	
});



//Ajax setUp

$.ajaxSetup({
  	type: "GET",
 	url: xml_path,
	dataType: "xml"
});


var basicFunction = basicFunction || {
	
	setup: function(){
		$('#slides').fadeOut();

	
		this.loadContent(0);
		
		//Load Titles in Other-work
		basicFunction.setUpTitles();
		
		
		//Slide
		//Next SLide
		$('#main-slideshow, #next a','#main-content').click(function(){
			var limit = $('.slide').length;
			if(click+1 < limit){
				click ++;
				$('#prev a').fadeTo(200,1);
				basicFunction.navSlide('#slides');
				(click+1 >= limit)?
					$('#next a').fadeTo(1,0.1):
					$('#next a').fadeTo(200,1);
				
			}
		});
		//Previous Slide
		$('#prev a','#main-content').click(function(){
			if(click > 0){
				click --;
				$('#next a').fadeTo(200,1);
				basicFunction.navSlide('#slides');
				(click <= 0)?
					$(this).fadeTo(1,0.1):
					$(this).fadeTo(200,1);
			}
		});
		
		//Check to see if we need Slide-nav
		basicFunction.checkForSlideNav();
		
		
		//Fade out slide-nav prev 
		$('#prev a').fadeTo(1,0.1);
		
		// Determin slide-nav visibility
		if($('.slide').length > 1)
			$('li','#slide-nav').fadeIn('fast');
		
		//Binding all Events
		basicFunction.recurringEvents();
	},
	
	loadContent: function(index, f){
		$('#slides').hide();
		$.ajax({
			success: function(xml){
				//console.log(index);
				var target = $(xml).find('project[id='+index+']');
				
				var title = '<span class="eyebrow '+target.find('client').attr('status')+'">'+target.find('client').text()+'</span>'+target.find('title').text();
				var when = target.find('timeframe').text();
				var status = target.find('timeframe').attr('status');
				var desc = target.find('description').text();
				var slides = "";
				var roles = "";
				var skills = "";
				var slides_obj = $('#slides');
				var roles_obj = $('#roles');
				var skills_obj = $('#skills');
	
				
				target.find('image').each(function(i,e){
					var s = $(this).find('slide').text();
					slides +='<li class="slide" style="background: transparent url('+s+') center center no-repeat;"></li>';
				});	
				
				
				target.find('role').each(function(i,e){
					roles +='<li>'+$(this).text()+'</li>';
				});	
				
				target.find('skill').each(function(i,e){
					skills +='<li>'+$(this).text()+'</li>';
				});	
				
				
					$('h3','#project-content').hide().html(title).fadeIn();
					$('#when','#project-content').removeClass().addClass(status).hide().html(when).fadeIn();
					
					
					//Load in Slides
					(slides)?
						slides_obj.html(slides).css('marginLeft','0').hide().fadeIn('fast',function(){
							var x = $('.slide');
							
							$('#slides').width(function(){
								return x.length*x.outerWidth(true);
							});
							
							if(x.length)
								$(this).parents('#main-slideshow').slideDown('slow').siblings('#slide-nav').fadeIn('fast');
						}):
						slides_obj.hide().parents('#main-slideshow').slideToggle('fast').siblings('#slide-nav').fadeOut('fast');
						
					//reset click	
					click = 0;
	
				
					
					//Load in Roles
					(roles)?
						roles_obj.hide().html('<span class="labels">Roles</span><ul id="role-list">'+roles+'</ul>').fadeIn():
						roles_obj.html('');
						
					//Load in Skills
					(skills)?
						skills_obj.hide().html('<span class="labels">Skills</span><ul id="skills-list">'+skills+'</ul>').fadeIn():
						skills_obj.html("");
						
						
					//Load in Description
					(desc)?
						$('#description').hide().html('<span class="labels">About the Project</span>'+desc).fadeIn():
						$('#description').html("");
						
					$('#project-id').attr('value',index)
	
					//Scroll Window to top
					basicFunction.scrollWindow();
					basicFunction.checkForSlideNav();
				if(typeof f === 'function')f();
			},
			complete: function(){
				basicFunction.checkForSlideNav();
			}
		});
	},

	
	setUpTitles: function(){
		//Set loading Data
		$.ajax({
			success: function(xml){
				var project = $(xml).find('project');
				
				var result = "";
				var exclude = $('#project-id').attr('value');
				
				project.each(function(i,e){
					if(i == 0)
						return;
					
					
					var title = $(this).find('title').text();
					var client = $(this).find('client').text();
					var status = $(this).find('client').attr('status');
					
					result += '<li id="'+i+'"><a href="#" rel="./xml/projects.xml"><span class="eyebrow '+status+'">'+client+'</span>'+title+'</a></li>'
				});
				
				
				
				$('#other-work').html(result);
				//If there is no content hide sub-section
		
				if($('li','#other-work').length)
					$('#sub-content').fadeIn();
				
				//set other-work pane height
				$('#thumbs, #thumb-box, #thumb-screen, #overflow-pane' ,'#sub-content').height(function(){
					var other_work = $('li','#other-work');
					return other_work.length*other_work.outerHeight(true);
				});
				
				//Attaching events to list
				basicFunction.recurringEvents();
			}
		});
	},
	
	loadTitle: function(index, f){
		$.ajax({
			success: function(xml){
				var target = $(xml).find('project[id='+index+']');
				var title = target.find('title').text();
				var client = target.find('client').text();
				var client_status = target.find('client').attr('status');
				
				var result = '<li id="'+index+'"><a href="#"><span class="eyebrow '+client_status+'">'+client+'</span>'+title+'</a></li>';
				
				$('#other-work').append(result);
			},
			complete: function(){
			basicFunction.recurringEvents();
			}
		});
	},
	
	recurringEvents: function(){
		// Unbind Click event so they do not bubble
		$('li','#other-work').unbind('mouseenter').unbind('mouseleave');
		$('a','#other-work').unbind('click');
		
		//Fade Out Other Work For Non IE
		$('li','.non-ie #other-work').fadeTo(1,0.1).hover(function(){
			$(this).fadeTo(100,1);
			var index = $(this).attr('id');
			document.getElementById('thumb-screen').style.background = 'transparent url(./images/preloader.gif) center center no-repeat';
			basicFunction.getThumb(index);
		},function(){
			xhr_thumbs.abort();
			$(this).fadeTo(1,0.2);
			document.getElementById('thumb-screen').style.background = 'transparent none';
		});
		
		//For IE Load thumbnail Non Fade
		$('li','.ie #other-work').hover(function(){
			var index = $(this).attr('id');
			document.getElementById('thumb-screen').style.background = 'transparent url(./images/preloader.gif) center center no-repeat';
			basicFunction.getThumb(index);
		},function(){
			xhr_thumbs.abort();
			document.getElementById('thumb-screen').style.background = 'transparent none';
		});
		
		//Load Content when CLicking on Other-work
		$('a','#other-work').bind('click',function(){
			var parents = $(this).parents('li');
			var index = parents.index();
			var toLoad = parseInt(parents.attr('id'));
			var toLoadTitle = $('#project-id').attr('value');
			
			basicFunction.loadContent(toLoad);
			basicFunction.loadTitle(toLoadTitle);
			parents.slideUp('fast',function(){
				parents.remove();
			});
		});
		
		
		//Disable all the # links
		$('a[href=#]').click(function(){
			return false;
		});
	},
		
	checkForSlideNav: function(num){
		if(!num)
			num = $('.slide').length;
	
		if(num <= 1){ 
			$('li','#slide-nav').fadeOut();
		}else{
			$('#prev a').fadeTo(100,0.1);
			$('#next a').fadeTo(100,1);
		}
	},
	
	scrollWindow: function(){
		var t = $('body').offset().top;
	
		if(!t)
			$('html,body').animate({
				scrollTop: t
			},500);
	},
	
	getThumb: function(index){
		 xhr_thumbs = $.ajax({
			success: function(xml){
				var thumb = $(xml).find('project:eq('+index+')').children('image:first').find('fullpath').text();
				basicFunction.exchangeThumbnail(thumb);
			}
			
		});
	},
	
	exchangeThumbnail: function(img){
			var x = $('#thumb-screen');
			x.hide(1,function(){
				document.getElementById('thumb-screen').style.background = "#fff url('"+img+"') center center no-repeat";
			}).fadeIn('fast');
			
	},
	
	navSlide: function(slide){
			//Slide Navigation
			$(slide).stop().animate({
				'marginLeft':-740*click
			},500);
	}

}















