jQuery.fn.showIt = function(){
	return $(this).css({"opacity": "0.0","display":"block"}).animate({opacity: 1.0}, 600);
};

jQuery.fn.hideIt = function(){
	return $(this).css({"opacity": "0.0", "display":"none"}).animate({opacity: 0.0}, 600);
};

jQuery.fn.remActive = function(){
	return $(this).removeClass('active').addClass('inactive');
};

jQuery.fn.remIn = function(){
	return $(this).removeClass('inactive').addClass('active');
};

function showGals(element){
	var the_id = "";
	var sub = "";
	var gal = "";
	var active = "";
	
	active = $(".gal li:.active");
	the_id = $(element).attr('id')+"-gal";
	sub = $(element).attr('id')+"-sub";
	
	if($("#"+sub).is(":hidden")){
		if($.browser.msie)
		{
			$(".sub").hide(375).remActive();
			$("#"+sub).show(375).remIn();
		} 
		else
		{
			$(".sub").slideUp(375).remActive();
			$("#"+sub).slideDown(375).remIn();
		}
	}
	
	else{
		$("#"+sub).slideUp(375).remActive();
	}
	$("#"+sub).not('active').remActive();
}

function initSlide(gal){
		var timer = 0;
		timer = window.setInterval(function(){
		var active = $('.gal > li.active');
		var s_active = $("#initial-switcher li.active");
		var s_next = $(s_active).next().length ? $(s_active).next() : $("#initial-switcher li:last");
		$(s_active).removeClass('active');
		$(s_next).addClass('active');
		
		if($(active).next('li').length !=0)
		{
			var next = $(active).next('li');
			if ($.browser.msie) {
       			$(active).hide().removeClass("active").addClass("inactive");
       			$(next).removeClass('inactive');
       			$(next).show().addClass('active');
   			}
			
			else{
				$(active).hideIt().remActive();
				$(next).showIt().remIn();
			}
		}
		
		else{window.clearInterval(timer);}
		},4000);
	
	$("#initial-switcher li").click(function(){
		clearInterval(timer);
		$("#initial-switcher li").remActive();
		var ind = $(this).index();
		$("#text-gal .active").remActive().css({"display":"none"}).fadeOut("fast");
		$(this).remIn();
		
		if ($.browser.msie){
			$("#text-gal > li").eq(ind).remIn().css({"display":"block"}).fadeIn("slow");
			}
		
		else{
			$("#text-gal > li").eq(ind).remIn().showIt();
		}
	});
				
	
	$(".switcher .sub li").click(function(){
		$("#initial-switcher").animate({opacity:0.0},600).css({"display":"none"}).remActive();
		
		var ind = $(this).index();
		var id = $(this).parent().siblings("a").attr('id');
		var gallery = id+"-gal";
		var switcher =  "#"+id + "-switcher";
		$(switcher+" > li").eq(ind).addClass('active');
		
		if ($.browser.msie)
		{
			$("#text-gal #first").css({"display":"none"}).hide("slow");
			$("#"+gallery).css({"display":"block"}).show("slow");
			$(switcher).css({"display":"block"}).animate({opacity:1.0},600).remIn();
		}
		
		else
		{
			$(this).parent().remIn();
			$("#text-gal #first").hideIt();
			$("#"+gallery).remIn().showIt();
			$(switcher).css({"display":"block"}).animate({opacity:1.0},600).remIn();
			
		}

		$("#"+gallery+" .frame").eq(ind).remIn().showIt();
		$("#"+gallery+" .frame").not('.active').remActive().css({"display":"none"});
		rotateGal(id);
	});
	
	$('a.popup-open').click(function(){
		clearInterval(timer);
	});
}

