var maxPhotocount = 6;
//max number of links is 7, with 0 index max is 6
var photoCount = 1;
var newsdirection = "fwd";

var newsdata = 'latest_news.htm';
var videodata = 'latest_videos.htm';
var photodata = 'latest_photos.htm';
var flagdata = 'flag.htm';

$(document).ready(function() {
	
	// preload images
	$.preloadCssImages();

    // rollovers
    PEPS.rollover.init();
	
	// CSS zoom fix for IE6 hasLayout bugs
	if(jQuery.browser.msie && parseInt(jQuery.browser.version) == 6){
		jQuery('*').css({'zoom' : '1'});	
	}

    // PNG tranparency for IE6
    $('.png').pngfix();

    // set div links
    $(".link").click(function() {
        window.location = $(this).find("a").attr("href");
        return false;
    });

	// calendar print link
	$(".printButton").click( function() {
		window.print();
		return false;
	});
	
    // external links
    $("a[rel='external']").click(function() {
        window.open($(this).attr('href'));
        return false;
    });

    // Add end class to various lists
    $('ul li:last-child, ol li:last-child').addClass('end');

    // Zebra Stripes for lists
    $('li:odd', '.xml_list').addClass('zebra');
    $('li:odd', '.zebra_list').addClass('zebra');

    // List background color Hover
    $('li', 'ul.xml_list').hover(
    function() {
        if ($(this).hasClass('zebra')) {
            $(this).removeClass('zebra');
            $(this).addClass('hover');
            $(this).addClass('zebra2');
        } else {
            $(this).addClass('hover');
        }
    },
    function() {
        if ($(this).hasClass('zebra2')) {
            $(this).removeClass('zebra2');
            $(this).removeClass('hover');
            $(this).addClass('zebra');
        } else {
            $(this).removeClass('hover');
        }
    });
    
    // Top Photos
	// First, transparent background for photoText
	$('.photoText').prepend('<div class="photoTextBG"></div>');
	$('.photoTextBG').fadeTo(0,0.66);
	
	// Next, set up slideshow
	$('#top_photo ul').cycle({
	    fx:     'fade', 
    	speed:   0, 
    	timeout: 8000, 
  		pager:  '#topPhotoPager'
	});

    // Updating News
    if ($('#bermynews').length > 0) {
        var news_list_height = 0;
        $('#news_list > li').each(function() {
            news_list_height += $(this).height();
        });
        $('#news_list').height(news_list_height);
        updateNews();


        $('#play_pause').click(function() {
            if ($(this).hasClass('pause')) {
                $(document).stopTime('newstimer');
                $(this).removeClass('pause');
                $(this).addClass('play');
            } else {
                $(this).removeClass('play');
                $(this).addClass('pause');
                if (newsdirection == "fwd") {
                    updateNewsDown();
                } else {
                    updateNewsUp();
                }
                updateNews();
            }
            return false;
        });
        $('a.up', '#bermynews').click(function() {
            $(document).stopTime('newstimer');
            newsdirection = 'rev';
            $('#play_pause').removeClass('pause');
            $('#play_pause').addClass('play');
            updateNewsUp();
            return false;
        });
        $('a.down', '#bermynews').click(function() {
            $(document).stopTime('newstimer');
            $('#play_pause').removeClass('pause');
            $('#play_pause').addClass('play');
            newsdirection = 'fwd';
            updateNewsDown();
            return false;
        });
    }
    // Toggle Shopaholic Products
    $('#shopaholic_moreless').click(function() {
        if ($(this).hasClass('view_more')) {
            $('#shopaholic_more').slideDown('normal');
            $('.shopaholics_footer').html('View less');
            $(this).removeClass('view_more');
            $(this).addClass('view_less');
        } else {
            $('#shopaholic_more').slideUp('normal');
            $('.shopaholics_footer').html('<strong>Want to see more great deals?</strong> Expand window for more &gt;');
            $(this).removeClass('view_less');
            $(this).addClass('view_more');
        }
        return false;
    });

    // Recent Videos Buttons
    $('a.prev', '#recent_videos').click(function() {
        getNewVideos('rev');
        return false;
    });
    $('a.next', '#recent_videos').click(function() {
        getNewVideos('fwd');
        return false;
    });
    
        // Photo Stories Buttons
    $('a.prev', '#our_photo_stories').click(function() {
        getNewPhotos('rev');
        return false;
    });
    $('a.next', '#our_photo_stories').click(function() {
        getNewPhotos('fwd');
        return false;
    });
    
    // Main Nav drop downs
    $('#main_nav ul li.dd').hover(
    function() {
        $('ul', this).show();
        $('a:first', this).addClass('hover');
		if($.browser.msie && $.browser.version < 7) {
			$('input, select').hide();
		}
    },
    function() {
        //$('ul', this).css("display", "none");
        $('ul', this).hide();
        $('a:first', this).removeClass('hover');
		if($.browser.msie && $.browser.version < 7) {
			$('input, select').show();
		}
    });

    // Premiers Address
    if ($('#premiers_address').length > 0) {
        // Make sure this only runs on the right page(s)
        $('#premiers_address').flash(
        {
            src: premier_clip,
            width: 320,
            height: 266,
            wmode: 'transparent'
        },
        {
            version: '9'
        }
        );
    }

    $('a.flag').click(function() {
        if (!$(this).hasClass('alreadyClicked')) {
            $(this).addClass('alreadyClicked');
            var flagID = $(this).attr('id');
            $.get('data/' + flagdata,
            {
                flagID: flagID
            },
            function(data) {
                $('#' + flagID).replaceWith(data);
                $('#' + flagID).fadeOut(15000);
            },
            'html');
        }
        return false;
    });

});




