var count = 0;
var doAcc = true;
var slideTime = 800;

$(function(){
	$("a[href^=http]").each(function(){
      if(this.href.indexOf(location.hostname) == -1) {
        $(this).attr('target', '_blank');
      }
    });
    
    $(".fancybox").fancybox({
    	'transitionIn'	:	'elastic',
    	'transitionOut'	:	'elastic',
    	'titleShow' : false
    });
    
  
    
    var gallerycount = 1;
    
    $(".gallery").each(function(){
    	$(this).children("dl").each(function(){
    		$(this).children("dt").children("a").attr("rel", "gallery-"+gallerycount);
    	});
    	
    	$("a[rel='gallery-"+gallerycount+"']").fancybox({
	    	titleShow: false
	    });
    	
    	gallerycount++;
    });
		
	$("a.youtube").click(function() {
				$.fancybox({
				'padding'			: 10,
				'centerOnScroll'	: true,
				'autoScale'			: false,
				'href'                : this.href.replace(new RegExp("watch\\?v=", "i"), 'v/')+'&autoplay=1',
                'type'                : 'swf',
                'swf'                 : {'allowfullscreen':'true'}
				});
				return false;
			});
		$(".vimeo").click(function() {
				$.fancybox({
				'padding'			: 10,
				'centerOnScroll'	: true,
				'autoScale'			: false,
				'href'				: this.href.replace(new RegExp("vimeo.com/", "i"), 'vimeo.com/moogaloop.swf?clip_id=')+'&autoplay=1',
				'type'				: 'swf',
				'swf'                 : {'allowfullscreen':'true'}
				});
				return false;
	});
	
	$("#main-nav li").hover(function(){
		$(this).children("ul").slideDown(200);
	}, function(){
		$(this).children("ul").slideUp(200);
	});
	
	if($("body").hasClass('home'))
	{
		var accordionWidth = 710;
		var accordionCount = 0;
		
		$(".accordion ol").cycle({ 
		    fx: 'scrollHorz',
		    timeout:  5000
		});
		
		/*$("#accordion #wrapper li").each(function(){
			accordionCount++;
			accordionWidth += 38;
		});*/
		
		//$("#accordion #wrapper").msAccordion({autodelay:4});
		/*$("#accordion #wrapper").liteAccordion({
			containerWidth: accordionWidth,
			containerHieght: 299,
			headerWidth: 38,
			autoPlay: true,
			slideCallback : function() {
				var newImage;
				var currentImage;
				$("h2.selected").removeClass("closed");
				newImage = $("h2.selected").attr("rel");
				currentImage = $("h2.selected").children("img").attr("src");
				$("h2.selected").children("img").attr("src", newImage);
				$("h2.selected").attr("rel", currentImage);
				
				$("h2").each(function(){
					if($(this).hasClass('selected') == false)
					{
						if($(this).hasClass("closed") == false)
						{
							$(this).addClass("closed");
							newImage = $(this).attr("rel");
							currentImage = $(this).children("img").attr("src");
							$(this).children("img").attr("src", newImage);
							$(this).attr("rel", currentImage);
						}
					}
				});
			}
		});*/
		/*
		$("#accordion #wrapper .set").each(function(){
			count++;
			$(this).attr("rel", count);
			$("#accordion #wrapper .set").children(".content").animate({
				marginLeft: "-710px",
				opacity: 0
			}, 0);
			
			if(count > 1)
			{
				var currentTitle = $(this).children('.title').children('img').attr("src");
				var newTitle = currentTitle.replace(".jpg", "-closed.jpg");
				$(this).children('.title').children('img').attr("src", newTitle);
			}
		});
		
		$("#accordion #wrapper .set").first().children(".content").animate({
			marginLeft: "0",
			opacity: 1
		}, 0, function(){ $(this).show(); });
		$("#accordion #wrapper .set").first().addClass('on');
		
		setTimeout('accRotate()', 4000);
		
		$("#accordion .title").click(function(){
			var parent = $(this).closest(".set");
			
			if(parent.hasClass("on"))
			{
				return false;
			}
			
			var active = $("#accordion .set.on");
			active.removeClass('on');
			active.children('.content').animate({
				marginLeft: "-710px",
				opacity: 0
			}, slideTime);
			
			var currentTitle = active.children('.title').children('img').attr("src");
			var newTitle = currentTitle.replace(".jpg", "-closed.jpg");
			active.children('.title').children('img').attr("src", newTitle);
			
			parent.addClass('on');
			parent.children('.content').animate({
				marginLeft: "0",
				opacity: 1
			}, slideTime);
			
			var currentTitle = $(this).children('img').attr("src");
			var newTitle = currentTitle.replace("-closed.jpg", ".jpg");
			$(this).children('img').attr("src", newTitle);
			
			doAcc = false;
		});
		*/
		
		$("#feature .content.on").show();
		
		$("#feature #tabs li").click(function(){
			if($(this).hasClass("on"))
			{
				return false;
			}
			
			var name = $(this).attr("class");
			
			var active_tab = $("#feature #tabs .on");
			
			active_tab.removeClass("on");
			$(this).addClass("on");
			
			var active_box = $("#feature .content.on");
			active_box.removeClass("on").slideUp(400, function(){
				var new_box = $("#feature .content." + name);
				new_box.addClass("on").slideDown();
			});
		});
		
		$("#feature .videos #others li").click(function(){
			var video_id = $(this).attr("rel");
			
			$("#main-video").fadeOut(500, function(){
				$("#main-video").html("<iframe src=\"http://player.vimeo.com/video/"+video_id+"\" width=\"560\" height=\"315\" frameborder=\"0\"></iframe>");
				$("#main-video").fadeIn(500);
			});
		});
	}
	
	$("#rotator .box.active").show();
		
	$("#rotator .tabs li").click(function(){
		if($(this).hasClass("active"))
		{
			return false;
		}
		
		var name = $(this).attr("class");
		
		var active_tab = $("#rotator .tabs .active");
		
		active_tab.removeClass("active");
		$(this).addClass("active");
		
		var active_box = $("#rotator .box.active");
		active_box.removeClass("active").fadeOut();
		
		var new_box = $("#rotator .box." + name);
		new_box.addClass("active").fadeIn();
	});
});

function accRotate()
{
	if(doAcc == false)
	{
		return false;
	}
	
	var currentSet = $("#accordion #wrapper .set.on");
	if(currentSet.attr("rel") == count)
	{
		var nextSet = $("#accordion #wrapper .set").first();
	}
	else
	{
		var nextSet = $("#accordion #wrapper .set.on").next('.set');
	}
	currentSet.removeClass('on');
	currentSet.children('.content').animate({
		marginLeft: "-710px",
		opacity: 0
	}, slideTime);
	
	var currentTitle = currentSet.children('.title').children('img').attr("src");
	var newTitle = currentTitle.replace(".jpg", "-closed.jpg");
	currentSet.children('.title').children('img').attr("src", newTitle);
	
	nextSet.addClass('on');
	nextSet.children('.content').animate({
		marginLeft: "0",
		opacity: 1
	}, slideTime);
	
	var currentTitle = nextSet.children('.title').children('img').attr("src");
	var newTitle = currentTitle.replace("-closed.jpg", ".jpg");
	nextSet.children('.title').children('img').attr("src", newTitle);
	
	setTimeout('accRotate()', 4000);
}
	
