var cycleInterval;
$(window).load(function() {
    init();
	
	$("#search_nav input").keyup(function(event) {
		if(event.keyCode === 13) {
			window.location = 'http://www.google.com/#' + $(this).serialize() + '+site:olmcshrine.org';
		}
	});
	
    var History = window.History; // Note: We are using a capital H instead of a lower h
    if ( !History.enabled ) {
         // History.js is disabled for this browser.
         // This is because we can optionally choose to support HTML4 browsers or not.
        return false;
    }

    History.Adapter.bind(window,'statechange',function(e){ // Note: We are using statechange instead of popstate
        e.preventDefault();
        var State = History.getState(); // Note: We are using History.getState() instead of event.state
        History.log(State.data, State.title, State.url);
        var p = State.data.p;

        if(p === undefined) {
            p = 'home';
        }
        $('#target').load(p + ' #main');
        $('title').load(p + ' title');
    });

    $('a').click(function(e) {
        var domain = document.domain;
        var $link = $(this);
        var link = $link.prop('href');

        if((link.indexOf(domain) > 0)&&(link.indexOf(domain + '/img/') < 0)) {
            e.preventDefault();
            var href = $link.attr('href');
            $('#target').load(href + ' #main');
            $('title').load(href + ' title');
            
            History.pushState({'p': href, rand:Math.random()}, $('title').val(), href);
            init();
        }
    });
});

function init() {
    $('.gallery a.gallery_link').colorbox({rel:'colorbox'});
    $('.shop .item a').colorbox();
    clearInterval(cycleInterval);
	cycleInterval = setInterval(function() {
        var $old = $('#hp_img img:first');
        var $new = $old.next();
        $old.css({'z-index' : 5});
        $new.css({'z-index' : 5});
        $('#hp_img').animate({
            left: -1 * $new.width()
        }, 1000, function() {
            $old.remove();
            $('#hp_img').append($old);
            $('#hp_img').css({ 'left' : '0px' })
        });
	}, 7000);
}
