/**
 * Gallery Variables
 */
var curGallery = null;
var curGalleryNav = null;
var curGallerySlide = 0;
var galleryNextButton = null;
var galleryPrevButton = null;
var galleryBusy = false;
var slideWidth = 960;
var slideHeight = 600;
var hasVideo = false;
/**
 * On Dom Ready Init Galleries
 */
jQuery(document).ready(function(){
	if(jQuery('#portfoliogallery').get(0)){
		if(jQuery('#portfoliogallery ul.gallery.active').get(0)){
			curGallery = jQuery('#portfoliogallery ul.gallery.active').first();
			curGalleryNav = jQuery('#galleryNav li.active').first().addClass('active');
		}else{
			curGallery = jQuery('#portfoliogallery ul.gallery').first().addClass('active');
			curGalleryNav = jQuery('#galleryNav li').first().addClass('active');
		}
		createGalleryNav();
		updateGalleryNav();
		bindGalleryEvents();
		hasVideo = curGallery.hasClass('motion');
	}
	jQuery('div.menuwrap a').click(function(){
		if(hasVideo) jwplayer("mediaplayer").stop();
	});
});
/**
 * Create Gallery Next/Prev Navigation
 */
function createGalleryNav(){
	galleryNextButton = jQuery('<span class="next"/>');
	galleryPrevButton = jQuery('<span class="prev"/>');
}
/**
 * Show/hide Gallery Navigation
 */
function updateGalleryNav(){
	curGalleryNav.append(galleryNextButton);
	curGalleryNav.append(galleryPrevButton);
	if(hasVideo){
		var pl = jwplayer("mediaplayer").getPlaylist();
		var count = pl.length;
		//setTimeout(function(){jwplayer("mediaplayer").play();}, 2000);
		setTimeout(function(){jwplayer("mediaplayer").play();}, 0);
		jQuery('body').addClass('motion');
	}else{
		var count = jQuery('li', curGallery).length;
	}
	if( count > 1 ){
		galleryNextButton.fadeIn();
		galleryPrevButton.fadeIn();
	}else{
		galleryNextButton.hide();
		galleryPrevButton.hide();
	}
	curGallerySlide = 0;
}
/**
 * Bind Gallery Navigation Events
 */
function bindGalleryEvents(){
	jQuery('#galleryNav li').live('click', function(){
		if(!jQuery(this).hasClass('web')){
			if(jQuery(this).hasClass('active') || galleryBusy) return false;
			else switchGallery(jQuery(this));
		}
	});
	galleryNextButton.bind('click', function(){
		if(hasVideo){
			jwplayer("mediaplayer").playlistNext();
		}else{
			var count = curGallery.find('li').length;
			if(count > 1){
				var next = curGallerySlide + 1;
				if(next >= count) next = 0;
				animateSlide(next, curGallerySlide, 'next');
			}
		}
		return false;
	});
	galleryPrevButton.bind('click', function(){
		if(hasVideo){
			jwplayer("mediaplayer").playlistPrev();
		}else{
			var count = curGallery.find('li').length;
			if(count > 1){
				var next = curGallerySlide - 1;
				if(next < 0) next = count-1;
				animateSlide(next, curGallerySlide, 'prev');
			}
		}
		return false;
	});
}
/**
 * Function Switch Gallery
 */
function switchGallery(li){
	if(galleryBusy) return false;
	if(hasVideo) jwplayer("mediaplayer").stop();
	var p = jQuery(li).attr('class');
	jQuery('#galleryNav li').removeClass('active');
	curGalleryNav = jQuery(li).addClass('active');
	galleryBusy = true;
	jQuery('body').removeClass('motion');
	jQuery(curGallery).fadeOut(1, function(){
		curGallery = jQuery('#portfoliogallery ul.'+p);
		if(curGallery.hasClass('motion')){
			curGallery.css({'left':0}).show().animate({left:0}, 1000, function(){
				hasVideo = curGallery.hasClass('motion');
				updateGalleryNav();
				galleryBusy = false;
			});
		}else{
			curGallery.hide().css({'left':0}).fadeIn(1, function(){
				hasVideo = curGallery.hasClass('motion');
				updateGalleryNav();
				galleryBusy = false;
			});
		}
	});
}
/**
 * Function Animate Next Gallery Slide
 */
function animateSlide(next, cur, dir){
	// Fadeout the current slide
	$cur = jQuery('li', curGallery).get(cur);
	$next = jQuery('li', curGallery).get(next);
	if(!$cur || !$next) return;
	galleryBusy = true;
	curGallerySlide = next;
	if(jQuery.browser.msie && jQuery.browser.version < 9){
		/*jQuery($cur).css({overflow:'hidden'}).animate({width:0, height:0}, 500, function(){
			jQuery($next).css({width:0, height:0, left:0, overflow:'visible'}).animate({width:slideWidth, height:slideHeight}, 500, function(){galleryBusy = false;});
		});*/
	}else{
		/*jQuery($cur, jQuery('.img', $cur) ).css({overflow:'hidden', opacity:1}).animate({width:0, height:0, opacity:0}, 500, function(){
			jQuery($next, jQuery('.img', $next)).css({width:0, height:0, left:0, opacity:0, overflow:'visible'}).animate({width:slideWidth, height:slideHeight, opacity:1}, 500, function(){galleryBusy = false;});
		});*/
	}
	jQuery($cur).hide();
	jQuery($next).css({left:0}).show();
	galleryBusy = false;
}

