//-+
// | Description: My jQuery
//-+
$(document).ready(function() {
	if ($('.section_text').length != 0) {
		$('.section_text').append('<p class="navBtn"><a id="navBtn1" href="javascript:animateNavigation();">Show</a><a id="navBtn2" href="javascript:location.reload(true);">Hide</a> Animated Navigation</p>');
		$("#toc").tableOfContents(
			$(".section_text"),{
				startLevel:2,
				depth:1
			}
		);
		$("#navBtn1").show();
	}
	$(".hidden").hide();
	$('.wg_twitter').hide();
	$("#content_col").click(function(){
		$(".wg_twitter:visible").animate({
			width: "150px"
		}).slideUp(1500);
	});
	$("#content_col").uiforms();
	$(".multiselect").multiselect();
	$(".accordion").accordion({
		autoHeight: false,
		navigation: true,
		collapsible: true,
		active: false
	});
	//$('#h1_box_r_col').after($('#quote_box'));
	if ($('.boxL').length != 0) {
		$(".boxL").corner("top, 10px");
		$(".boxR, .boxL dt, .boxR dt").corner("10px");
	}
	/*if (window.PopEx && $(".popbox").length) {
		$("#content_col img").mouseover(function(){
			//alert('image mouseover');
			PopEx(this,-50,-25,0,200,100,'gallery_hover');
		}).attr({onmouseover:''});
	}*/
	$('#content_col img').each(function() {
		caption = $(this).parent().find('h4').text();
		imgAlt = $(this).attr('alt');
		imgSrc = $(this).attr('src');
		if (caption !== '') {
			title = caption;
		} else {
			title = imgAlt;
		} 
		$(this).wrap('<a href="'+imgSrc+'" title="'+title+'" rel="shadowbox[iaceGallery]"/>');
	});
	$('#content_col a.gallery').each(function() {
		$(this).attr('rel', 'shadowbox[iaceGallery]');
	});
	if (window.Shadowbox) {
		Shadowbox.init({
			counterType: "skip",
			overlayOpacity: 0.8
		});
		$("#content_col img").mouseover(function(){
			$(this).stop().css({
				'cursor':'pointer',
				'border-color':'#000000'
			});
		}).mouseout(function(){
			$(this).stop().css({
				'border-color':'#666666'
			});						 
		});
	}	
	if ($('.sbgroup').length != 0) {
		sbCount = new Array();
		imgCount = new Array();
		sbBorderSpace = new Array();
		imgBorderSpace = new Array();
		sbImgWidthNorm = new Array();
		imgWidthNorm = new Array();
		sbConfig();
		$('.sbgroup').attr('id', function(arr){
			return 'sbgroup' + arr;
		});
		$('.sb.a5 .caption').css({
			top: '100%',
			left: '0px'
		});
		$('.sb.a6 .caption, .sb.a7 .caption').css({
			top: '88%',
			left: '0px'
		});
		$('.sb').hover(function(){
			sbGroupId = $(this).parent().attr('id');
			i = sbGroupId.match(/\d+/);
			sbImg = $('img', this);
			$('h4', this).length != 0 ? sbH4CharCount = $(this).find('h4').text().length : sbH4CharCount = 0;
			$('p', this).length != 0 ? sbPCharCount = $(this).find('p').text().length : sbPCharCount = 0;
			$('h4', this).length != 0 ? sbH4FontSize = $('h4', this).css("font-size").match(/\d+/) : sbH4FontSize = 14;
			$('p', this).length != 0 ? sbPFontSize = $('p', this).css("font-size").match(/\d+/) : sbPFontSize = 12;
			if (sbPCharCount <= 100) {
				sbH4Width = sbH4CharCount*sbH4FontSize*0.667;
				sbPWidth = sbPCharCount*sbPFontSize*0.667/2;
				var sbImgWidth = 0;
				$('img',this).each(function() {
					sbImgWidth += $(this).width();
				});
				if (sbH4Width >= sbImgWidth) {
					sbImgWidthLarge = sbH4Width;
				} else if (sbPWidth >= sbImgWidth) {
					sbImgWidthLarge = sbPWidth;
				} else {
					sbImgWidthLarge = sbImgWidth+1;	
				}
			} else if (sbPCharCount >= 700) {
				sbImgWidthLarge = sbImgWidthNorm[i]*sbCount[i];
			} else {
				sbImgWidthLarge = (sbH4CharCount+sbPCharCount)*sbH4FontSize*0.667/9;
			}
			sbImgWidthSmall = ($('#content_col').width()-sbBorderSpace[i]-sbImgWidthLarge)/(sbCount[i]-1);
			$('#'+sbGroupId+' .sb').stop().animate({
				width:sbImgWidthSmall+'px'
			});
			$(this).stop().animate({
				width:sbImgWidthLarge+'px'
			});
		}, function() {
			$('#'+sbGroupId+' .sb').stop().animate({
				width:sbImgWidthNorm[i]+'px'
			});
		});		
		//To switch directions up/down and left/right just place a "-" in front of the top/left attribute
		//Vertical Sliding
		$('.sb.a1').hover(function(){
			$(".cover", this).stop().animate({top:'100%'});
		}, function() {
			$(".cover", this).stop().animate({top:'0%'});
		});
		//Horizontal Sliding
		$('.sb.a2').hover(function(){
			$(".cover", this).stop().animate({left:'100%'});
		}, function() {
			$(".cover", this).stop().animate({left:'0%'});
		});
		//Diagnal Sliding
		$('.sb.a3').hover(function(){
			$(".cover", this).stop().animate({top:'100%', left:'100%'});
		}, function() {
			$(".cover", this).stop().animate({top:'0%', left:'0%'});
		});
		//Partial Sliding (Only show some of background)
		$('.sb.a4').hover(function(){
			$(".cover", this).stop().animate({top:'40%'});
		}, function() {
			$(".cover", this).stop().animate({top:'0%'});
		});
		//Full Caption Sliding (Hidden to Visible)
		$('.sb.a5').hover(function(){
			$(".cover", this).stop().animate({top:'60%'});
		}, function() {
			$(".cover", this).stop().animate({top:'100%'});
		});
		//Caption Sliding (Partially Hidden to Visible)
		$('.sb.a6').hover(function(){
			$(".cover", this).stop().animate({top:'60%'});
		}, function() {
			$(".cover", this).stop().animate({top:'88%'});
		});
		//Caption Sliding (Partially Hidden to 100% Visible)
		$('.sb.a7').hover(function(){
			$(".cover", this).stop().animate({top:'0%'});
		}, function() {
			$(".cover", this).stop().animate({top:'88%'});
		});
	}
});
function sbConfig() {
	$('.sbgroup').each(function(i) {
		sbCount[i] = $('.sb', this).length;
		sbBorderSpace[i] = sbCount[i] * 3;
		sbImgWidthNorm[i] = ($('#content_col').width()-sbBorderSpace[i])/sbCount[i];
		$('.sb', this).css({
			width:sbImgWidthNorm[i] + 'px'
		});
		$('.sb').each(function(i) {
			imgCount[i] = $('img', this).length;
			imgBorderSpace[i] = imgCount[i] * 3;
			imgWidthNorm[i] = ($('#content_col').width()-imgBorderSpace[i])/imgCount[i];
			if ($('img',this).length > 2) {
				$('img', this).css({
					width:imgWidthNorm[i] + 'px'
				});
			}
		});	
	});	
}
function show_content(id) {
	$(id).slideToggle();
};
function animateNavigation() {
	$(".section_text span").show().click(function(){
		$(".section_text_inner:visible").hide("fast");
		$(".section_text span:hidden").show("fast");
		$(this).next().slideToggle("fast");
		$(this).slideToggle("fast");
		return false;
	}).css({
		'display':'block',
		'width':'375px',
		'padding':'3px',
		'margin':'0px'
	}).next().hide();
	
	$("#toc,#navBtn1").hide();
	$("#show,#navBtn2").show();
	$("#vertical").css({
		'width':'10px',
		'height':'170px',
		'float':'right',
		'position':'absolute',
		'font-size':'12px',
		'line-height':'10px'
	}).hide().html($('#vertical').text().replace(/(.)/g,"$1<br />"));
	
	$(".section_text span").hover(function () {
		$(this).css({
			'color':'#DC1A22',
			'font-weight':'bold',
			'background-color':'#f2f1ed',
			'cursor':'pointer'
		});
	}, function () {
		$(this).css({
			'color':'#000000',
			'font-weight':'normal',
			'background-color':''
		});
	});
	
	$('#faq').makeFAQ({
		indexTitle: "Activity Index",
		displayIndex: true,
		faqHeader: "h3"
	});
	$('#faq2').makeFAQ({
		indexTitle: "FAQ Index",
		displayIndex: true,
		faqHeader: "h3"
	}); 
};
//-+
// | Description: Vertically Align
//-+
(function ($) {
$.fn.vAlign = function() {
	return this.each(function(i){
	var ah = $(this).height();
	var ph = $(this).parent().height();
	var mh = (ph - ah) / 2;
	$(this).css('margin-top', mh);
	});
};
})(jQuery);
//-+
// | Description : Twitter Widget
//-+
function wg_twitter_btn() {
	$(".wg_twitter").slideDown(1500).animate({
		width: "222px"
	});
	scrollHeight = $("#right").height()-250;
	$("html, body").animate({scrollTop:scrollHeight}, "slow");
}
function wg_twitter() {
	new TWTR.Widget({
	  version: 2,
	  type: 'profile',
	  rpp: 50,
	  interval: 6000,
	  width: 'auto',
	  height: 300,
	  theme: {
			shell: {
				background: '#0053a3',
				color: '#ffffff'
			},
			tweets: {
				background: '#E4DFD9',
				color: '#685556',
				links: '#DC1A22'
			}
	  },
	  features: {
			scrollbar: true,
			loop: false,
			live: true,
			hashtags: true,
			timestamp: true,
			avatars: true,
			behavior: 'all'
	  }
	}).render().setUser('iaceacu').start();
}
//-+
// | Description: FAQ
//-+
(function($) { 
    $.fn.makeFAQ = function(options) {
        var defaults = {
            indexTitle: "Index",    // Change to whatever you want to be displayed
            faqHeader: ":header",   // default grabs any header element - h1,h2, etc...
            displayIndex: true      // Display Index
        };
        var options = $.extend(defaults, options);

        return this.each(function() {
            // load the parent object only once
            var $obj = $(this);

            // wrap parent in faqRoot div
            $obj.wrap("<div id='faqRoot'></div>");
            
            // Add index div
            if(options.displayIndex) {
                $obj.before("<div id='faqindex'><h3>" + options.indexTitle + "</h3><ul></ul></div>");
            };

            // Get header children using the obj ID
            var $faqEntries = $obj.children(options.faqHeader);
			// counting integer - ensures unique id names
			var i = 0;
            // enumerate through each entry and perform several tasks
            $faqEntries.each(function () {
                // load object only once
                var $entry = $(this);

                // Get entry name
                var entryName = $entry.text();
                // strip whitespaces and special characters
                var entryNameSafe = entryName.replace(/\W/g,"") + i;
				// Increment counter
                i++;

                // build index line for entry
                var itemHTML = "<li><a id='" + entryNameSafe.toString() + "Index' href='#" + entryNameSafe.toString() + "' >" + entryName + "</a></li>";
                // append the index line to the index
                $('#faqindex ul').append(itemHTML);

                // add click event for index entry
                if(options.displayIndex) {
                    $('#' + entryNameSafe.toString() + 'Index').click( function(){ 
                        // slide down the selected index before jumping to the bookmark    
                        $('#' + entryNameSafe.toString()).next('span').slideDown('fast');
                        // make sure it gets the faqopened class
                        $('#' + entryNameSafe.toString()).addClass('faqopened');
                     });
                };

                // add class to faq entry content
                $entry.next("div").addClass('faqcontent');

                // add title, name and id to entry
                $entry.attr({
                    title: "Click to expand/collapse",
                    name: entryNameSafe,
                    id: entryNameSafe                    
                    })
                    // add class
                    .addClass("faqclosed")
                    
                    // Add click event to entry
                    .click( function() {
                        $entry.next('div').slideToggle('fast');
                        $entry.toggleClass('faqopened');
                        })
                        // Collapse the span tag of the entry
                        .next('div').css({ 
                            display: "none"
                        });
            }); // end enumeration of each faq entry

        }); // end this each
    }; // end function
})(jQuery);
