// Global JavaScripts

/* Function to add routines to page load event */
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

/* Just add class="rollover" to the img tag and make sure your images are named whatever.gif and whatever-over.gif */
/* You can use any kind of images and name them what you like.  The "-over" is the part that matters */
function initrollovers() {
	if (!document.getElementById) return;
	var aPreLoad = new Array();
	var sTempsrc;
	var aImages = $("img,input");
	for (var i = 0; i < aImages.length; i++) {
		if (aImages[i].className == 'rollover') {
			var src = aImages[i].getAttribute('src');
			var ftype = src.substring(src.lastIndexOf('.'), src.length);
			var hsrc = src.replace(ftype, '-over'+ftype);
			aImages[i].setAttribute('hsrc', hsrc);
			aPreLoad[i] = new Image();
			aPreLoad[i].src = hsrc;
			aImages[i].onmouseover = function() {
				sTempsrc = this.getAttribute('src');
				this.setAttribute('src', this.getAttribute('hsrc'));
			}
			aImages[i].onmouseout = function() {
				if (!sTempsrc) sTempsrc = this.getAttribute('src').replace('-over'+ftype, ftype);
				this.setAttribute('src', sTempsrc);
			}
		}
	}
} 


addLoadEvent(initrollovers);



// Browser Detection


function BrowserCheck() {
	var userAgent = navigator.userAgent.toLowerCase();
    $.browser.chrome = /chrome/.test(navigator.userAgent.toLowerCase()); 
    
    if($.browser.msie){
        $('body').addClass('msie');
        $('body').addClass('msie' + $.browser.version.substring(0,1));
    }
  
    if($.browser.chrome){
        $('body').addClass('chrome');
        userAgent = userAgent.substring(userAgent.indexOf('chrome/') +7);
        userAgent = userAgent.substring(0,1);
        $('body').addClass('chrome' + userAgent);
        $.browser.safari = false;
    }
    
    if($.browser.safari){
        $('body').addClass('safari');
        userAgent = userAgent.substring(userAgent.indexOf('version/') +8);
        userAgent = userAgent.substring(0,1);
        $('body').addClass('safari' + userAgent);
    }
    
    if($.browser.mozilla){
        if(navigator.userAgent.toLowerCase().indexOf('firefox') != -1){
            $('body').addClass('firefox');
            userAgent = userAgent.substring(userAgent.indexOf('firefox/') +8);
            userAgent = userAgent.substring(0,1);
            $('body').addClass('firefox' + userAgent);
        }
        else{
            $('body').addClass('mozilla');
        }
    }

    if($.browser.opera){
        $('body').addClass('opera');
    }
}




function emptyShoppingCart () {

	if ($("#catCartDetails a.cartlink").length > 0) {
	
		//$("#catCartDetails").html('<h3>Your shopping cart is empty.</h3><p><a href="/shop-twb/">Continue shopping.</a></p>');
		
	}

}





// ADDED BY JAY

function PromoRollovers() {
	$(".Promo").mouseover(function() {
		$(this).addClass("Over");
	}).mouseout(function() {
		$(this).removeClass("Over");
	}).click(function() {
		location.href=$(this).find("a").attr("href");
	});
	
	$(".Promo2").mouseover(function() {
		$(this).addClass("Over2");
	}).mouseout(function() {
		$(this).removeClass("Over2");
	}).click(function() {
		location.href=$(this).find("a").attr("href");
	});
	
	$(".Promo3").mouseover(function() {
		$(this).addClass("Over3");
	}).mouseout(function() {
		$(this).removeClass("Over3");
	}).click(function() {
		location.href=$(this).find("a").attr("href");
	});
}

function HomepageCalendar() {
	if ($("#ButtonImg4").length > 0) {
		// Create an array of the 12 months 
		var months = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
		
		// Get the current date and then find the current month in the array
		var currentDate = new Date();
		var currentMonth = months[currentDate.getMonth()].toString();
		
		// Hide the generic calendar and show the current month calendar	
		var monthDiv = $("#" + currentMonth);
		if (monthDiv.length > 0) {
			$("#Generic").hide();
			monthDiv.css("display","block");
		}
	}
}

function SeasonalCalendar() {
	if ($("#Calendar").length > 0) {
		// Create an array of the 12 months 
		var months = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
		
		// Get the current date and then find the current month in the array and the current year
		var currentDate = new Date();
		var currentMonth = months[currentDate.getMonth()];
		var currentYear = currentDate.getFullYear();
		
		// Get the selected year and month from the URL
		var selectedYear = $.url.attr("file").replace(".htm", "");
		var selectedMonth = $.url.param("month");
		
		if ((currentYear == selectedYear) && (selectedMonth == undefined)) {
			// If the page was loaded without any selected values then set the current calendar and dropdown menus
			$("#January").hide();
			$("#" + currentMonth).css("display","block");
			$("#CalendarHdr h2").text(currentMonth + " " + currentYear);
			
			$("select[name=month]").val(currentMonth);
			$("select[name=year]").val(currentYear);
		}
		else {
			// Show January if there is no month selected
			if (selectedMonth == undefined) {
				selectedMonth = "January";
			}
			// Set the calendar and dropdown menus based on the user's selections
			$("#January").hide();
			$("#" + selectedMonth).css("display","block");
			$("#CalendarHdr h2").text(selectedMonth + " " + selectedYear);
			
			$("select[name=month]").val(selectedMonth);
			$("select[name=year]").val(selectedYear);
		}
	}
}

