$(document).ready(function(){
	$('.tabs a').click(function(){
		activateTabContent(this);
		return false;
	});
	
	$('.expandlinks li a').click(function() {
		if($(this).hasClass("reallink")){
			return true;
		}
		if( $(this).nextAll('.linkcontent').is(":visible") == true ){
			$(this).nextAll('.linkcontent').hide();
		}
		else{
			$(this).nextAll('.linkcontent').show();
		}
		return false;
	});
	
	$("a").filter(function() {
    	return this.hostname && this.hostname !== location.hostname;
	}).attr('target', '_blank').attr("title", "Opens in a new window");;
	
});

var nestedShop = false;
var nestedPerformance = false;
var searchIsOpen = false;

$(function() {
	enableSearch();
	customTooltip();
	$('#mainnav').find('.performance').mouseover(function() {
		var $div = $('#performance-nested');
		var height = $div.height() - 14;
		$div.bind('mousemove', function(e) {
			var offset = $div.offset();
			var x = e.pageX - offset.left;
			var y = e.pageY - offset.top;
			if ((x < 14 || y < 11) || (x > 155 && y < 48) || (x > 247) || (y > height)) {
				$div.unbind('mousemove').hide();
				nestedPerformance = false;
			}
		});

		if (!nestedPerformance) {
			$('#performance-nested').show();
			nestedPerformance = true;
		}
	});
	$('#mainnav').find('.shop').mouseover(function() {
		var $div = $('#shop-nested');
		var height = $div.height() - 14;
		$div.bind('mousemove', function(e) {
			var $this = $(this);
			var offset = $this.offset();
			var x = e.pageX - offset.left;
			var y = e.pageY - offset.top;
			if ((x < 14 || y < 11) || (x > 103 && y < 48) || (x > 247) || (y > height)) {
				$div.unbind('mousemove').hide();
				nestedShop = false;
			}
		});
		if (!nestedShop) {
			$('#shop-nested').show();
			nestedShop = true;
		}
	});

	if (navigator.userAgent.indexOf('MSIE') !=-1){
		$('A[rel="widthchart"]').popupWindow({
				height:790,
				width:880,
				top:50,
				left:50,
				centerBrowser:1,
				menubar:0,
				location:0
		});
		$('A[rel="fitfinder"]').popupWindow({
				height:480,
				width:920,
				top:50,
				left:50,
				centerBrowser:1,
				menubar:0,
				location:0
		});
	}else{
		$('A[rel="widthchart"]').popupWindow({
				height:690,
				width:880,
				top:50,
				left:50,
				centerBrowser:1,
				menubar:0,
				location:0
		});
		$('A[rel="fitfinder"]').popupWindow({
				height:480,
				width:920,
				top:50,
				left:50,
				centerBrowser:1,
				menubar:0,
				location:0
		});
	}


	$('#sharebtn').click(function() {
		$.post('?action=shareContent&a=1&ac=1', { pageID: PAGE_URL, pageTitle: PAGE_TITLE }, function(data) {
			if (data != 'error') {
				var sAjax=s_gi(s_account);
				sAjax.events='event24';
				sAjax.prop19=PAGE_TITLE;
				sAjax.linkTrackVars='events,prop19';
				sAjax.linkTrackEvents='event24';
				sAjax.tl(this,'o','Share Content');
				updateActivity(data);
			}
		});
		return true;
	});

	$('.inlinesharebtn').click(function() {
		$.post('?action=shareContent&a=1&ac=1', { pageID: PAGE_URL, pageTitle: PAGE_TITLE }, function(data) {
			if (data != 'error') {
				var sAjax=s_gi(s_account);
				sAjax.events='event24';
				sAjax.prop19=PAGE_TITLE;
				sAjax.linkTrackVars='events,prop19';
				sAjax.linkTrackEvents='event24';
				sAjax.tl(this,'o','Inline Share Content');
				updateActivity(data);
			}
		});
		showSharing(this);
		return false;
	});
	
});