// Updating News function
function updateNews() {
    $(document).everyTime(5000, 'newstimer',
    function() {
        if (newsdirection == 'fwd') {
            updateNewsDown();
        } else {
            updateNewsUp();
        }
    });
}

function updateNewsDown() {

    var isZebra = $('li:last', '#news_list').hasClass('zebra');
    var articleID = $('li:last a:first', '#news_list').attr('title');
    $('#news_list').append('<li></li>');
    $('li:last', '#news_list').hover(
    function() {
        if ($(this).hasClass('zebra')) {
            $(this).removeClass('zebra');
            $(this).addClass('hover');
            $(this).addClass('zebra2');
        } else {
            $(this).addClass('hover');
        }
    },
    function() {
        if ($(this).hasClass('zebra2')) {
            $(this).removeClass('zebra2');
            $(this).removeClass('hover');
            $(this).addClass('zebra');
        } else {
            $(this).removeClass('hover');
        }
    });
    $('li:last', '#news_list').hide();
    if (isZebra != true) {
        $('li:last', '#news_list').addClass('zebra');
    }
    $('li:last', '#news_list').load('data/' + newsdata + ' div',
    {
        thearticleID: articleID,
        whichDirection: newsdirection
    },
    function() {
        var padding = ($('li:first', '#news_list').height() * -1) + "px";
        $('li:last', '#news_list').fadeIn('slow');
        $('li:first', '#news_list').animate({
            marginTop: padding
        },
        900,
        function() {
            $(this).remove();
        });
    });
}

function updateNewsUp() {
    var isZebra = $('li:first', '#news_list').hasClass('zebra');
    var articleID = $('li:first a:first', '#news_list').attr('title');
    $('#news_list').prepend('<li></li>');
    $('li:first', '#news_list').hover(
    function() {
        if ($(this).hasClass('zebra')) {
            $(this).removeClass('zebra');
            $(this).addClass('hover');
            $(this).addClass('zebra2');
        } else {
            $(this).addClass('hover');
        }
    },
    function() {
        if ($(this).hasClass('zebra2')) {
            $(this).removeClass('zebra2');
            $(this).removeClass('hover');
            $(this).addClass('zebra');
        } else {
            $(this).removeClass('hover');
        }
    });
    $('li:first', '#news_list').hide();
    $('li:first', '#news_list').css({
        marginTop: '-46px'
    });
    if (isZebra != true) {
        $('li:first', '#news_list').addClass('zebra');
    }
    $('li:first', '#news_list').load('data/' + newsdata + ' div',
    {
        thearticleID: articleID,
        whichDirection: newsdirection
    },
    function() {
        var padding = "0px";
        $('li:first', '#news_list').fadeIn('slow');
        $('li:first', '#news_list').animate({
            marginTop: padding
        },
        900,
        function() {
            $('li:last', '#news_list').remove();
        });
    });
}

// Recent Videos
function getNewVideos(direction) {
    if (direction == 'fwd') {
        videoID = $('a:last', '#recent_videos_previews').attr('title');
    } else {
        videoID = $('a:first', '#recent_videos_previews').attr('title');
    }
    $('#recent_videos_previews').load('data/' + videodata + ' div:first', {
        thevideoID: videoID,
        whichDirection: direction
    });
}
// Photo Stories
function getNewPhotos(direction) {
    if (direction == 'fwd') {
        photoID = $('a:last', '#photo_stories_previews').attr('title');
    } else {
        photoID = $('a:first', '#photo_stories_previews').attr('title');
    }
    $('#photo_stories_previews').load('data/' + photodata + ' div:first', {
        thephotoID: photoID,
        whichDirection: direction
    });
}
