
$j = jQuery.noConflict();

//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize(){
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if (yScroll < windowHeight) {
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if (xScroll < windowWidth) {	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

function show_overlay() {
	var size = getPageSize();
	$j('#overlay').css('height', size[1]).show();
}

function toggle_more_options() {
	if ($j('#search_section > input').val() != '') {
		var section = '#'+ $j('#search_section > input').val() +'_options';
		$j(section).toggle();
		if ($j(section).css('display') == 'block') { 
			$j('#search_top .more').text($j(section +' .hide_options').text());
		}
		else {
			$j('#search_top .more').text($j('#toggle_lang').val());
		}
		$j('#search_container .search_options:not('+section+', #club_search)').hide();
	}
}

function set_search_options(value) {
	var form_action = '';
	$j('#search_top').show();
	$j('#search_top .searchbox').show();
	$j('#search_top .clubs_inputs').hide();
	$j('#search_top').removeClass('club_list');

	if (value == 'clubs') {
		$j('#search_top').addClass('club_list');
		$j('#search_top .searchbox').hide();
		$j('#search_top .clubs_inputs').show();
		if ($j('#search_url_'+value+'_form_action').length) form_action = $j('#search_url_'+value+'_form_action').val();
		if ($j('#club_search_more').val() == 1) $j('#search_section').nextAll('.more').show();
		else $j('#search_section').nextAll('.more').hide();
	}
	else if (value == 'store') {
		if ($j('#store_search_more').val() == 1) $j('#search_section').nextAll('.more').hide();
	}
	else if (value != ''){
		$j('#search_section').nextAll('.more').show();
		if ($j('#search_url_'+value+'_form_action').length) form_action = $j('#search_url_'+value+'_form_action').val();
	}
	else $j('#search_section').nextAll('.more').hide();
		
	$j('#site_search_form').attr('action', form_action);
}

function set_search_text() {
	$j('#search_section li input').each(function () {
		if ($j(this).val() == $j('#search_section > input').val()) {
			$j('#search_section .select_button').text($j(this).parent().text());
		}
	});
}

/* set up the search forms to submit to the proper section location.
*/
function site_search_submit() {	
	if ($j('#search_section > input').val() != '') {
		var section = $j('#search_section > input').val();
		
		if ($j('#search_top .'+section+'_inputs').length) {
			$j('#search_top .'+section+'_inputs :input').each(function () {
				if ($j(this).attr('name') && $j(this).val()) {
					if (($j(this).attr('type') == 'radio' || $j(this).attr('type') == 'checkbox') && $j(this).attr('checked') == false) section = section;
					else $j('#site_search_form').append('<input type="hidden" name="'+$j(this).attr('name')+'" value="'+$j(this).val()+'"/>');
				}
			});
		}
		else if (section == 'store') {
			$j('#site_search_form').append('<input id="keywords" name="substring" type="hidden" value=""/><input type="hidden" name="mode" value="search" />');
			$j('#site_search_form #keywords').val($j('#search_box').val());
		}
		else {
			$j('#site_search_form').append('<input id="keywords" name="keywords" type="hidden" value=""/>');
			$j('#site_search_form #keywords').val($j('#search_box').val());
		}
		section = '#'+ $j('#search_section > input').val() +'_options';
		if ($j(section).length) {
			$j(section +' :input').each(function () {
				if ($j(this).attr('name') && $j(this).val()) {
					if (($j(this).attr('type') == 'radio' || $j(this).attr('type') == 'checkbox') && $j(this).attr('checked') == false) section = section;
					else $j('#site_search_form').append('<input type="hidden" name="'+$j(this).attr('name')+'" value="'+$j(this).val()+'"/>');
				}
			});
		}
		$j('#site_search_form').submit();
	}
	else {
		$j('#cse-search-box').append('<input id="q" name="q" type="hidden" value=""/>');
		$j('#cse-search-box #q').val($j('#search_box').val());
		$j('#cse-search-box').submit();
	} 
}

function homepage_cta_next_item(anchor) {
	$j(anchor).parents('ul').find('a').removeClass('active');
	$j(anchor).blur().addClass('active');
	var index = $j(anchor).parent().prevAll().length;
	$j('#homepage_content img').attr('src', images[index]).attr('alt', $j(anchor).text());
	$j('#homepage_content a').attr('href', links[index]);
	$j('#homepage_content h3 > span').html(captions[index]);
}

function homepage_cta_rotate() {
	$j('#homepage_cta ul').oneTime(4000, function () {
		var total = $j(this).find('li').length;
		var index = $j(this).find('a.active').parent().prevAll().length;
		var next = (total == index + 1) ? 0 : index + 1;		
		homepage_cta_next_item($j(this).find('li').eq(next).find('a').eq(0));
		homepage_cta_rotate();
	});
}

function toggle_image(anchor, element_id, show) {
	if (show == 1) {
		size = getPageSize()
		$j('#overlay').css('height', size[1]);
		// show image and overlay
		var pos = $j(anchor).parent().position();
		$j('#'+ element_id).parent().css({
			left: pos.left + 35,
			top: pos.top - 30
		});
		$j('#'+ element_id).siblings().hide().end().show().parent().addClass('modal').show();
		$j('#overlay').show();
	}
	else {
		$j('#overlay').hide();
		$j('#'+ element_id).siblings().hide().end().hide().parent().removeClass('modal').hide();
	}
}

function toggle_search_options(section) {
	$j('#'+section+'_options').toggle();
	if ($j('#'+section+'_options').css('display') == 'block') { 
		$j('#'+section+' .more').text($j('#'+section+'_options .hide_options').text());
	}
	else {
		$j('#'+section+' .more').text($j('#toggle_lang').val());
	}
}

$j(function () {
	$j('#overlay').click(function (){
		$j(this).hide();
		$j('.modal').removeClass('modal').hide();
	});
	
	// fake select list functions
	var blur = '';
	$j('#lang_value, div.select_button').parent().click(function () {
		if ($j(this).children('.select_options').css('display') == 'none' && blur != 'select_button') {
			// make the currently selected item highlighted
			var options = $j(this).children('.select_options');
			var text = $j(options).siblings('div:first').text();
			$j(options).find('li').each(function () {
				if ($j(this).text() == text) $j(this).addClass('selected');
				else $j(this).removeClass('selected');
			});
			$j(this).children('.select_options').show().attr('tabindex', 200).focus();
		}
		blur = '';
	}).mousedown(function (event) {
		if ($j(this).children('.select_options').css('display') != 'none') blur = 'select_button';
	});
	$j('div.select_options').blur(function () {
		$j(this).hide();
	})
	$j('.select_options ul li').mouseover(function () {
		$j(this).siblings().removeClass('selected');
		$j(this).addClass('selected');
	}).click(function () {
		blur = 'select_button';
		var value = $j(this).find('input').val();
		if (value.indexOf('http') == 0) {
			document.location.href = value;
			return;
		}
		$j(this).parents('div.select_options').blur();
		var button_text = $j(this).parents('div.select_options').siblings('div:first');
		var current_value = button_text.nextAll('input').val();
		button_text.text($j(this).text());
		var className = $j(this).attr('class');
		if (className != '' && className != 'selected') button_text.attr('class', className);
		button_text.nextAll('input').val(value);
		
		var list_parent = $j(this).parents('.select_options').parent();
		if (list_parent.attr('id') == 'search_section') {
			set_search_options(value);
			if (current_value != value) {
				$j('#search_container .search_options:not(#club_search)').hide();
				$j('#search_top .more').text($j('#toggle_lang').val());
			}
		}
	});
	
	$j('#footer_cont #newsletter_name').click(function () {
		if ($j(this).val() == $j(this).siblings('label').text()) $j(this).select();
			
	});
	$j('#footer_cont #newsletter_email').click(function () {
		if ($j(this).val() == $j(this).siblings('label').text()) $j(this).select();
	});
	

	set_search_options($j('#search_section > input').val());
	set_search_text();
	$j('#search_box').keypress(function (e) {
		if (e.keyCode == '13') site_search_submit();	   
	});
	if ($j('#events_options .padding .date').length) $j('#events_options .padding .date').datepick();

	// hide/show normal search option 'more options'
	$j('#site_search .more, #site_search .search_options .hide_options').click(function (e) {
		e.preventDefault();
		toggle_more_options();
	});

	// hide the club search more options
	$j('#club_search .more, #club_search .hide_options').click(function (e) {
		e.preventDefault();
		toggle_search_options('club_search');
	});

	// hide the club search more options
	$j('#product_review_search .more, #product_review_search .hide_options').click(function (e) {
		e.preventDefault();
		toggle_search_options('product_review_search');
	});
	
	// show/hide exercise options in the search
	$j('#exercises_options .padding > div > a').click(function () {
		$j(this).nextAll('.option:first').toggle().end().find('span').text(($j(this).find('span').text() == '-' ? '+' : '-'));
	});

	// show/hide primary nav children
	$j('#nav_container li').hover(function () {
		$j(this).children('ul.menu, ul.secondary, ul.sub_menu, div').show();
		var padding = parseInt($j(this).parent().css('padding-right'));
		$j(this).children('ul.sub_menu').css('left', $j(this).outerWidth() + padding);
	}, function () {
		$j(this).children('ul.menu, ul.secondary, ul.sub_menu, div').hide();
	});

	
	// hide/show nutrition info
	$j('.recipe_results .header .b_green').click(function (e) {
		e.preventDefault();
		var pos = $j(this).position();
		$j(this).nextAll('.nutrition_info').toggle().css('left', pos.left + $j(this).outerWidth() + 5);
	});
	$j('.recipe_results .nutrition_info a').click(function (e) {
		e.preventDefault();
		$j(this).parents('.nutrition_info').hide();
	});
	
	$j('#homepage_cta li a').click(function () {
		homepage_cta_next_item(this);
		$j('#homepage_cta ul').stopTime();
	});	
	if ($j('#homepage_cta ul li').length) homepage_cta_rotate();	

	$j('.tools .share_button').hover(function() {
		var share_cont = $j(this).find('.share_cont');
		var top = $j(this).position().top;
		var left = $j(this).position().left;
		left -= share_cont.outerWidth() - $j(this).outerWidth() + 10;
		share_cont.show();
		$j(this).addClass('share_button_hover');
		if (top < 500) {
			top -= share_cont.outerHeight();
			share_cont.css({'top': top + 1, 'left': left});
		}
		else {
			top += $j(this).outerHeight();
			share_cont.css({'top': top - 1, 'left': left});
		}
	}, function() {
		$j(this).find('.share_cont').hide();
		$j(this).removeClass('share_button_hover');
	});
	$j('#clubs_options .options a').click(function() {
		$j(this).next().toggle();
		if ($j(this).find('span').html() == '+') {
			$j(this).find('span').html('-');
		}
		else {
			$j(this).find('span').html('+');
		}
	});
	
	$j('#exercise_search_toggle').click(function () {
		$j('#exercise_search_toggle').toggle();
		$j('#exercise_search').toggle();
	});
	$j('#exercise_search h2').click(function () {
		$j('#exercise_search_toggle').toggle();
		$j('#exercise_search').toggle();
	});
	
	if ($j('#news_ip_address').length && $j('#footer_ip').length) {
		$j('#news_ip_address').val($j('#footer_ip').val());
	}
});
