jQuery(document).ready(function() {
	if (typeof jCake.vars.homepageImages != 'undefined') {
		preloadHomepageImages();
		rotateHomepageImages();
	}
	
	setInterval('rotateRecentRecognitions();', 10000);
	
	jQuery('a.read-more-link').bind('click', readMore);
	
	var unclearableSelector = '.preview-search, #registry-search';
	jQuery('.preview-search').addClass('uncleared');
	if (jQuery('#registry-search').val() == '') {
		jQuery('#registry-search').addClass('uncleared');
		addDefaultTextForSearch();
	}
	jQuery(unclearableSelector).live(
		'click'
		, function() { 
			if (jQuery(this).hasClass('uncleared')) {
				jQuery(this).removeClass('uncleared');
				jQuery(this).val(''); 
			}
		}
	);

	jQuery('div#call-out-two').bind('click', function() { window.location = '/pages/product'; });
	jQuery('div#call-out-three').bind('click', function() { window.location = '/americasmosthonoredprofessionals'; });

	jQuery('div#home-body-center-right').bind('click', function() { window.location = '/pages/product'; });

	jQuery('div.registry-search-text :checkbox').live('click', changeOtherCheckboxes);
	
	jQuery('div.recent-recognitions div.box-text').live('click', loadRecentRecognitionPage);
	
	//var imagePreviewSelector = 'div.imagePreview img.preview, div.clickClose, p.clickEnlarge';
	//jQuery(imagePreviewSelector).live('click', enlargePreviewImage);

	jQuery('a.recognition-search').click(function() { jQuery('div.recognition-search-form').slideDown('slow'); });
	
	jQuery('#magnifyLinkEl').colorbox({ width:'1100', height: '1600' });
	
	jQuery().bind('cbox_complete', function() {
		jQuery('#enlargedImgEl').bind('click', function(){
			resizeEnlarged(); 
		 });
	});
	
});

resizeEnlarged = function(){
	
	currentEnlargedWidth = jQuery('#enlargedImgEl').attr('width');
	if(currentEnlargedWidth == 600){
		currentEnlargedWidth = 1000;
	} else {
		currentEnlargedWidth = 600;
	}
	jQuery('#enlargedImgEl').attr('width', currentEnlargedWidth);
}

function addDefaultTextForSearch() {
	jQuery('div.registry-search-text :checkbox').each(
		function() {
			if (jQuery('input#' + this.id).attr('checked')) {
				var parts = (this.id).split('__');
				switch (parts[0]) {
					case 'SearchName':
						jQuery('#registry-search').val('Enter Company Name or First and Last Name');
						break;
					case 'SearchRecognition':
						jQuery('#registry-search').val('Enter the Recognition Here');
						break;
				}
			}
		}
	);
}

function enlargePreviewImage() {
	var bigWidth = 625;
	var previewImageSelector = 'div.imagePreview img.preview';
	var currentWidth = jQuery(previewImageSelector).attr('width');
	if (currentWidth != bigWidth) {
		jCake.vars.contractWidth = jQuery(previewImageSelector).attr('width');
		jQuery(previewImageSelector).attr('width', bigWidth);
		jQuery('.clickClose').show();
	} else {
		jQuery('.clickClose').hide();
		jQuery(previewImageSelector).attr('width', jCake.vars.contractWidth);
	}
}

function loadRecentRecognitionPage() {
	var parts = (this.id).split('__');
	var id = parts[1];
	
	window.location = '/search/load_recent_recognition/' + id;
}

function changeOtherCheckboxes() {
	var self = this;
	var parts = (self.id).split('__');
	var randomVal = parts[1];
	var thisId;
	
	jQuery('div.registry-search-text :checkbox').each(function() {
		thisId = this.id;
		if (self.id != thisId && thisId.indexOf(randomVal) != -1) {
			jQuery('input#' + this.id).attr('checked', false);
		}
	});
	
	if (jQuery('#registry-search').hasClass('uncleared')) {
		addDefaultTextForSearch();
	}
}

function rotateRecentRecognitions() {
	var recentDiv;
	jQuery('.recent-recognitions').each(function() {
		recentDiv = this;
		jQuery.get(
			'/special_sections/recent'
			, {}
			, function(result) {
				jQuery(recentDiv).fadeOut(
					'slow'
					, function() {
						jQuery(recentDiv).html(result);
						jQuery(recentDiv).fadeIn('slow');
					}
				);
			}
		);
	});
}

function preloadHomepageImages() {
	var img = new Array();
	jQuery(jCake.vars.homepageImages).each(function() {
		img[this] = document.createElement('img');
		imageUrl = jCake.vars.homepageImagesDir + '/' + this;
		img[this].src =  imageUrl;
	});
}	

function rotateHomepageImages() {
	var imageArray = new Array;
	jQuery(jCake.vars.homepageImages).each(function() {
		imageUrl = jCake.vars.homepageImagesDir + '/' + this;
		imageArray[imageArray.length] = {src : imageUrl};
	});
	
	jQuery('div#home-body-center-right').crossSlide(
		{ sleep: 5, fade: 1 }
		, imageArray
		
	);
}

function readMore() {
	jQuery(this).hide();
	var position = false;
	jQuery(this).parents().each(function() {
		if (jQuery(this).hasClass('read-more-container')) {
			jQuery(this).css('overflow', 'auto');
			jQuery('.read-more-text', this).each(function() {
				jQuery(this).fadeIn('slow');
				if (!position) {
					position = jQuery(this).position();
				}
			});
			
			if (position) {
				jQuery(this).scrollTop(position.top);
			}
		}
	});
}

function loadRecognition() {
	var ssId = jCake.vars.ssId;
	loadRecognitionBySpecialSection(ssId);
}

function loadRecognitionBySpecialSection(ssId, page) {
	var useId = jCake.vars.useId;
	var recognitionType = jCake.vars.recognitionType;
	
	jQuery.get(
		'/customer_interface/' + recognitionType + '_recognition/' + ssId + '/' + useId + '/' + page
		, {}
		, function(result) {
			if (result.length > 0) {
				jQuery('#recognition-loading-spinner').fadeIn('slow');
				jQuery('#recognition-loading').html(result);
				jQuery('#recognition-loading img.preview').load(function() {
					jQuery('#recognition-loading-spinner').fadeOut('slow', function() {
						jQuery('#recognition-loading').slideDown('slow');
					});
				});
				jQuery('#magnifyLinkEl').colorbox({ width:'1100', height: '1600' });
			}
		}
	);
}

function loadRecognitionClick() {
	jQuery('#recognition-loading').slideUp('slow', function() {
		jQuery('#recognition-loading-spinner').fadeIn('slow');
	});
	
	jQuery('a.recognition-load-link').removeClass('active');
	jQuery(this).addClass('active');
	
	var parts = this.id;
	parts = parts.split('__');
	var ssId = parts[1];
	var page = parts[2];
	loadRecognitionBySpecialSection(ssId, page);
	return false;
}

