var galPageSize = 6;
var pathToPlayer = 'http://www.fisherproductions.co.uk/assets/templates/fisher/flash/player-licensed.swf';

if (typeof menuItemInView == 'undefined') {
	menuItemInView = 0;
}

function externalLinks() {
	if (!document.getElementsByTagName) return;
		var anchors = document.getElementsByTagName("a");
		
	for (var i=0; i<anchors.length; i++) {
		var anchor = anchors[i];
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external")
			anchor.target = "_blank";
	}
}

function doTrigs() {
	//do the trigs
	jQuery('a.trig-result-image').click(function() {
		jQuery('ul.accordian li.result a.result').click();
		jQuery('ul.accordian li.result li:first a').click();
		return false;
	});

	jQuery('a.trig-in-depth').click(function() {
		jQuery('ul.accordian li a.in-depth').click();
		return false;
	});
}

function doSmallGals() {
	//do the venues small gallery
	jQuery('ul.gallery.small li a').click(function() {
		var imgSrc = jQuery(this).attr('href');
		var imgAlt = jQuery(this).attr('title');
		var img = new Image();

		jQuery(img).hide();

		jQuery('#content-content').addClass('loading').children().fadeOut('normal', function() {
			jQuery(img).load(function() {
				jQuery('#content-content').animate({height: img.height}).removeClass('loading');

				jQuery(this).appendTo('#content-content').fadeIn();
			}).error(function() {

			}).attr({
				src: imgSrc,
				alt: imgAlt,
				title: imgAlt,
				id: 'background-image'
			});

			jQuery('#content-content').empty();
		});
		
		return false;
	});

	jQuery('#image-big').css('height', '300px');
	jQuery('#content ul.gallery.small li:first a').click();
}

jQuery.noConflict();
jQuery('document').ready(function() {
	externalLinks();
		
	jQuery('#content, #sidebar, #sidebar ul.subnav, #footer').hide();
	
	if (typeof bgPath != 'undefined') {
		var bgImg = new Image();

		jQuery(bgImg).hide();
	
		jQuery(bgImg).load(function() {
			jQuery('#sidebar #content-content').animate({height: bgImg.height});
 			jQuery(this).appendTo('#sidebar #content-content').fadeIn();	
		}).attr({
			src: bgPath,
			alt: 'Background Image',
			title: 'Background Image',
			id: 'background-image'
		});
		
		//jQuery('#sidebar #content-content').addClass('image').empty();
		jQuery('#sidebar #content-content').addClass('image').find('img:not(.keep-me)').remove();
	}
	
	jQuery('#content').fadeIn('slow', function() {
		jQuery('#content').animate({opacity: 1}, 2000, function() {
			jQuery('#sidebar').fadeIn('slow', function() {
				jQuery('#sidebar ul.subnav, #footer').fadeIn('slow');   
			});
		});
	});
	
	//hide the submenus
	jQuery('ul.accordian li ul').hide();

	//page the venues list
	jQuery('ul.venues').pagedlist({
		pagesize: 16,
		itemInView: menuItemInView
	});

	//deal with any galleries
	jQuery('ul.accordian li.result ul.gallery').pagedlist({
		pagesize: galPageSize,
		navElementClass: 'gallery-nav',
		navBackClass: 'gallery-back-link',
		navMoreClass: 'gallery-more-link'
	});

	jQuery('ul.accordian li.before ul.gallery').pagedlist({
		pagesize: 3,
		navElementClass: 'gallery-nav',
		navBackClass: 'gallery-back-link',
		navMoreClass: 'gallery-more-link'
	});


	//deal with empty galleries
	jQuery('ul.accordian li ul.gallery').each(function(i) {
		if (jQuery(this).children().length==0) {
			jQuery(this).parent().hide();
		}
	});
  
	doSmallGals();

	doTrigs();

	//deal with people lists
	jQuery('ul.people .person-detail').hide();
	
	jQuery('ul.people a').click(function() {
		var newPerson = jQuery(this).parent().parent().find('div.person-detail').clone();
		jQuery('#content-content div.person-detail').fadeOut('fast').remove();
		jQuery('#content-content').append(newPerson);
		jQuery(newPerson).fadeIn();
		
		return false;
	});
	
	//news more/back links
	jQuery('.news-item-nav').live("click", function() {
		var clicked = this;
		jQuery('#content #article').fadeOut('fast', function() {
			jQuery('#content #article').empty().html(jQuery(jQuery(clicked).attr('href')).html()).fadeIn();
		});
		
		return false;	
	});
	
	//deal with the click
	jQuery('ul.accordian li a').click(function() {
		//if it's a video load it in
		if (jQuery(this).hasClass('video')) {
			var s1 = new SWFObject(pathToPlayer,'ply','720','500','9','#ffffff');
			s1.addParam('allowfullscreen','true');
			s1.addParam('allowscriptaccess','always');
			s1.addParam('wmode','transparent');
			s1.addParam('flashvars','file=' + jQuery(this).attr('href') + '&backcolor=000000&frontcolor=EEEEEE&lightcolor=666666&screencolor=000000&autostart=true');
			s1.write('content-content');

		//if we're posting to the content div
		} else if (jQuery(this).hasClass('to-content')) {
			//where is the new content hiding?
			var newContent = jQuery(jQuery(this).attr('href')).html();

			//get rid of the old thing and activate this one
			jQuery(this).parent().parent().find('li.active').removeClass('active').find('ul').slideUp();
			jQuery(this).parent().addClass('active');

			//fade content out, inject the new content and fade back up
			jQuery('#content-content').fadeOut('normal', function() {
				jQuery(this).removeClass('image').html(newContent).fadeIn('normal', function() {
					doSmallGals();
					doTrigs();
				});
			});
		
		//if it's an image
		} else if ((jQuery(this).parent().parent().hasClass('gallery'))&&(!jQuery(this).parent().hasClass('gallery-nav'))) {  
			jQuery(this).parent().parent().find('.clicked').removeClass('clicked');
			jQuery(this).parent().addClass('clicked');
			var imgSrc = jQuery(this).attr('href');
			var imgAlt = jQuery(this).attr('title');
			var gal = jQuery(this).parent().parent().attr('id');
			var img = new Image();

			jQuery(img).hide();

			jQuery('#content-content').addClass('loading').children().fadeOut('normal', function() {
				jQuery(img).load(function() {
					//jQuery('#content-content').animate({height: img.height}).removeClass('loading');
					jQuery('#content-content').removeClass('loading');
					jQuery(this).appendTo('#content-content').fadeIn();
				}).error(function() {

				}).attr({
					src: imgSrc,
					alt: imgAlt,
					title: imgAlt,
					rel: '#' + gal,
					id: 'background-image'
				}).click(function() {
					jQuery(jQuery(this).attr('rel')).find('.clicked').next().find('a').click();
				});

				jQuery('#content-content').addClass('image').empty();
      		});
      		
		//if its a level one link
		} else if (jQuery(this).parent().children('ul').length > 0) {
			jQuery(this).parent().parent().find('li.active').removeClass('active').find('ul').slideUp();
			jQuery(this).parent().addClass('active').find("ul").slideDown();
		} else {
			return true;
		}

		return false;  
	});

	jQuery('a.result').click().parent().removeClass('active');
	jQuery('li.overview').addClass('active');

	if (typeof menuOpen != 'undefined') {
		jQuery('ul.accordian li#nav' + menuOpen + ' a').click();
	}
  
	if (typeof launchResult != 'undefined') {
		jQuery('ul.accordian li.result').addClass('active').find('li:first a').click();
	}
});