function showSharing(parentItem){
	$tooltip = $('#tooltip');
	var id = '#tt_sharing';
	$('#tooltip .content').html($(id).html());
	var h = $tooltip.height();
	var w = $tooltip.width();
	var offset = $(parentItem).offset();
	$('#tooltip').css({ top: (offset.top - h) + 'px', left: ((offset.left - (w / 2))+75) + 'px'}).fadeIn('fast');
	$('#tooltip .closeTool').click(function() {
		$('#tooltip').fadeOut('fast');return false;
	});
	
}

function customTooltip() {
	$('body').append('<div id="tooltip"><div class="top"></div><div class="content"></div><div class="bottom"></div></div>');
	$('.tooltip').mouseover(function(e) {
		$tooltip = $('#tooltip');
		var id = '#tt_' + $(this).attr('rel');
		// position tooltip on element
		$('#tooltip .content').html($(id).html());
		var h = $tooltip.height();
		var w = $tooltip.width();
		var offset = $(this).offset();
		$('#tooltip').css({ top: (offset.top - h) + 'px', left: (offset.left - (w / 2)) + 'px'}).fadeIn('fast');
	}).mouseout(function() {
		$('#tooltip').fadeOut('fast');
	});
}

function enableSearch() {
	$('#searchbox').focus(function() {
		$(this).addClass('focus');
		if (this.value == this.defaultValue){
			this.value = '';
		}else{
			hideSearchResults();
		}
	});
	$('#submit-search').click(function() {
		var terms = $('#searchbox').val();
		if (terms.length >= 3) {
			searchForTerm(terms);
		}
		return false;
	});
	$('#cancel-search').click(function() {
		hideSearchResults();
		return false;
	});
	$('#searchbox').keypress(function(e) {
        if (e.which == 13) {
			var terms = $('#searchbox').val();
			if (terms.length >= 3) {
				searchForTerm(terms);
			}
		}
		return true;
	});
	$('#searchbox').keydown(function(e) {
		hideSearchResults();
		return true;
	});
}

searchTerm = "";
originalLink1 = $('#site-search').attr('href');
originalLink2 = $('#all-results').attr('href');
function searchForTerm(terms) {
	if(terms == "Search"){
	}else{
		searchTerm = terms;
		$('#submit-search').hide();
		$('#cancel-search').hide();
		$('#status-search').show();
		$.post(DATA_SERVER+'ajax/search.php', {keywords: terms}, onSearchComplete, 'json');
	}
}
function onSearchComplete(data) {
	var count = 0;
	var output = [];
	if (data.products != undefined) {
		var x = data.products.length;
		if (x > 0) {
			for (i = 0; i < x; i++) {
				output.push('<a href="' + data.products[i].url + '">');
				output.push('<span class="title">' + data.products[i].style + '</span>');
				output.push('<span class="summary">' + data.products[i].summary + '</span>');
				output.push('<span class="shoe"><img src="' + data.products[i].thumburl + '" /></span>');
				output.push('<span class="more-colors">more colors</span>');
				if (i != (x - 1)) output.push('<span class="divider"></span>');
				output.push('</a>');
				count++;
			}
			$('#product-results').html(output.join('')).show();
			output = [];
		} else {
			$('#product-results').hide();
		}
	} else {
		$('#product-results').hide();
	}

	// see if any other items exist
	if (data.other != undefined) {
		var x = data.other.length;
		if (x > 0) {
			for (i = 0; i < x; i++) {
				output.push('<a href="' + data.other[i].url + '">');
				output.push('<span class="link">' + data.other[i].title + '</span>');
				if (i != (x - 1)) output.push('<span class="divider"></span>');
				output.push('</a>');
				count++;
			}
			$('#other-results').html(output.join('')).show();
		} else {
			$('#other-results').hide();
		}
	} else {
		$('#other-results').hide();
	}


	var sAjax=s_gi(s_account);
	sAjax.events='event20';
	sAjax.prop9=searchTerm;
	if(count == 0){
		sAjax.prop10="Zero";
	}else{
		sAjax.prop10=count;
	}
	sAjax.eVar3='spotlight search';
	sAjax.linkTrackVars='events,eVar3,prop9,prop10';
	sAjax.linkTrackEvents='event20';
	sAjax.tl(this,'o','Spotlight Search');

	if (count == 0) {
		$('#product-results').hide();
		$('#other-results').hide();
		$('#all-results').hide();
		$('#no-shortcut').show();
		$('#site-search').css('display', 'block');
		$('#site-search').attr('href', originalLink1+searchTerm);
		output.push();
		output.push();
	} else {
		$('#no-shortcut').hide();
		$('#all-results').css('display', 'block');
		$('#all-results').attr('href', originalLink2+searchTerm);
		$('#site-search').hide();
	}
	showSearchbox();
    return false;
}

