(function($) {
	var interval = null;
	var checklist = [];

	$.elementReady = function(id, fn) {
		checklist.push({id: id, fn: fn});
		if (!interval) {interval = setInterval(check, $.elementReady.interval_ms);}
		return this;
	};
	
	// Plugin settings
	$.elementReady.interval_ms = 23; // polling interval in ms
	
	// Private function
	function check() {
		var docReady = $.isReady; // check doc ready first; thus ensure that check is made at least once _after_ doc is ready
		for (var i = checklist.length - 1; 0 <= i; --i) {
			var el = document.getElementById(checklist[i].id);
			if (el) {
				var fn = checklist[i].fn; // first remove from checklist, then call function
				checklist[i] = checklist[checklist.length - 1];
				checklist.pop();
				fn.apply(el, [$]);
			}
		}
		if (docReady) {
			clearInterval(interval);
			interval = null;
		}
	}
})(jQuery);
function crop(img_id, crop_id, x, y, width, height, oWidth, oHeight, url, alt) { // oWidth, and oHeight refer to original (JSON supplied) width and height. Refer to function call on how to use.
	if(isIE){var cimg = '<a href="'+url+'"><img id="' + crop_id + '_img" src="' + img_id + '" alt="' + alt + '" style="display:none" /></a>';}
	else {var cimg = '<img id="' + crop_id + '_img" src="' + img_id + '" alt="' + alt + '" style="display:none" />';}
	
	jQuery("#"+crop_id).html(cimg);
	
	var scale_x = 80 / width;
	var scale_y = 80 / height;
	
	jQuery("#"+crop_id).css({"position": "relative","overflow": "hidden"});
	jQuery("#"+ crop_id + '_img').css({"position": "absolute","display": "block","left": (-x * scale_x) + "px", "top": (-y * scale_y) + "px","width": (oWidth * scale_x) + "px","height": (oHeight * scale_y) + "px"});
}
var isMacFF = function () {
	if(navigator.userAgent.indexOf('Mozilla') > -1 && navigator.userAgent.indexOf('Macintosh') > -1){return true;}
	return false;
};
var isMacSafari = function () {
	if(navigator.userAgent.indexOf('Safari') > -1 && navigator.userAgent.indexOf('Macintosh') > -1){return true;}
	return false;
};
var isSafari = function () {
	if(navigator.userAgent.indexOf('Safari') > -1){return true;}
	return false;
};
var isIE6 = function () {
	if(navigator.userAgent.indexOf('MSIE 6') > -1){return true;}
	return false;
};
var isIE = function () {
	if(navigator.userAgent.indexOf('MSIE') > -1){return true;}
	return false;
};

