/* 
Author: John Smith & Farhan Muhammad
fusion-room.com  2010 
*/

// function template
$(function() {
	
});

// Accordion
$(function() {
 if ($('#accordion').length > 0) {		
	$('#accordion').easyAccordion({ 
			autoStart: true, 
			slideInterval: 8000
	});
    var f = 1;
	
		$('#accordion').find('dt').each(function(){
			$(this).attr('id', 'slide' + f);
			f = f + 1;
		});
 }
});

// Twitter Rss Ticker
$(function() {
	if ($('.tweets').length > 0) {
	$('.tweets').twitterfeed('fusionroom', {
			tweeticon: false, 
			tweetname: false, 
			tweettime: false
	}).ajaxStop(function() {
		$('.tweets div.twitterBody').vTicker( { 
			showItems: 3,
			pause: 4800
		});
		$('.twitterBody ul').css({'list-style' : 'none'});
		//$('.twitterBody ul .twitterRow').css({'height' : 'auto'});
	});
	}
});

// Site-wide fade functions
$(function() {
    $('.fadeIn').fadeIn(750);
    $('.subMainImg').fadeIn(1000);
    $('#loading').hide();
    $('#mainScroll').fadeIn(3000);
    $('#socialMedia').fadeIn(1200);
});

//Fade In hidden divs one-by-one						   


$(document).ready(function() {
	var thingsToAnimate = $('#singleCol div.galleryInner, #singleCol div.galleryInner span, #slideThumbs li.menuItem').css('opacity', 0);
	i=0;
	function movement () {
		$(thingsToAnimate[i++]).animate({
				'right': 0,
				'opacity': 1
			}, 250, arguments.callee);
	};
	movement();
	
	//Show Banner
	$(".main_image .desc").show(); //Show Banner
	$(".main_image .block").animate({ opacity: 0.85 }, 1 ); //Set Opacity

	//Click and Hover events for thumbnail list
	$(".image_thumb ul li:first").addClass('active'); 
	$(".image_thumb ul li").click(function(){ 
		//Set Variables
		var imgAlt = $(this).find('img').attr("alt"); //Get Alt Tag of Image
		var imgTitle = $(this).find('a').attr("href"); //Get Main Image URL
		var imgDesc = $(this).find('.block').html(); 	//Get HTML of block
		var imgDescHeight = $(".main_image").find('.block').height();	//Calculate height of block	
		
		if ($(this).is(".active")) {  //If it's already active, then...
			return false; // Don't click through
		} else {
			//Animate the Teaser				
			$(".main_image .block").animate({ opacity: 0, marginBottom: -imgDescHeight }, 250 , function() {
				$(".main_image .block").html(imgDesc).animate({ opacity: 0.85,	marginBottom: "0" }, 250 );
				$(".main_image img").attr({ src: imgTitle , alt: imgAlt});
			});
		}
		
		$(".image_thumb ul li").removeClass('active'); //Remove class of 'active' on all lists
		$(this).addClass('active');  //add class of 'active' on this list only
		return false;
		
	}) .hover(function(){
		$(this).addClass('hover');
		}, function() {
		$(this).removeClass('hover');
	});
			
	//Toggle Teaser
	$("a.collapse").click(function(){
		$(".main_image .block").slideToggle();
		$("a.collapse").toggleClass("show");
	});
						   	   
    var pathRoot = 2;
    // Adds a > next to link with class 'readMore'
    $('.readMore').wrap('<div class="readMore"></div>');
    $('.readMore a').removeClass('readMore');
    $('.readMore a').after(' &gt;');
    $(".readMore:has(a)").click(function() {
        window.location = $("a:first", this).attr("href");
    });
    
    // Part of the global fade-in
    $('.fadeIn').css({ 'display': 'none' });
    // to fade-in the image holders
    $('#imgHolder').fadeIn(900);

    // Auto selecting active navigation
    $(function() {
        var path = location.pathname.split("/")[1];
        if (path) {
            $('.nav ul li a[href$="' + path + '"]').attr('class', 'active');
        }
    });
    
});

var currentMargin = 0;
function smoothScroll(dir) {
    var content = $('#verticalContent1');
    var height = content.height();
    var containerHeight = 205;
    var maxMargin = height - containerHeight;

    if (dir == "next") {
        currentMargin += containerHeight;
        if (currentMargin > maxMargin) {
            currentMargin = maxMargin;
        }
    }
    else {
        currentMargin -= containerHeight;
        if (currentMargin < 0) {
            currentMargin = 0;
        }
    }
    if (currentMargin == 0) {
        $('#upArrow').addClass("upDisabled");
    } else {
        $('#upArrow').removeClass("upDisabled");
    }

    if (currentMargin == maxMargin) {
        $('#downArrow').addClass("downDisabled");
    } else {
        $('#downArrow').removeClass("downDisabled");
    }
    content.animate({
        marginTop: "-" + currentMargin + "px"
    }, 1000);
}

/*  up and down slider code */
	var currentPanel = 1;
	var maxPanel = 4;
	function scroll(dir) {
			var next
			if (dir == "next") {
					if (currentPanel == maxPanel) {return;}
	next = currentPanel + 1;
			} else {
					if (currentPanel == 1) {return;}
	next = currentPanel - 1;
			}
			var visiblePanel = $('#verticalContent' + currentPanel);
			var nextPanel = $('#verticalContent' + next);
	
			visiblePanel.slideUp(750);
			nextPanel.slideDown(1000);
	
			currentPanel = next;
	if(currentPanel == 1){
	$('#upArrow').addClass("upDisabled");
	} else {
	$('#upArrow').removeClass("upDisabled");
	}
	
	if(currentPanel == maxPanel){
	$('#downArrow').addClass("downDisabled");
	} else {
	$('#downArrow').removeClass("downDisabled");
	}
	}
		
		
$(document).ready(function(){
	/* This code is executed after the DOM has been completely loaded */
	var totWidth=0;
	var positions = new Array();
	
	$('#slides .slide').each(function(i){
		// Traverse through all the slides and store their accumulative widths in totWidth
		positions[i]= totWidth;
		totWidth += $(this).width();
		// The positions array contains each slide's commulutative offset from the left part of the container
		if(!$(this).width())
		{
			alert("Please, fill in width & height for all your images!");
			return false;
		}
	});
	$('#slides').width(totWidth);

	// Change the cotnainer div's width to the exact width of all the slides combined
	$('#slideThumbs ul li a').click(function(e,keepScroll){
			$('li.menuItem').removeClass('act').addClass('inact');
			$(this).parent().addClass('act');
			var pos = $(this).parent().prevAll('.menuItem').length;	
			// Start the sliding animation
			$('#slides').stop().animate({marginLeft:-positions[pos]+'px'},450);
			// Prevent the default action of the link 
			e.preventDefault();
			// Stopping the auto-advance if an icon has been clicked:
			if(!keepScroll) clearInterval(itvl);
	});
	// On page load, mark the first thumbnail as active
	$('#slideThumbs ul li.menuItem:first').addClass('act').siblings().addClass('inact');
	// Enabling auto-advance
	var current=1;
	function autoAdvance()
	{
		if(current==-1) return false;
		
		$('#slideThumbs ul li a').eq(current%$('#slideThumbs ul li a').length).trigger('click',[true]);	// [true] will be passed as the keepScroll parameter of the click function on line 28
		current++;
	}
	// The number of seconds that the slider will auto-advance in:
	var changeEvery = 10;
	var itvl = setInterval(function(){autoAdvance()},changeEvery*1000);
});
