
(function($){
// Watermarks
	watermark(jQuery('#SearchForm_SearchForm_Search'), 'Search KCSE.com');
// CUFON
	Cufon.replace('h1, .navLevel1, .arrowLink, .outlineLink, #footer p, #slider span, #closeCaseStudy, .bell, #nav h2, #hLeft h2, #hLeft h3, #sidebar h2, .revealHeader h2, #sidebarProfilesList li a span');
	 
// Navigation Dropdown
	function showNav(){
		$(this).children('a').not('#navHome, .level2 a').addClass('selected').siblings('ul').fadeIn();
	}
	function hideNav(){
		$(this).children('a').removeClass('selected').siblings('ul').fadeOut();
	}
	var navConfig = {
		over: showNav,
		timeout: 500,
		out: hideNav
	};
	$('#nav li').hoverIntent(navConfig);
	
// Homepage
	// Homepage Slider
	$('#slider').cycle({
		next: '#sliderNext',
		prev: '#sliderPrev',
		before: function(){$(this).parent().find('li.current').removeClass();},
		after: function(){
				$(this).addClass('current');
				var currentSlide = $(this).index();
				$('#sliderCurrent').html(currentSlide+1);
			}
	});
	
	// Homepage Videos
	$('#hVideo ul a').live('click', function(){
		currentYouTubeID = $('#hVideo embed').attr('title');
		newImage = 'http://i1.ytimg.com/vi/' + currentYouTubeID + '/default.jpg'
		youTubeID = $(this).attr('rel');
		embedCode = '<object width="246" height="180"><param name="movie" value="http://www.youtube.com/v/' + youTubeID + '?version=3&amp;hl=en_US&amp;rel=0&amp;hd=1&amp;autohide=1&amp;modestbranding=1&amp;controls=0"><param name="wmode" value="transparent"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/' + youTubeID + '?version=3&amp;hl=en_US&amp;rel=0&amp;hd=1&amp;autohide=1&amp;modestbranding=1&amp;controls=0" type="application/x-shockwave-flash" width="246" height="180" wmode="transparent" allowscriptaccess="always" allowfullscreen="true"></embed></object>';
		$('#firstVideo').html(embedCode);
		$(this).children().attr('src', newImage).attr('title', currentYouTubeID).parent().attr('rel', currentYouTubeID);
		$('#hVideo embed').attr('title', youTubeID);
		return false;
	});
	
	$('#hVideo ul a.arrowLink').click(function(){
		window.location.href = $(this).attr('href');
	});

// Case Studies
	// Case study hover highlight and reveal
	$('#caseStudiesList').find('.caseStudy').click(function(){
		$('#closeCaseStudy').show();
		$(this).addClass('caseStudySelected').siblings('.caseStudy').hide();
	}).hover(
		function(){
			$(this).addClass('caseStudyHover').css({'cursor':'pointer'});
		}, function(){
			$(this).removeClass('caseStudyHover').css({'cursor':'default'});
		}
	);
	
	// Case study hide
	$('#closeCaseStudy').click(function(){
		$('#caseStudiesList').find('.caseStudySelected').removeClass('caseStudySelected').siblings().show();
		$(this).hide();
		return false;
	});
	
	// Case study images select
	$('.caseStudyImages a').live('click', function(){
		src = $(this).children('img').attr('src');
		href = $(this).attr('href');
		largesrc = $(this).children('img').attr('title');
		rel = $(this).children('img').attr('rel');
		name = $(this).children('span').html();
		$(this).parent().addClass('selected').siblings().removeClass('selected');
		$(this).parent().parent().parent().children('.currentCaseStudyImage').children('a').attr({href: largesrc, rel: rel}).children('img').attr('src', src);
		$(this).parent().parent().parent().children('.currentCaseStudyImage').children('span').html(name);
		$('.caseStudyImages a').removeClass('selected');
		
		return false;
	});
	
	// Gallery popup
	$('.currentCaseStudyImage a').colorbox().each(function(){
		$(this).attr('rel', $(this).parents('.currentCaseStudyImage').attr('rel'));
		//alert($(this).parents('.currentCaseStudyImage').attr('rel'));
	});
	
// Service Sidebar Dropdown
	$('#serviceChooserTrigger').click(function(){
		$('#moduleServiceChooser ul').toggle();
		return false;
	});
	
// Content Reveal
	$('.revealTrigger').click(function(){
		$(this).parent().parent().toggleClass('selected').find('.revealContent').toggle();
		$(this).parent().parent().siblings().removeClass('selected').find('.revealContent').hide();
		return false;
	});
	
// Team Members Cluetip
	$('.cluetip').cluetip({
		splitTitle: '|',
		width: 370,
		activation: 'click',
		sticky: true,
		onHide: function(ct, ci){
			$('#teamMembers a.selected').removeClass('selected');
		}
	}).click(function(){
		$(this).addClass('selected');
	});

// Languages popup	
	$('#languages a').cluetip({
		local: true,
		width: 540,
		activation: 'click',
		sticky: true,
		cluezIndex: 200,
		cluetipClass: 'language'
	});

// Switch Videos
	$('.video').click(function(){
		title = $(this).find('strong').html();
		description = $(this).find('.videoHidden p').html();
		youTubeID = $(this).attr('rel');
		embedCode = '<object width="553" height="310"><param name="movie" value="http://www.youtube.com/v/' + youTubeID + '?version=3&amp;hl=en_US&amp;rel=0&amp;hd=1&amp;autohide=1&amp;modestbranding=1&amp;controls=0"><param name="wmode" value="transparent"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/' + youTubeID + '?version=3&amp;hl=en_US&amp;rel=0&amp;hd=1&amp;autohide=1&amp;modestbranding=1&amp;controls=0" type="application/x-shockwave-flash" width="553" height="310" wmode="transparent" allowscriptaccess="always" allowfullscreen="true"></embed></object>';
		$('#videoTitle strong').html(title);
		$('#videoDescription').html(description);
		$('#videoEmbed').html(embedCode);
		
		$(this).addClass('videoSelected').parent().siblings().find('.video').removeClass('videoSelected');
		return false;
	});	
})(this.jQuery);

function watermark(obj, text) {
    // set the watermark text
    jQuery(obj).val(text);
    
    // clear on focus
    jQuery(obj).focus(function() {
        if (jQuery(obj).val() == text) {
            jQuery(obj).val('');
        }
    });
    
    // restore on blur
    jQuery(obj).blur(function() {
        if (jQuery(obj).val() == '') {
            jQuery(obj).val(text);
        }
    });
}