/**
* Item html creation helper.
*/
function mycarousel_getItemHTML(item, addtitle, int) {
	var sectionTitle = (addtitle) ? '<h3>' + it_carousel_title_single + '</h3>' : '';
	return '<a href="' + item.link + '" title="' + item.img.iAlt + '"><img src="' + item.img.ipath + '" width="78" height="67" border="0" alt="' + item.img.iAlt + '" style="text-decoration:none" />' + sectionTitle +'<span>' + item.title + '</span></a>';
}
function gallerycarousel_getItemHTML(item, i, len) {	
	var highlighted = pageNum - 1;
	highlighted = (highlighted == i) ? "border-color:#D80101" : "";
	var displayCounter = (highlighted !== "") ? "showCounter" : "";
	jQuery('span.currentSlide').html(pageNum + ' of ' + len);
	return '<a href="' + item.link + '" title="' + item.img.iAlt + '"><div class="croppedIMG" id="crop' + (i+1) + '" style="width:80px; height:80px; margin-bottom:5px;'+highlighted+'"></div><span class="'+displayCounter+'">'+ (i+1) +' of '+len+'</span></a>';
	
}
function mycarousel_itemAddCallback(carousel, first, last, data, addTitle, isGallery) {
	var results, cdiv;
	if(isGallery){
		results = data.photogalleries;
		for (i = 0; i < results.length; i++) {
			carousel.add(i, gallerycarousel_getItemHTML(results[i], i, results.length));			
		}
		for (i = 0; i < results.length; i++){
			cdiv = 'crop' + (i+1);
			/* ***
			* To use crop, provide: 
			* crop([img src], [the div id that crops the img], [crop coordinate from left], [crop coordinate from top], [ % smaller width-wise],[% smaller height-wise], [original width], [original height])
			* Original width and height are necessary or irregularity of display of image thumbnail will take place with uses with AJAX.
			*** */
			crop(results[i].img.ipath, cdiv, 40, 40, 280, 280, results[i].img.iWidth, results[i].img.iHeight, results[i].link, results[i].img.iAlt );
		}
	} else {
		results = (addTitle)? data.leaderboardphotoandvideo : data.photoandvideo;
		for (i = 0; i < results.length; i++) {
			carousel.add(i, mycarousel_getItemHTML(results[i], addTitle, i));
		}
	}
	carousel.size(results.length);
}
function mycarousel_itemLoadCallback(carousel, state) {
	if (state != 'init') { return; }
	jQuery.getJSON(it_carousel_url, function(data) {
	   mycarousel_itemAddCallback(carousel, carousel.first, carousel.last, data);
	});
}
function mycarousel_itemLoadCallback_single(carousel, state) {
	if (state != 'init') { return; }
	jQuery.getJSON(it_carousel_url_single, function(data) {
	   mycarousel_itemAddCallback(carousel, carousel.first, carousel.last, data, true);
	});
}
function gallerycarousel_itemLoadCallback(carousel, state) {
	if (state != 'init') { return; }
	jQuery.getJSON(it_carousel_url, function(data) {
	   mycarousel_itemAddCallback(carousel, carousel.first, carousel.last, data, false, true);
	});
}
jQuery.elementReady('nav-container', function() {
	jQuery('#topnav > ul li a span').bind("click", function(){
		var url = jQuery(this).attr("href");
		if(window.name == 'diggiFrame'){window.document.location = url;} 
		else {parent.window.document.location = url;}
	});
	var setNavTab = smap['default'];
	var $tabs = jQuery('#topnav > ul').tabs({event: 'mouseover', selected: setTabNav});
	if (typeof(sectionNavName) != 'undefined') {
		if (typeof(smap[sectionNavName]) != 'undefined') {setNavTab = smap[sectionNavName];}	
	}	
	var selected = jQuery('#topnav > ul').tabs('option', 'selected'); // Sets the page default
	jQuery('#topnav').mouseleave(function(){ // Returns the nav to the page default
		jQuery('#topnav > ul').tabs('option', 'selected', setTabNav);
	});
	jQuery('#topnav li:first a').css('background-image','none');
	jQuery('#topnav #fragment-1 li:first, #topnav #fragment-2 li:first, #topnav #fragment-3 li:first, #topnav #fragment-4 li:first, #topnav #fragment-5 li:first, #topnav #fragment-16 li:first, #topnav #fragment-7 li:first, #topnav #fragment-8 li:first').css({'border':'0px', 'padding-left':'2px'});
	jQuery('#topnav').css('display', 'block');			   
});
jQuery(document).ready(function() {
	/* CSS helper function that removes text-decorations from links with img children */
	jQuery('a:has(img)').css("textDecoration","none");
	/* End helper */						
	if(typeof(LBPrimaryColor) !== "undefined"){
		jQuery('#carousel_1').jcarousel({wrap: "both", scroll: 1, visible: 1, itemLoadCallback: mycarousel_itemLoadCallback_single});
		jQuery('#carousel_2').jcarousel({wrap: "both", scroll: 3, visible: 3, itemLoadCallback: mycarousel_itemLoadCallback });
		jQuery('#container2').prepend("<h3>" + it_carousel_title + "</h3>");
	}
	else if(typeof(contentType) !== "undefined" && contentType === "gallery"){
		if(isIE6){jQuery('#footer li:first-child').css('border','0');}	
		if(typeof(moregalleriesurl) !== "undefined"){
			pageNum = parseInt(pageNum);
			pageNumDiv = parseInt(pageNum / 4);
			pageNumRem = pageNum % 4;
			imageTotal = galleryTotal % 4
			var numOfImagestoStart = ((imageTotal > 0) && ((galleryTotal - pageNum) < 4 )) ? (4 - imageTotal) : 0;
			pageNumResult = (pageNumRem > 0) ? (pageNumDiv * 4) + 1 - numOfImagestoStart : (pageNum - 3);
			
			jQuery.getJSON(moregalleriesurl, function(data){
				var choices = [8];
				var int=0, used, galleryItem;
/* NEW CODE: tp and willi: 01/25/2010: changed: */ 
				var moregallerieslength = data.moregalleries.length; 
				for(var i=0;i<8;){
				  used = 'false';
/* NEW CODE: tp and willi: 01/25/2010: changed: */ 
					//int = Math.floor(Math.random()*20);
					int = Math.floor(Math.random()* moregallerieslength);
					choices[i] = int;
					for(var j=0;j<choices.length;j++){
/* NEW CODE: tp and willi: 01/25/2010: changed: */ 
						if ((j != i && choices[j] == int) && (typeof data.moregalleries[choices[j]] != 'undefined' && data.moregalleries[choices[j]].link.indexOf(articleID) > -1)){
						//if ((j != i && choices[j] == int) || data.moregalleries[choices[j]].link.indexOf(articleID) > -1){
						  used = 'true';
						  break;
						}
					}
					if(used === 'false'){i++;}
				}		
				for(var k=0;k<choices.length;k++){
					galleryItem = data.moregalleries[choices[k]];
					mdiv = 'crop' + (k+1);
					jQuery('#more-galleries-div p').append('<a href="' + galleryItem.link + '" title="' + galleryItem.img.iAlt + '"><img src="' + galleryItem.img.ipath + '" width="78" height="67" border="0" alt="' + galleryItem.img.iAlt + '" /><span>' + galleryItem.title + '</span></a>');
					if(k==0){jQuery('#more-galleries a img').replaceWith('<img src="' + galleryItem.img.ipath + '" width="78" height="67" border="0" alt="' + galleryItem.img.iAlt + '" />')}
				}
			});
			jQuery('#right-rail #advance-btns').width(jQuery('div#gallery img').width()).css('left', ((600 - jQuery('div#gallery img').width())/2));
			jQuery('div#gallery *')
				.mouseover(function(){jQuery('#right-rail #advance-btns').show();})
				.mouseout(function(){jQuery('#right-rail #advance-btns').hide();});
			
			function closeMoreGalleries(closebtn){
			  jQuery('#more-galleries-div').hide("normal", function(){jQuery('#more-galleries-div p').empty();});	
			}
			
			jQuery('#more-galleries, #more-galleries-div #close-btn').click(function(){jQuery('#more-galleries-div').toggle("normal");});
			jQuery('#carousel').jcarousel({
				wrap: "both", 
				start: pageNumResult,
				scroll: 4,
				itemLoadCallback: gallerycarousel_itemLoadCallback,
				itemVisibleInCallback: {
  					onBeforeAnimation: function(carousel, item, i, state, evt){
						if(i < 5){jQuery('.jcarousel-prev').hide();	}
						else {jQuery('.jcarousel-prev').show();}
						if(galleryTotal === i || galleryTotal < 5){jQuery('.jcarousel-next').hide();	}
						else {jQuery('.jcarousel-next').show();}
					}
				}
			});
		}
	}
	else if(typeof(contentType) !== "undefined" && contentType === "channel"){
		if(jQuery('#tabbed-toolbar > ul').length > 0){
			jQuery('#tabbed-toolbar > ul').tabs({event: 'click', selected : it_ttb_default});
		}
		// ******* Omniture clicking functionality Start ********
		var fireOmniClick = function(path){
			s_time.linkTrackVars = 'prop28,prop29';
			s_time.linkTrackEvents = 'None';				
			s_time.prop28 = 'widget|'+sectionNavName;
			s_time.prop29 = path;
			void(s_time.tl(this,'o','Widget Actions'));
		}
		function cleanArray(items) {
			var len = items.length;
			var newArray = [];
			for(var i=0; i<len; i++){
				if(items[i].search(/[a-zA-Z0-9]/) < 0){
					newArray.push('x');
				}
				else {
					newArray.push(items[i]);
				}
			}
			return newArray;
		}
		function getTab (){
			return jQuery('#tabbed-toolbar > ul ').find('li.ui-tabs-selected').text();
		}
		var sTab, val, tree, href;
		jQuery('#tabbed-toolbar').find('a').click(function(){
			sTab = getTab();
			val = jQuery(this).text();
			if (sTab.toLowerCase().indexOf('equipment') > -1 && val == ""){
				href = jQuery(this).find('img').attr('src');
				switch(true){
					case href.indexOf('club') > -1:
						val = "find clubs";
						break;
					case href.indexOf('balls') > -1:
						val = "find balls";
						break;
					case href.indexOf('shoes') > -1:
						val = "find shoes";
						break;
					case href.indexOf('bags') > -1:
						val = "find bags";
						break;
					case href.indexOf('acc') > -1:
						val = "find accessories";
						break;
				}
			}
			else if (jQuery(this).attr('href').indexOf('egolfcertificates') > -1 && val == ""){
				val = "ad|"+jQuery(this).attr('href').match(/\d+$/);				
			}			
			var temp1 = sTab.slice(sTab.length-1,sTab.length);
			sTab = (temp1 == ' ') ? sTab.substr(0, sTab.length-1) : sTab;
			tree = (sTab.toLowerCase() === val.toLowerCase()) ? sTab.toLowerCase() : sTab.toLowerCase()+"|"+val.toLowerCase();
			fireOmniClick("widget|"+tree.toLowerCase());
		});
		jQuery('#tabbed-toolbar').find('form').submit(function(){
			var arr = [];
			sTab = getTab();
			
			jQuery(this).find("input, select").each(function(i){arr.push(this.value);});
			
			arr.pop();
			val = cleanArray(arr);
			val = val.join('|');
			var temp2 = sTab.slice(sTab.length-1,sTab.length);
			sTab = (temp2 == ' ') ? sTab.substr(0, sTab.length-1) : sTab;
			if(val.indexOf('Equipment Finder') > -1){fireOmniClick("widget|"+sTab.toLowerCase()+"|search|search|"+val.toLowerCase());}
			else {fireOmniClick("widget|"+sTab.toLowerCase()+"|search|"+val.toLowerCase());}
		});
		// ******* Omniture clicking functionality End ********
		jQuery('#tabbed-toolbar').css('display', 'block');
		jQuery('.right-banner #tabbed-toolbar .ui-tabs-nav li').css('min-width', '72px');
		
		if(isMacFF()){
			jQuery('#tabbed-toolbar .ui-tabs-nav li').css('min-width', '55px');
			jQuery('#tabbed-toolbar .ui-tabs-nav li.selected').css('min-width', '56px');
			jQuery('.right-banner #tabbed-toolbar .ui-tabs-nav li').css('min-width', '72px');
		}
		if(isMacSafari()){
			jQuery('#tabbed-toolbar .ui-tabs-nav li').css('min-width', '56px');
			jQuery('#tabbed-toolbar .ui-tabs-nav li.selected').css('min-width', '57px');
		}
		if(isIE6()) {
			jQuery('#tabbed-toolbar .ui-tabs-nav li').css('width', '72px');
		}
		jQuery('#carousel').jcarousel({
			wrap: "both", 
			scroll: 4,
			itemLoadCallback: mycarousel_itemLoadCallback
		});
		it_carousel_title = (typeof(it_carousel_title) !== "undefined") ? it_carousel_title : "";
		jQuery('.jcarousel-container').prepend("<h3>" + it_carousel_title + "</h3>");
		jQuery('#s_courses_travel, #s_equipment, #s_instruction').bind("focus", function() {
				var defaultValue = $(this).value;
				var def = ["search instruction","search equipment","search courses & travel"];
				var len = def.length;
				for (var i=0; i<len;i++){
					if($(this).value === def[i]){
						$(this).value = "";
						$(this).style.color = "#4C4C4C";
					}
				}					
				function resetF(e) {
					for (var i=0; i<len;i++){
						if($(this).value.length === 0){
							$(this).value = defaultValue;
							$(this).style.color = "#CCCCCC";
						}
					}
					jQuery('#s_courses_travel, #s_equipment, #s_instruction').unbind("blur");
				}
				return jQuery('#s_courses_travel, #s_equipment, #s_instruction').bind("blur", resetF); 
		});
	}
	else if(typeof(contentType) !== "undefined" && contentType === "article") {
		jQuery('#carousel').jcarousel({ wrap: "both",  scroll: 4, itemLoadCallback: mycarousel_itemLoadCallback });
		jQuery('#carousel_2').jcarousel({ wrap: "both",  scroll: 3, itemLoadCallback: mycarousel_itemLoadCallback });
	}
	else {
		jQuery('#carousel').jcarousel({
			wrap: "both", 
			scroll: 4,
			itemLoadCallback: mycarousel_itemLoadCallback
		});
	}
	
	jQuery('#tabbed-scorecard > ul').tabs({event: 'click', selected : '0'});
	jQuery('#tabbed-scorecard').css('display', 'block');
	jQuery('#tabbed-scorecard ul li:last span').css('border','0');
	jQuery('#toggle9 a:first').click(function(){
		jQuery('.course-scorecard:eq(0) div:eq(1), .course-scorecard:eq(1) div:eq(1), .course-scorecard:eq(2) div:eq(1), .course-scorecard:eq(3) div:eq(1), .course-scorecard:eq(4) div:eq(1)')
			.removeClass("box_shown")
			.addClass("box_hidden");
		jQuery('.course-scorecard:eq(0) div:eq(0), .course-scorecard:eq(1) div:eq(0), .course-scorecard:eq(2) div:eq(0), .course-scorecard:eq(3) div:eq(0), .course-scorecard:eq(4) div:eq(0)')
			.removeClass("box_hidden")
			.addClass("box_shown");
	})		
	jQuery('#toggle9 a:last').click(function(){
		jQuery('.course-scorecard:eq(0) div:eq(0), .course-scorecard:eq(1) div:eq(0), .course-scorecard:eq(2) div:eq(0), .course-scorecard:eq(3) div:eq(0), .course-scorecard:eq(4) div:eq(0)')
			.removeClass("box_shown")
			.addClass("box_hidden");
		jQuery('.course-scorecard:eq(0) div:eq(1), .course-scorecard:eq(1) div:eq(1), .course-scorecard:eq(2) div:eq(1), .course-scorecard:eq(3) div:eq(1), .course-scorecard:eq(4) div:eq(1)')
			.removeClass("box_hidden")
			.addClass("box_shown");
	})
});

