Cufon.replace('h2', { fontFamily: 'Avenir'});
Cufon.replace('h4', { fontFamily: 'Avenir'});
Cufon.replace('.projectTitle', { fontFamily: 'Avenir'});
Cufon.replace('.projectSubTitle', { fontFamily: 'Avenir-Small'});
Cufon.replace('a.mapLink', { fontFamily: 'Avenir-Small', hover: true });
Cufon.replace('a.navLink', { fontFamily: 'Avenir-Small', hover: true });
Cufon.replace('#formBar', { fontFamily: 'Avenir-Small'} );
Cufon.replace('#featurenav', { fontFamily: 'Avenir-Small', hover: true });

var SliderIndex = 0;
var AutoSlide = true;
var MediaOpen = false;
var ShouldAnimate = false;

function initMenus() {
	$('ul.menu ul').hide();
	$.each($('ul.menu'), function(){
		$('#' + this.id + '.expandfirst ul:first').show();
	});
	$('ul.menu li a').click(
		function() {
			var checkElement = $(this).next();
			var grandparent = this.parentNode.parentNode.parentNode.id
			var parent = this.parentNode.parentNode.id;
			var group = this.parentNode.id;
			if(grandparent != 'menu_featured'){
				if($(this).hasClass('subsectionTitle')) {
					$('a.subsectionTitle').removeClass('active');
					$('#' + group + ' a.subsectionTitle').addClass('active');
					Cufon.refresh();
				} else {
					$('a.sectionTitle').removeClass('active');
					$('#' + group + ' a.sectionTitle').addClass('active');
					Cufon.refresh();
				}
				
				var FilmMenuHidden = false;
				if($('#' + group).hasClass('hasSubMenu') && !$('#' + group + ' ul').is(':visible')) {
					/*Do Film Design Menu Stuff*/
					$('#' + group + ' ul li ul').hide();
					$('#' + group + ' ul li ul').first().show();
					FilmMenuHidden = true;
				}
				
				
				if((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
					if(ShouldAnimate) {
						$('#' + parent + ' ul:visible').slideUp('normal');
						checkElement.slideDown('normal');
					} else {
						$('#' + parent + ' ul:visible').hide();
						checkElement.show();
					}
				}
				
				if($('#' + group).hasClass('hasSubMenu') && FilmMenuHidden) {
					/*Do Film Design Menu Stuff*/
					$('#' + group + ' ul li a.subsectionTitle').removeClass('active');
					$('#' + group + ' ul li a.subsectionTitle').first().addClass('active');
					Cufon.refresh();
				
				}
			}
			return false;
			
		}
	);
}


function showSearchBox() {
	$('.searchBox').show();
	$('.searchBox').focus();
	$('.searchCommand').show();
}

function hideSearchBox() {
	if($('.searchBox').val() == '') {
		$('.searchBox').hide();
		$('.searchCommand').hide();
	}
}

function sumbitSearch() {
	$('#searchsubmit').click();
}

function switchFeature() {
	if(AutoSlide && !MediaOpen) {
		SliderIndex++;
		if(SliderIndex > $('#slider #featurenav').find('a').length - 1) {
			SliderIndex = 0;
		}
		$('#slider #featurenav').find('a').eq(SliderIndex).click();
	}
}

function disableAutoSlide() {
	alert('autoslide disbaled');
	AutoSlide = false;
}

function setupScroll() {
	var $panels = $('.panel');
    var $container = $('div.scrollcontainer');
	
    var horizontal = true;

    // float the panels left if we're going horizontal
    if (horizontal) {
        $panels.css({
            'float' : 'left',
            'position' : 'relative' // IE fix to ensure overflow is hidden
        });

        // calculate a new width for the container (so it holds all panels)
        $container.css('width', $panels[0].offsetWidth * $panels.length);
    }

    // collect the scroll object, at the same time apply the hidden overflow
    // to remove the default scrollbars that will appear
    var $scroll = $('#slider .scroll').css('overflow', 'hidden');

    // handle nav selection
    function selectNav(evt) {
    	var FeatureLinks = $('#menu_featured ul').find('a.navLink');
    	var LinkIndex = $('#featurenav a').index(this);
        $('#featurenav a').removeClass('selected');
        $(this).addClass('selected');
        FeatureLinks.removeClass('green');
        FeatureLinks.eq(LinkIndex).addClass('green');
        Cufon.refresh();
        SliderIndex = LinkIndex;
    }
    
    function selectNavB(evt) {
    	var LinkIndex = $('#menu_featured ul').find('a.navLink').index(this);
        $('#slider #featurenav').find('a').eq(LinkIndex).click();
    }

    $('#slider #featurenav').find('a').click(selectNav);
   	$('#menu_featured ul').find('a.navLink').click(selectNavB);

    // go find the navigation link that has this target and select the nav
    function trigger(data) {
        var el = $('#slider #featurenav').find('a[href$="' + data.id + '"]').get(0);
        selectNav.call(el);
    }

    if (window.location.hash) {
        trigger({ id : window.location.hash.substr(1) });
    } else {
        $('#featurenav a:first').click();
    }

    // offset is used to move to *exactly* the right place, since I'm using
    // padding on my example, I need to subtract the amount of padding to
    // the offset.  Try removing this to get a good idea of the effect
    var offset = parseInt((horizontal ? 
        $container.css('paddingTop') : 
        $container.css('paddingLeft')) 
        || 0) * -1;

    var scrollOptions = {
        target: $scroll, // the element that has the overflow

        // can be a selector which will be relative to the target
        items: $panels,

        navigation: '#featurenav a',

        // selectors are NOT relative to document, i.e. make sure they're unique
        prev: 'img.left', 
        next: 'img.right',

        // allow the scroll effect to run both directions
        axis: 'xy',

        onAfter: trigger, // our final callback

        offset: offset,

        // duration of the sliding effect
        duration: 500,

        // easing - can be used with the easing plugin: 
        // http://gsgd.co.uk/sandbox/jquery/easing/
        easing: 'swing'
    };
	
    // apply serialScroll to the slider - we chose this plugin because it 
    // supports// the indexed next and previous scroll along with hooking 
    // in to our navigation.
    $('#slider').serialScroll(scrollOptions);
    // now apply localScroll to hook any other arbitrary links to trigger 
    // the effect
    $.localScroll(scrollOptions);

    // finally, if the URL has a hash, move the slider in to position, 
    // setting the duration to 1 because I don't want it to scroll in the
    // very first page load.  We don't always need this, but it ensures
    // the positioning is absolutely spot on when the pages loads.
    scrollOptions.duration = 1;
    $.localScroll.hash(scrollOptions);

}

$(document).ready(function() {
	//initMenus();
	$('#logo').click(function(){window.location='http://www.picturemill.com';});
	$('.searchBox').hide().blur(hideSearchBox);
	$('.searchCommand').hide().click(sumbitSearch);
	$('#searchForm').click(showSearchBox);
	$('.big_play').hide();
	$('.poster').hover(
		function(){ $(this).find('.big_play').show(); },
		function(){ $(this).find('.big_play').hide(); }
	);
	
	setupScroll();
	
	$('#slider').mouseover(function(){AutoSlide = false;})
				.mouseout(function(){AutoSlide = true;});
	$('#menu_featured').mouseover(function(){AutoSlide = false;})
				.mouseout(function(){AutoSlide = true;});
	
	setInterval(switchFeature, 5000);
	ShouldAnimate = true;

});