/*
Although this javascript file initially contained only button code, I decided to use this as a common code file because it 
is included on all pages. 
 */

// simple animations if ie 7 or less.
var simpleAnim = ($.browser.msie && $.browser.version <= 8);

$(document).ready(function()
{
	// quick fix- wrap .rollover in .rollover-outer
	$(".rollover").wrap("<span class='rollover-outer'></span>");

	// fix for arrows
	$(".arrow-left, .arrow-right, #button-back").wrapInner("<span class='rollover-outer'><span class='rollover' /></span>");
	
    $(".rollover-outer").css({'opacity':'0'});

    var backgroundImage;
    
	$('.button, .arrow, .arrow-left, .arrow-right, #button-back').hover(
		function() {
			$(this).find('.rollover-outer').stop().fadeTo(150, 1, function()
			{
				$(this).parent().data("background-position",$(this).parent().css("background-position"));
				$(this).parent().css({"background-position": "1000px 1000px"});
			});
		},
		function() {
			$(this).find('.rollover-outer').stop().fadeTo(150, 0);

			$(this).css({"background-position": "0 0"});
		}
	)
	
	// navigation buttons (menu at bottom)
	$('#navigation .over-outer').fadeTo(0,0);
	$('#navigation a:not(.active)').hover(
		function() {
			$(this).find('.over-outer').stop().fadeTo(300, 1);
			$(this).find('.normal-outer').stop().fadeTo(300, 0);
		},
		function() {
			$(this).find('.over-outer').stop().fadeTo(500, 0);
			$(this).find('.normal-outer').stop().fadeTo(500, 1);
		}
	)
	
	$(window).load(function(){
		
		// preposition content to right
		var $c = $("#container");
		var left = $c.offset().left;
	});
	
	// preloader behaves different on front page -- as it disappears quickly
	$(window).ready(function(){
		if ($("#flash").length > 0)
		{
			$("#preloader").fadeOut(500);
		}
		else
		{
			$(window).load(function(){
				$("#preloader").fadeOut(500);
			});
		}
	});	
	
	// all links perform action before delayed redirect
	$('a:not(.radio)').each(function(idx, item) {
		if ($(this).parent().parent().parent().attr("id") == "device-navigation-inner") return;
		if ($(this).attr("id") == "device-navigation-down") return;
		if ($(this).attr("id") == "device-navigation-up") return;
		
		if ($(this).attr("href") != undefined)
		{
			if ($(this).attr("href") != "")
			{
				$(this).click(function(){
					delayedRedirect($(this).attr("href"));
					return false;
				})
			}
		}
	});
	

});	

/* delayed redirect does fancy things before redirecting */
function delayedRedirect(url) {
	
	// fade in preloader
	$("#preloader").fadeIn("300", function() { 
		// when faded in, load url
		window.location = url;
	});
	
}

function getScrollerWidth() {

    var scr = null;
    var inn = null;
    var wNoScroll = 0;
    var wScroll = 0;

	// Outer scrolling div
    scr = document.createElement('div');
    scr.style.position = 'absolute';
    scr.style.top = '-1000px';
    scr.style.left = '-1000px';
    scr.style.width = '100px';
    scr.style.height = '50px';
    // Start with no scrollbar
    scr.style.overflow = 'hidden';



    // Inner content div
    inn = document.createElement('div');
    inn.style.width = '100%';
    inn.style.height = '200px';

    // Put the inner div in the scrolling div
    scr.appendChild(inn);
    // Append the scrolling div to the doc
    document.body.appendChild(scr);

    // Width of the inner div sans scrollbar
    wNoScroll = inn.offsetWidth;
    // Add the scrollbar
    scr.style.overflow = 'auto';
    // Width of the inner div width scrollbar
    wScroll = inn.offsetWidth;

    // Remove the scrolling div from the doc
    document.body.removeChild(
    document.body.lastChild);

    // Pixel width of the scroller

    return (wNoScroll - wScroll);
}
function isMyStuffScrolling() {

    var docHeight = document.documentElement.scrollHeight;

    var scroll = $(window).height() + $(window).scrollTop();

    if ((docHeight - scroll) == 3 || docHeight == scroll) {
        return true;
    }

    else {
        return false;
    }
}
