

///////////////////////////////////////////////////////
// DOCUMENT ON READY 

	$(document).ready(function() {

	//use this line to disable JS without disconnecting the files
	// return true;

///////////////////////////////////////////////////////
// GLOBAL SCRIPTS

	//// oh how we love old versions of IE and their incapacities

		// fix the transparent PNG issues 
		// in older vesions fo IE
		$(document).pngFix(); 


	//// Search Box Default Value
		
		$("#search-text").focus(function () {
			
			//if initial value then clear text input
			if ($(this).attr('value') == "Search") {
				$(this).attr("value", "");

			//otherwise just select the contents of the box
			} else {
				$(this).select();
			}

		});
		
		$("#search-text").blur(function () {
			
			//if initial value then clear text input
			if ($(this).attr('value') == "") {
				$(this).attr("value", "Search");
			}

		});
		

	//// Rollover Box Code

		// Find number fo rollover items on the page
		if ($(".rollover-box").size() >= 1) {

				// Hide the roll over box contents
				$(".rollover-box span").hide();
				// onHover reveal, hide onRollout
				$(".rollover-box").livequery(function(){ 
  			    	$(this).hover(
					function () {
						$(this).find(".overlay").show();
						$(this).find("span").show();
					},
					function () {
						$(this).find(".overlay").hide();
						$(this).find("span").hide();
					});
				});
	
		}





///////////////////////////////////////////////////////
// HOME PAGE SCRIPTS


	//// Spotlight Thumbs Management
		
		// check to see if there is a spotlight
		if ($("#spotlight").size() == 1) {
		
			var props_on = {opacity: '0.7'};
			var props_off = {opacity: '1.0'};

			// hide the first thumb by default
			// as this is the one initially on view 
			$('#spotlight #thumbnails div:eq(0)').hide();
		
			//click on thmbnails to enlarge!
			$("#spotlight #thumbnails div").click(
				function () {
					var spot = $("#primary-image img");
					var thumb = this;
					var hidden = $("#thumbnails ."+ $(spot).attr('id'));

					// 3/ Show thumbnail for old SPOTLIGHT
					$(hidden).show();

					// 2/ Hide this thumbnail
					$(thumb).hide();
				
					// 1/ Set SPOTLIGHT with this item
					$("#primary-image img").attr({
						id: $(this).attr('class'), 
						src: $(this).find('.thumb').attr('src').replace('/thumbnails/','/spotlight/').replace('Small','Big')
					});
				}
			);
			$("#spotlight #thumbnails a").attr("href","#");

		}




///////////////////////////////////////////////////////
// WORK FILTER SCRIPTS


	//// Filters Management
		
		// Find categories content ont he page
		if ($("#categories-list").size() == 1) {
		
			//AVAILABLE or ACTIVE - click
			$("#cats li.available, #cats li.selected, #cats li.active").live('click', function(event) {
					//check to see if JSON already loading
					if (!$(this).hasClass("loadingjson")) {
						//add and remove filter from DB/AJAX
						var cat = $(this).attr('id');
						var list = $(this).parent().attr('id');
						
						//perform JSON request
						if ($(this).hasClass('available')) {
							$.SD.setFilter(this, 'add');
						} else {
							$.SD.setFilter(this, 'remove');
						}
					}
					return false; //disable link
			});

			//UNAVAILABLE - click (DISABLE BUTTONS) and update ALT
			$("#categories-list li.unavailable").live('click', function(event) {
				return false; //disable link
			});

		}

	//// SEARCH RESULTS Management
		
		// Find categories content ont he page
		if ($("#search").size() == 1) {

			//switch of css SCROLL
			$("#results").css("overflow","hidden");

			//Set totals
			$.SD.setResultTotals();
			
			if ($("#results > div").size() > 1) {
				//draw the pagination
				$.SD.drawPaginator();		
			}
			
		}



///////////////////////////////////////////////////////
// WORK SCRIPTS


	//// work details Management
		
		// Find categories content ont he page
		if ($("#work").size() == 1) {
		
			//click link to update question
			$("#work #pager a").click(
				function () {
					$.SD.updateWork(this); //update stage
					return false; //disable link
				}
			);
			
			$.SD.reconnectPaginate();

		//	return false; //disable link

		}

		// Find paginater on the page
		if ($("#work .paginate").size()==1) {

			var results = $('#results, #projectportfolio');

			$('.paginate li.page').live('click', function() {
				if (!$(this).hasClass('scrolling')) {
					$(results).serialScroll().trigger('goto', $(this).attr('id').replace('item','')); 
					$(results).serialScroll().trigger('notify', $(this).attr('id').replace('item','') );
				}
			});

			//load in options for paging through with the keybaord
		    $(document).keypress(function (e) {
    			if (e.which == 97) {
    				$(results).trigger( 'prev');
    			} else if (e.which == 122) {
    				$(results).trigger('next');
    			}
    		});


			$('.paginate #next').live('click', function() {
				$(results).trigger('next');
			});

			$('.paginate #previous').live('click', function() {
				$(results).trigger( 'prev');
			});

			$('.paginate #next-arrow').live('click', function() {
				var pages = $(".resultgroup").size();
				$(results).trigger( 'goto', pages);
			});
			$('.paginate #previous-arrow').live('click', function() {
				$(results).trigger( 'goto', 0);
			});

		}

		// Find orphan page
		if ($("#tweets").size()==1) {
		
			$("#tweets .utterance a").each(function () {
				// && eregi("http",$(this).text())
				if ($(this).text().length > 15) {
					$(this).text($(this).text().substr(0,15)+'...');
				}
			});

			//assign the scroller
			var utterings = $('#tweets');
			//check to see if the scrollTo plugin has been loaded
			if (typeof $(utterings).serialScroll == 'function') {

				$(utterings).serialScroll({
					items:'.utterance',
					duration: 1000,
					axis:'y',
					step: 1, 
					interval: 10000,
					easing: 'linear', 
					force: true, 
					cycle: true,
					lock: true,
					onBefore:function( e, elem, $pane, $items, pos ) {
						
					}, 
					onAfter:function( elem ) {
					}
	
				});
			
			}

		}


///////////////////////////////////////////////////////
// FAQ SCRIPTS


	//// FAQ response Management
		
		// Find categories content ont he page
		if ($("#faq").size() == 1) {
		
			if ($("#faqvisual").hasClass("map")) {
				$("#map").show();
				$("#faqImage").hide();
				loadScript();
			} else {
				$("#map").hide();
				$("#faqImage").show();
			}
		
			//click link to update question
			$("#faq #sections a, #faq #pager a").click(
				function () {
					$.SD.updateQuestion(this);
					return false; //disable link
				}
			);
			

		}


///////////////////////////////////////////////////////
// SEARCH SCRIPTS


	//// Search Results Management
		
		// Find search content
		if ($("#search").size() == 1) {
		
			//click link to update question
			if ($(".resultsall").size() == 0) {
				$("#searchresults h1").click(
					function () {
						if ($(this).hasClass('down')) {
							$(this).removeClass().addClass('up');
							$(this).next().slideUp();
							$(this).find('.resultsh1').css('background-image','url(/images/pages/search/h1-arrow-right.png)');
						} else {
							$(this).removeClass().addClass('down');
							$(this).next().slideDown();
							$(this).find('.resultsh1').css('background-image','url(/images/pages/search/h1-arrow-down.png)');
						}
					});
				}
			
			//handle below the fold content
			$('.searchresults .belowfold').hide();
			$('.toggleresults').show();
			$('.toggleresults').click(function () {
				var current = 0;
				$(this).parent().parent().find('.belowfold').each(function() {
					$(this).animate({opacity: 1.0},((500*current)), function () {
						$(this).fadeIn(300);
					});
					current++;
				});
				$(this).hide();
				$(this).siblings(".toggleresultsmore").show();
				return false;
			});

		    if ($(".resultsall").size() > 0) {
				if ($(".paginate").size() == 1) {

					//set the default resultset to view and 
					//the number of records per set
					var recordGroup = globals.searchResGroup;
					var recordSetNum = globals.resultSetNum;

					//clear the paginator and build page buttons
					$(".paginate").empty();
					var nPages = $("#resultsetOverall").text();
					var pageLinks = Math.round(nPages/recordSetNum);
					var linkCounter = 0;
					if (pageLinks > 1) {
						$(".paginate").append('<li id="previous">PREV</li>');
						while (linkCounter <= pageLinks) {
							if (linkCounter > 0) {
								$(".paginate").append('<li id="p'+linkCounter+'" class="page">'+linkCounter+'</li>');
							}
							linkCounter++;
						}
						$(".paginate").append('<li id="next">NEXT</li>');
						$(".paginate #p1").addClass('selected');
					}
					$(".paginate li.page, .paginate #previous, .paginate #next").click(function () {
						$.SD.paginatorLeafPages(this);
					});	

				}
				//hide everything bar resultset 0 on first load
				var rowCounter = 0;
				$(".searchresultitem").each(function () {
					rowCounter++;
					if (rowCounter > recordSetNum) {
						$(this).hide();
					}
				});

			}

		}


///////////////////////////////////////////////////////
// END READY

	});

