$(document).ready(function() {
	
	function init(){
		loadFonts();
		if($('div.secondary').length > 0) checkWidth();		
		if ($('#pg_our_projects').length > 0 ) projectPage();		
		if ($('#images').length > 0) projectCycle();	
		
		$('.popup a').lightBox({
			imageLoading: siteurl+'images/lightbox-ico-loading.gif',
			imageBtnClose: siteurl+'images/lightbox-btn-close.gif'
		});
	}
	
	function projectCycle(){
		if ($('#cycleGroup').children().length > 1) {
		$('#cycleGroup').after('<div id="pagination">View Images &#187; ').cycle({ 
		    fx:     'fade', 
		    speed:  'fast', 
		    timeout: 0, 
		    pager:  '#pagination' 
		});
	};
	}
	
	function projectPage(){		
		$("#keywords").keyup(ajaxCall).mouseup(ajaxCall);
		$('#search_market').change(ajaxCall);
		$('#search_service').change(ajaxCall);		
		projectHover();
	}
	
	function projectHover(){
		$('.project').hover(function(){			
			if ($(this).find('.project_link').length > 0) $(this).css('background-color','#dfe1e6');			
		}, function(){
			if ($(this).find('.project_link').length > 0) $(this).css('background-color','#fff');
		}).click(function(){                  
			if ($(this).find('.project_link').length > 0) window.location.href = $(this).find('.project_link').attr('href');
			return false;
		});
	}
	
	function ajaxCall(){	
			var defaultEntries = $('div#entries');
			var market = $("#search_market option:selected").attr("value");
	        var service = $("#search_service option:selected").attr("value");
	        var keywords = $("#keywords").attr("value").toLowerCase();
			var searchString = market +"/"+ service +"/"+ keywords;
	
	        if(keywords=="search by keywords") searchString="";

			$.ajax({
			    type: "GET",
			    url: siteurl + "search/" + searchString,
			    dataType: "xml",
			    success: function(xml) {
			        var projectString = getProjects($(xml), keywords, searchString=="search/keywords");                	
			        
					if(projectString=="") projectString = "<div class='program'>There are programs matching your search.</div>";
			
			        $('div#entries').empty();
			        $('div#entries').append(projectString);
			    }
			});
	    }

	    function getProjects(xml, searchString, urlString){
	
	        var htmlString='';
	        var schoolString = '';
			var curMarket = '';
			var oldMarket = '';
			var count = 0;
			
			$(xml).find('search').each(function(){
				var market = $(this).attr('marketFull');
				var service = $(this).attr('serviceFull');
				var keywords = $(this).attr('keywords');
				var string = "";
				
				if (market !="") { market = "<em>"+market+"</em>"; };
				if (service !="") { service = "<em>"+service+"</em>"; };
				if (keywords !="") { keywords = "<em>"+keywords+"</em>"; };
				
				if (market == "" && service == "" && keywords !="") { string = "You searched for "+keywords+"." };
				if (market == "" && service != "" && keywords !="") { string = "You searched for "+keywords+" in "+service+"." };
				if (market != "" && service == "" && keywords !="") { string = "You searched for "+keywords+" in "+market+"." };
				if (market != "" && service != "" && keywords !="") { string = "You searched for "+keywords+" in "+market+" and "+service+"." };
				if (market != "" && service != "" && keywords =="") { string = "You searched for "+market+" and "+service+"." };
				if (market != "" && service == "" && keywords =="") { string = "You searched for "+market+"." };
				if (market == "" && service != "" && keywords =="") { string = "You searched for "+service+"." };
				if (market == "" && service == "" && keywords =="") {  };
								
				htmlString += '<h3 class="searchString">'+string+'</h3>';
			});

	        $(xml).find('project').each(function(){
		
				var title = $(this).attr('title');            
				var client = $(this).attr('client');
				var market = $(this).attr('market');
				var img = $(this).attr('thumb');
				var urlTitle = $(this).attr('url_title');
				
				if (oldMarket != market) { 
					if(count !=0) { htmlString += '<div class="clr"></div></div>'; }
					htmlString += '<div class="group">';
					htmlString += '<h2>'+market+'</h2>'; 
				};		
				
				htmlString += '<div class="project"><h3 class="name">'+title+'</h3>';	
				if (img !=""){ 	htmlString += '<img src="'+siteurl+'img/projects/small_thumbs/'+img+'" height="43" width="43" alt="'+client+' '+title+'" />'; }
				htmlString += '<p><a href="'+siteurl+'our_projects/'+urlTitle+'" title="'+client+' '+title+'" class="project_link">&nbsp;'+client+'</a></p>';
				htmlString += '</div>';
				
				oldMarket = market;
				++count;
	        });
	
			projectHover();

	        return htmlString;
	    }
	
	function loadFonts(){		
		Cufon.replace('#top_section #welcome .article h1', {fontFamily: 'Gotham', textShadow: '1px 1px rgba(5, 20, 63, 1)' });
		Cufon.replace('#top_section #welcome .article h2', {fontFamily: 'Gotham',	textShadow: '1px 1px rgba(5, 20, 63, 1)' });		
		Cufon.replace('#top_section #elements ul.bullets li', {fontFamily: 'Gotham'});
		Cufon.replace('#feature_projects h3', {fontFamily: 'Gotham'});
		Cufon.replace('#page h1', {fontFamily: 'Gotham'});
		Cufon.replace('#intro', {fontFamily: 'Gotham', textShadow: '1px 1px rgba(5, 20, 63, .1)'});
		Cufon.replace('.col h2', {fontFamily: 'Gotham'});
		Cufon.replace('#elements a#login', {fontFamily: 'Gotham', hover: true});		
		Cufon.replace('#description h2', {fontFamily: 'Gotham'});
		Cufon.replace('#other h2', {fontFamily: 'Gotham'});
	}
	
	function checkWidth(){
		if($('#page_img').length > 0 ) $('.secondary #content').css('width', '410px'); 
	}
	
	init();
});



$(window).load(function() {
	if ($('div#cycleGroup').length != 0){
		var minHeight = 0;
		$('div#cycleGroup img').each(function(){
			var h = $(this).height();
			var w = $(this).width();
			if(h >= minHeight) minHeight = h+50;		
			$(this).css({"width": w,"height": h});
		});
		$('div#images').css({height: minHeight});
	};
});