$(document).ready(function() {
	function init(){
		if ($('body#home').length >0) {
			loadIcon();
			homePage();
		}
				
		if ($('body#home').length == 0) insidePages();
		if ($('body#s1_clients').length > 0) projectCycle();
		if ($('body#s1_services').length > 0 && $('div.bucket_list').length > 0) servicesBuckets();
		
		if ($('div#cascade-links').length !=0) cascadeLists();
		
		formFields();		
		playPopup();
	}
	
	function loadIcon(){
		swfobject.embedSWF(siteurl+"swf/logo.swf", "icon", "56", "56", "9.0.124");
		$('object#icon a').click(function(){ window.location = siteurl; });
	}
	
	function homePage(){
		var fadeSpeed = 400;
		var stuck = true;
		$('#overlay').css('background-color','rgb(235,234,217)').after('<div id="rollovers"></div>');
		$('#overlay').animate({ opacity: 0.8}, 0, function() {
		    // Animation complete.
		  });
						
		// FADE IN / OUT SERVICE GROUPS AND OVERLAY
		$('ul#services_groups li a').hover(function(){			
			$('#overlay').stop(true,true).fadeOut(fadeSpeed).stopTime("fadeIn");
		}, function(){});
		
		$('div#hover').hover(function(){},function(){
			$('#overlay').stop(true,true).fadeIn(fadeSpeed).removeClass('stick');
			$('#rollovers').fadeOut(fadeSpeed);
		});
		
		// LINK EFFECTS
		$('ul#services_groups li a').each(function(i)
		{ 
			var backgroundColor = $(this).parent().css('backgroundColor');		
			
			$(this).hover(function()
			{				
				$('#rollovers').html('');
				
				var id = $(this).parent().attr('id');
				var newID = id.replace('services_', '');
														
				$('#rollovers').hide().css({
					'background-color': backgroundColor, 
					'opacity': 0.8, 
					'left': 237*i 
				}).fadeIn(fadeSpeed).addClass('stick');
				
				$('#rollovers ul').css({
					'position': 'absolute',
					'bottom': 0
				})
				
				ajaxCall(newID);		
			}, 
			function(){	
				// NULL							
			});					
		});		
	}
	
	function ajaxCall(newID){
		$('#rollovers').html('');
		$.ajax({
			type: "GET",
			url: siteurl+'ajax/'+newID,
			dataType: "xml",
			success: function(xml){
				var newTags = getServices(xml);				
				$('#rollovers').append(newTags);
			}
		});
	}
	
	function getServices(xml){
		 var htmlString='<ul>';
		$(xml).find('li a').each(function(){
            var name_text = $(this).text();
            var details_url = $(this).attr('href')
             htmlString += '<li>';
             htmlString += '<a href="'+details_url+'" onclick="return false;">'+name_text+'</a>';
             htmlString += '</li>';
        });

        htmlString += '</ul>';
        return htmlString;
	}
	
	function insidePages(){
		var backgroundColor = $('div#bannerSpan').css('backgroundColor');
		$('div#banner h1').before('<div id="bannerSpan"></div>')
		$('div#bannerSpan').css({'background-color': backgroundColor, 'opacity': 0.8 });
	}
		
	function projectCycle(){
		if ($('#case_study_images').children().length > 1) {	
			$('#caption').before('<div id="pagination">View Images &#187; ');		
			$('#case_study_images').cycle({ 
			    fx:     'fade', 
			    speed:  'fast', 
			    timeout: 0, 
			    pager:  '#pagination',
				after: loadCaption
			});
		};
	}
	
	function loadCaption(){
		$('#caption').html(this.alt);
	}
	
	function servicesBuckets(){
		$('.inner').hide();
		$('.bucket_list_list').hide();		
		$('ul.bucket_list_list li a').click(function(){ return false; })
		
		$('ul#services_groups li a').hover(function(){
			var thisLink = $(this).parent().attr('id');
			var newListBackground = thisLink.replace('services_', 'bucket_list_');
			var newList = thisLink.replace('services_', 'ul_');
			
			if ($('.sticky').length >0 ) $('.sticky').fadeOut().removeClass('sticky');
			
			$('#'+newListBackground).addClass('sticky').fadeTo(0).animate({ opacity: 0.8}, 200, function() {
			    // Animation complete.
			  });;
			$('#'+newList).addClass('sticky').fadeIn();
			
		},function(){
			// NULL
		});
	}
	
	function cascadeLists(){
		var oldClass = '';
		
		$('div#cascade-links h3').each(function(){
			$(this).click(function(){
				var thisClass = $(this).attr('class');
				var nextObj = $('.'+$(this).attr('class')+'-list');		
												
				$(oldClass).slideUp(500);	
				$('.expanded').removeClass('expanded');				
							
				$(this).addClass('expanded');
				$(nextObj).slideDown(1000);
				
				oldClass = nextObj;
			});
		})
	}
	
	function formFields(){		
		$('body#s1_contact form input').each(function(){
			if ($(this).attr('placeholder')){
				$(this).focus(function(){
					if ($(this).attr('value') != "") $(this).attr('value', '').removeClass('unfocus');
				}).blur(function(){
					if ($(this).attr('value') == "") $(this).attr('value', $(this).attr('placeholder')).addClass('unfocus');					
				}).attr('value', $(this).attr('placeholder')).addClass('unfocus');
			}
		});   
	}
	
	function playPopup(){
		$('.play a').fancybox({
				'padding': 0,
				'autoScale'	: false,
				'transitionIn': 'fade',
				'transitionOut': 'fade',
				'title': this.title,
				'width': 480,
				'height': 360,
				'href': this.href,
				'type': 'swf'
			});
	}
	
	init();
});