function showSearchbox() {
	if (!searchIsOpen) {
		$('#search-results').fadeIn('fast', function() {
			$('#status-search').hide();
			$('#submit-search').hide();
			$('#cancel-search').show();
			searchIsOpen = true;
		});
	}else{
		$('#status-search').hide();
		$('#cancel-search').show();
	}
}

/**
 * Binds a click event
 */
function hideSearchResults() {
	if (searchIsOpen) {
		$('#search-results').fadeOut('fast', function() {
			$('#status-search').hide();
			$('#cancel-search').hide();
			$('#submit-search').show();
			$('#searchbox').removeClass('focus');
			var sb = document.getElementById('searchbox');
			//sb.value = sb.defaultValue;
			searchIsOpen = false;
		});
	}
}

function optionsAction(element) {
	alert(element.id);
}



(function($){ 		  
	$.fn.popupWindow = function(instanceSettings){
		
		return this.each(function(){
		
		$(this).click(function(){
		
		$.fn.popupWindow.defaultSettings = {
			centerBrowser:0, // center window over browser window? {1 (YES) or 0 (NO)}. overrides top and left
			centerScreen:0, // center window over entire screen? {1 (YES) or 0 (NO)}. overrides top and left
			height:500, // sets the height in pixels of the window.
			left:0, // left position when the window appears.
			location:0, // determines whether the address bar is displayed {1 (YES) or 0 (NO)}.
			menubar:0, // determines whether the menu bar is displayed {1 (YES) or 0 (NO)}.
			resizable:0, // whether the window can be resized {1 (YES) or 0 (NO)}. Can also be overloaded using resizable.
			scrollbars:0, // determines whether scrollbars appear on the window {1 (YES) or 0 (NO)}.
			status:0, // whether a status line appears at the bottom of the window {1 (YES) or 0 (NO)}.
			width:500, // sets the width in pixels of the window.
			windowName:null, // name of window set from the name attribute of the element that invokes the click
			windowURL:null, // url used for the popup
			top:0, // top position when the window appears.
			toolbar:0 // determines whether a toolbar (includes the forward and back buttons) is displayed {1 (YES) or 0 (NO)}.
		};
		
		settings = $.extend({}, $.fn.popupWindow.defaultSettings, instanceSettings || {});
		
		var windowFeatures =    'height=' + settings.height +
								',width=' + settings.width +
								',toolbar=' + settings.toolbar +
								',scrollbars=' + settings.scrollbars +
								',status=' + settings.status + 
								',resizable=' + settings.resizable +
								',location=' + settings.location +
								',menuBar=' + settings.menubar;

				settings.windowName = this.name || settings.windowName;
				settings.windowURL = this.href || settings.windowURL;
				var centeredY,centeredX;
			
				if(settings.centerBrowser){
						
					if ($.browser.msie) {//hacked together for IE browsers
						centeredY = (window.screenTop - 120) + ((((document.documentElement.clientHeight + 120)/2) - (settings.height/2)));
						centeredX = window.screenLeft + ((((document.body.offsetWidth + 20)/2) - (settings.width/2)));
					}else{
						centeredY = window.screenY + (((window.outerHeight/2) - (settings.height/2)));
						centeredX = window.screenX + (((window.outerWidth/2) - (settings.width/2)));
					}
					window.open(settings.windowURL, settings.windowName, windowFeatures+',left=' + centeredX +',top=' + centeredY).focus();
				}else if(settings.centerScreen){
					centeredY = (screen.height - settings.height)/2;
					centeredX = (screen.width - settings.width)/2;
					window.open(settings.windowURL, settings.windowName, windowFeatures+',left=' + centeredX +',top=' + centeredY).focus();
				}else{
					window.open(settings.windowURL, settings.windowName, windowFeatures+',left=' + settings.left +',top=' + settings.top).focus();	
				}
				return false;
			});
			
		});	
	};
})(jQuery);