function rotateGal(gallery){
	var timer = 0;
	var gal = gallery;
	gallery = gallery +"-gal";
	
	timer = window.setInterval(function(){
		var active = $("#"+gallery+" .active");
		var active_s = $("#"+gal+"-switcher .active");
		var next_s = $(active_s).next('li').length ? $(active_s).next('li') : $("#"+gal+"-switcher li:first");
		var next = $(active).next('.frame').length ? $(active).next('.frame') : $("#"+gallery+" > .frame:first");
		
		if ($.browser.msie) {
       			$(active).hide().removeClass("active").addClass("inactive");
       			$(next).removeClass('inactive').show().addClass('active');
       			$(active_s).removeClass("active").addClass("inactive");
       			$(next_s).removeClass('inactive').addClass('active');
   			}
   			
		else{
			$(active).removeClass('frame active').hideIt().addClass('frame inactive');
			$(next).removeClass('frame inactive').showIt().addClass('frame active');
			$(active_s).removeClass("active").addClass("inactive");
       			$(next_s).removeClass('inactive').addClass('active');
		}
	},4000);
	
	$(".return").click(function(){
		clearInterval(timer);
	});
	$('a.popup-open').click(function(){
		clearInterval(timer);
	});
	
	$(".next-arrow").click(function(){
		var active = $("#"+gallery+" .active");
		var next = $(active).next('.frame').length ? $(active).next('.frame') : $("#"+gallery+" > .frame:first");
		clearInterval(timer);
		
		if ($.browser.msie) {
       			$(active).hide().css({"display":"none"}).removeClass("frame active").addClass("frame inactive");
       			$(next).show().css({"display":"block"}).removeClass('frame inactive').addClass('frame active');
   			}
   			
   		else{	
			$(active).hideIt().removeClass('frame active').addClass('frame inactive');
			$(next).removeClass('frame inactive').showIt().addClass('frame active');
			}
	});
	
	$(".prev-arrow").click(function(){
		var active = $("#"+gallery+" .active");
		var prev = $(active).prev('.frame').length ? $(active).prev('.frame') : $("#"+gallery+" > .frame:last");
		clearInterval(timer);
		
		if ($.browser.msie) {
       			$(active).hide().css({"display":"none"}).removeClass("active").addClass("inactive");
       			$(prev).removeClass('inactive');
       			$(prev).show().css({"display":"block"}).addClass('active');
   			}
   			
   		else{	
			$(active).hideIt().removeClass('frame active').addClass('frame inactive');
			$(prev).removeClass('frame inactive').showIt().addClass('frame active');
			}
	});
	
		$(".switcher2 li").click(function(){
		var the_id = $(this).parent().attr('name');
		clearInterval(timer);
		var ind = $(this).index();
		$("#"+the_id+"-gal .active").remActive().css({"display":"none"}).fadeOut("fast");
		$("#"+the_id+"-switcher li").removeClass('active')
		$(this).remIn();
		
		if ($.browser.msie){
			$("#"+the_id+"-gal .frame").eq(ind).remIn().css({"display":"block"}).fadeIn("slow");
			}
		
		else{
			$("#"+the_id+"-gal .frame").eq(ind).remIn().css({"display":"block","opacity":"1.0"});
		}
	});
}

$(document).ready(function(){
	$('.gal li').eq(0).remIn();
	$("#initial-switcher").addClass('active');
	$("#initial-switcher li").eq(0).addClass('active');
	$("#items .gallery-holder").css({"left":"-9999px"});
});

$(window).load(function(){
	initSlide('#text-gal');
	$("#items .gallery-holder").css({"left":"0px"});
	$(".return").click(function(){
		$(".gal").css({"display":"none"});
		$("#text-gal #initial").css({"display":"none"});
		$(".gal .active").removeClass('active');
		$(".switcher2").hideIt().remActive();
		$("#initial-switcher li").remActive();
		$("#initial-switcher").css({"display":"block"}).animate({opacity:1},400).remIn();
		$("#initial-switcher li:last").addClass('active');
		
		if ($.browser.msie)
		{
			$(".frame active").removeClass('frame active').addClass('frame inactive').fadeOut("slow").css({"display":"none"});
				
				if($("#text-gal").is(":hidden")){
					$("#text-gal").css({"display":"block"}).fadeIn("slow");
				}
					$("#text-gal > li").remActive().css({"display":"none"}).fadeOut("slow");
					$("#text-gal #first").removeClass('inactive').css({"display":"block"}).fadeIn("slow").addClass('active');		
		}
		
		else
		{
			$(".frame.active").removeClass('frame active').addClass('frame inactive').animate({opacity:0.0}).css({"display":"none"});
			
			if($("#text-gal").is(":hidden")){$("#text-gal").showIt();}
				$("#text-gal li.active").remActive().css({"display":"none","opacity":"0.0"});
				$("#text-gal #first").removeClass('inactive').showIt().addClass('active');
		}
		$(".switcher2 li").removeClass('active');
	});
	
		$(".switcher .header").click(function(){
		var chosen = $(this);
		showGals(chosen);
	});
	
	$("a.popup-open").click(function(){
		$('body').find('.tech-active').removeClass('tech-active');
		$(this).addClass('tech-active');
	});
	
		$('ul.gal > li a').click(function(){
			$('ul.gal > li a').removeClass('active');	
			$(this).parent('a').addClass('active');
		});
		$('body').find('a.popup-open').fancybox({
			'titlePosition'		: 'inside',
			'transitionIn'		: 'fade',
			'transitionOut'		: 'fade',
			'onStart'			: function(){
				var _popup = $('body').find('a.tech-active').attr('href');
				$('body').find(_popup).show();
			},
			'onClosed'			: function(){
				var _popup = $('body').find('a.tech-active').attr('href');
				$('body').find(_popup).hide();
			}
		});
});