function ChangeCalendar() {
	// Get the values from the dropdowns
	var selectedYear = $("select[name=year]").val();
	var selectedMonth = $("select[name=month]").val();
	
	// Build a URL
	var url = (selectedYear + ".htm" + "?month=" + selectedMonth);
	
	// Redirect
	window.location.href=url;
}

function ChangeMonthCalendar() {
	// Get the values from the dropdowns
	var selectedYear = $("select[name=year]").val();
	var selectedMonth = $("select[name=month]").val();
	
	// Build a URL
	var url = ("../" + selectedYear + "/" + selectedYear + "_" + selectedMonth + ".htm");
	
	// Redirect
	window.location.href=url;
}


function ActiveNav() {
	if ($("#SideNav").length > 0) {
		// Get the file name of the page and section name
		var file = $.url.attr("file");
		var section = $.url.segment(1);
		
		if (file != null) {
			// If the file name exists, find the a tag that has that file name
			var currentNav = $("#SideNav a[href*=" + file + "]");
		} else {
			// Otherwise, find the a tag with the section name
			var currentNav = $("#SideNav a[href$=" + section + "/]");
		}
		// Make it the active link
		currentNav.addClass("active");
		
		// Navigate to the parent element
		currentSection = currentNav.parent().parent().prev();
		// If that element is a link, make that active as well
		if (currentSection.attr("href") != undefined) {
			currentSection.addClass("active");
		}
	}
}

function RecentPostsList(){
	if ($("ol.blogsitesummary").length > 0) {
		// Get all the links in the list and loop through
		$("ol.blogsitesummary li a").each(function() {
			// Create a variable for the current link
			var currentLink = $(this);
			// Find the position of the last hyphen
			var hyphen = currentLink.text().lastIndexOf("-");
			// Remove all text after the hyphen
			currentLink.text(currentLink.text().substr(0,hyphen));
			// Remove the two span tags next to the link
			currentLink.next().remove().end().next().remove();
		});
	}
}

function PhotoGallery(){
	if ($("#PhotoGalleryDisplay").length > 0) {
		// Remove elements with no image
		$('#PhotoGalleryDisplay li div img').each(function() {
			if ($(this).attr('src') == '') {
				$(this).parent().parent().remove();
			}
		});

		// Variable for gallery		
		var gallery = $("#PhotoGalleryDisplay");
		// Find number of images in gallery
		var numImages = gallery.find("li").length;
		
		gallery.find("li").each(function() {
			// Add Captions from alt tags
			var image = $(this).find("img");
			var caption = image.attr("alt");
			if (caption != "") {
				$(this).find("div").after("<div class=\"caption\">" + caption + "</div>");
			}
		});

		// Build gallery navigation
		var galleryNav = '<div id="GalleryNav">';
		for (i=1; i<=numImages; i++) {
			galleryNav += '<a id="GalleryNav' + i + '" href="#">' + i + '</a>';
		}
        galleryNav += '</div>';
		// Add the gallery navigation in before the gallery itself
		gallery.prepend(galleryNav);
		
		$('#PhotoGalleryDisplay').css({'display' : 'block'});
		$("#PhotoGalleryDisplay").jcarousel({
	        scroll: 1,
			animation: 0,
			auto: 0,
			wrap: 'last',
	        initCallback: carousel_initCallback,
			itemVisibleInCallback: carousel_itemVisibleInCallback,
	        buttonNextHTML: null,
	        buttonPrevHTML: null
	    });
	}
}

// Photo Gallery Support Functions
function carousel_itemVisibleInCallback(carousel, item, idx, state) {
	$('#GalleryNav a').removeClass("current");
	$('#GalleryNav'+idx).addClass("current");
}
function carousel_initCallback(carousel) {
	$('#GalleryNav a').bind('click', function() {
		carousel.scroll($.jcarousel.intval($(this).text()));
		carousel.stopAuto();
		carousel.startAuto();
		$('#GalleryNav a').removeClass("current");
		$(this).addClass("current");
		return false;
	});
	
	// Pause autoscrolling if the user moves with the cursor over the clip.
	carousel.clip.hover(function() {
		carousel.stopAuto();
	}, function() {
		carousel.startAuto();
	});
};


// Subscribe-Template



function showProductImageLarge(){
$("#imgHolder").css("display","block");
}

function showAddOns(){
$("#AddOnItems").css("display","block");
$("#AddOnEvents").css("display","block");
}























