$(document).ready(function() {
		$('#grid table td img').mouseenter(function() {
			$('#grid table td img').removeClass('current');
			$(this).addClass('current');
			
			$('#grid table td img').each(function() {
				if(!$(this).hasClass('current')) {
					$(this).stop().animate({ opacity: 0.35 });
				} else {
					$(this).stop().animate({ opacity: 1 });
				}
			});
		});
										
										
		
		$('#grid table td img').mouseleave(function() {
			$('#grid table td img').removeClass('current');
			
			$('#grid table td img').each(function() {
				$(this).stop().animate({ opacity: 1 });
			});
		});
		
		$('.menu ul li a').click(function() {
			if($(this).parent().hasClass('page_item page-item-12')) {
				return false;
			} else if($(this).parent().hasClass('page_item page-item-10')) {
				return false;
			} else {
				$('.menu ul li').removeClass('current_page_item');
				$(this).parent().addClass('current_page_item');
				var theURL = $(this).attr('href');
				loadNewPage(theURL);
				return false;								  
			}
		});
		
		
		if(window.location.hash) {
			var theURL = window.location.hash;
			theURL = theURL.substr(1);
			location.hash = url+"/"+theURL;
			loadNewPage(theURL);
			return false;		
		}
		
		readyfunctions();
});

var clicked = false;

function readyfunctions() {
	$('.scroll').jScrollPane();
	$('.director.click span').click(function() {
		var element = "#hidden-"+$(this).attr('id');
		if(!clicked) {
			if($('.director.click DIV').hasClass('active') && !$(this).parent().find('DIV').hasClass('active')) {
				$('.director.click DIV.active').slideUp(function() {
					$('.director.click DIV').removeClass('active');
					clicked = true;
					$(element).slideDown(function() {
						readyfunctions();	
						clicked = false;
						$(element).addClass('active');
					});
				});
			} else {
				clicked = true;
				$(element).slideToggle(function() {
					readyfunctions();	
					clicked = false;
					$(element).addClass('active');
				});
			}
		}
	});
	
	$("#theatre-wrap a").click(function() {
		if($("#theatre-wrap").find('.wditem')) { } else {
			var theURL = $(this).attr('href');
			if($(this).attr('target') != "_blank") {
				loadNewPage(theURL);
				return false;	
			}
		}
	});
	
	
	
	$('.director.click DIV a').click(function() {
		var theURL = $(this).attr('href');
		loadNewPage(theURL);
		return false;
	});
	
	$('.float-black a').click(function() {
			var theURL = $(this).attr('href');
			loadNewPage(theURL);
			return false;	
	});
	
	
	$('#expander ul:first li a').click(function() {
		if($(this).parent().find('ul').children().size() > 0 ) {
			$('#expander ul:first li ul').slideUp();
			if(!$(this).parent().find('ul').is(":visible")) {
				$(this).parent().find('ul').slideToggle();
			}
			return false;									
		} else {
			var theURL = $(this).attr('href');
			loadNewPage(theURL);
			return false;
		}
	});
	
	$('.btn-view a').click(function() {
		var theURL = $(this).attr('href');
		loadNewPage(theURL);
		return false;						  
	});
	
	$('#expander ul li').each(function() {
		if($(this).find('ul').size() <= 0 ) {
			$(this).addClass('nochildren');
		}
	})
	
	$('.vid-thumb').click(function() {
		var theURL = $(this).attr('href');
		var theELEMENT = $(this);
		$('.vid-thumb').removeClass('current');
		theELEMENT.addClass('current');
		return false;							   
	});
}

function loadNewPage(theURL) {
	var result = theURL.replace(url+"/","");
	location.hash = result;
	$('#theatre-cover').slideDown(function() {
			$('.loading').fadeIn();	
			$('#theatre-vid').load(theURL+' #page-content',function() {
				if($('#player').length > 0) {
					var theIMG = $('#player').find('span').html();
					var theFILE = $('#player').find('a').attr('href');
					 jwplayer('player').setup({
						'flashplayer': template+'/js/jwplayer-5.5.swf',
						'file': theFILE,
						'skin': template+"/js/glow/glow.zip",
						'image': theIMG,
						'width': '533',
						'height': '280',
						'stretching': 'fill',
						'controlbar': 'over',
						'provider': 'http',
						'autostart': 'true',
						'http.startparam': 'ec_seek'
					  });
				}
				
				if($('.tweet').length > 0) {
					$(".tweet").tweet({
						join_text: "auto",
						username: "TackleBoxFilms",
						avatar_size: false,
						count: 2,
						loading_text: "loading tweets..."
					});	
				}
				
				$('.loading').fadeOut(function() {
						readyfunctions();
						$('#theatre-cover').slideUp();					   
				});
			});
	});
}

function loadNewMovie(theURL) {
	var vidTemplate = '<object style="height: 390px; width: 640px"> \
                            	<param id="the-video-param" name="movie" value="'+theURL+'">\
                                <param name="allowFullScreen" value="true">\
                                <param name="allowScriptAccess" value="always">\
								<param name="wmode" value="transparent" />\
                                <embed class="the-video-param" src="http://www.youtube.com/v/'+theURL+'?version=3&autoplay=1" type="application/x-shockwave-flash" allowfullscreen="true" allowScriptAccess="always" width="533" height="280">\
                            </object>';
	return vidTemplate;
}

function setSlideSwitch(container,tabs,time) {
	slideInterval = setInterval( function() { slideSwitch(container,tabs)},time);	
}

function slideSwitch(container,tabs) {
	if($(container + ' DIV').size() > 1) {
    var $active = $(container + ' DIV.active');

    if ( $active.length == 0 ) $active = $(container + ' DIV:last');

    var $next =  $active.next().length ? $active.next()
        : $(container + ' DIV:first');

    $active.addClass('last-active');
	
	//var $activeDesc = $('.slider-desc .active');
	//var $nextDesc = $activeDesc.next().length ? $activeDesc.next() : $('.slider-desc DIV:first');
    $next.find('span').stop().animate({bottom:-(parseFloat($next.find('span').css('height'))+30)},0);
	$active.find('span').stop().animate({bottom:-(parseFloat($active.find('span').css('height'))+30)},750,'easeOutBack', function() {
	$next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
	$next.find('span').stop().delay(750).animate({bottom:0},750,'easeOutBack');
											});
	
	
	/*$activeDesc.addClass('last-active');

    $nextDesc.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $activeDesc.removeClass('active last-active');
        });*/
	
	if(tabs == "true") { 
		var $curtab = $(".tab-wrap ul.slide-tabs li.current");
		if ( $curtab.length == 0 ) $curtab = $('.tab-wrap ul.slide-tabs li:last');
		
		var $nextab =  $curtab.next().length ? $curtab.next()
        : $('.tab-wrap ul.slide-tabs li:first');
		
		$nextab.addClass('current');
		$curtab.removeClass('current');
	}
	
	}
	
}
