$(document).ready(function() {
	$('a.youtube > img').each(function(){
	$('<span style="height:'+$(this).height()+'px;"></span>').insertBefore(this);
	});
	$(".eq").equalHeights();
	if($.fn.simplerotator){
		/*$('div.home-scroll').simplerotator()
			.bind('rotator.change',function(e,v,$p){
		});		
		$('div.cs-scroll').simplerotator()
			.bind('rotator.change',function(e,v,$p){
		});*/		
		$('#scroller').cycle({
			timeout: 4000
		});
		$('#scroller.cs-scroll').cycle({
			timeout: 3000
		});
		$('div.home-scroll').cycle({
			timeout: 4000
		});
		$('.pause').click(function() { 
			$('#scroller').cycle('toggle');
		});
		$('.previous').click(function() { 
			$('#scroller').cycle('prev');
		});
		$('.next').click(function() { 
			$('#scroller').cycle('next');
		});

	}
	$("a.casestudy").fancybox({
		padding: 0,
		hideOnContentClick : true,
		titleShow : false,
		overlayOpacity: 0.8,
		overlayColor : 	'#000',
		height: 276,
		width: 541,
		type: 'iframe',
		autoDimensions : false,
		centerOnScroll: false,
		onStart: function() {
			$('#scroller').cycle('pause');
		},
		onComplete: function() {
			//$('#fancybox-wrap').css({'padding': '0 20px 0 207px', 'position': 'fixed'})
			//$('#fancybox-wrap').css("cssText", "top: 148px !important;");
		},
		onClosed: function() {$('#scroller').cycle('resume');}
	});
	$("a.casestudyleft").fancybox({
		padding: 0,
		hideOnContentClick : true,
		titleShow : false,
		overlayOpacity: 0.8,
		overlayColor : 	'#000',
		height: 276,
		width: 531,
		autoDimensions : false,
		onStart: function() {$('#scroller').cycle('pause');},
		onClosed: function() {$('#scroller').cycle('resume');}
	});
	$("a.diagram").fancybox({
		padding: 0,
		hideOnContentClick : true,
		titleShow : false,
		overlayOpacity: 0.9,
		overlayColor : 	'#14130f',
		autoDimensions : true
	});
	$("a.youtube,.video>a").click(function() {
		$.fancybox({
			padding			: 0,
			autoScale		: false,
			titleShow		 : false,
			overlayOpacity: 0.8,
			overlayColor : 	'#000',
			transitionIn	: 'none',
			transitionOut	: 'none',
			title			: this.title,
			width			: 541,
			href			: this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
			type			: 'swf',
			swf				: {
			wmode				: 'transparent',
			allowfullscreen	: 'true'
			}
		});

		return false;
	});
	if (location.href.search("/pragma-chat")!=-1) {
		$('<a href="/rss.xml" ><img src="/assets/images/rss.gif" alt="rss feed" name="rss" width="16" height="16" id="rss" /></a>').insertAfter('#addthis');
	}
	if(typeof tagtxt!="undefined")
 {
		//alert(tagtxt);
		document.title=tagtxt;
		var myText = $('#breadshare');
		myText.html(myText.html().replace('filtered',tagtxt));
	}
//		onComplete : function() {
//			$("#fancybox-wrap").css({'top': '0', 'padding': '0 20px 0 0', 'position': 'absolute'});
//		}
 });

(function($) {
	$.fn.equalHeights = function(minHeight, maxHeight) {
		tallest = (minHeight) ? minHeight : 0;
		this.each(function() {
			if($(this).height() > tallest) {
				tallest = $(this).height();
			}
		});
		if((maxHeight) && tallest > maxHeight) tallest = maxHeight;
		return this.each(function() {
			//$(this).height(tallest).css("overflow","auto");
			$(this).height(tallest);
		});
	}
})(jQuery);


// BANNER ROTATOR
(function($){

	$.fn.simplerotator = function(opts){
		
		opts = $.extend({
			interval:	4000,
			speed:		500,
			content:	'div.scroll-item',
			start:		7,
			menu:		null
		},opts);
		
		return $(this).each(function(){
			
			var $this 		= $(this),
				$content 	= $this.find(opts.content),
				num			= $content.length,
				active		= opts.start,
				paused		= false,
				timer;
			opts.start = num-1;
			
			
			// set the initial show/hide state
			$content.hide().eq(opts.start).show();
			
			// expose api
			$this.bind('rotator.set',function(e,v){change(v);});
			
			// set to pause on hover

			//$content.mouseover(function(){clearTimeout(timer);});
			//$content.mouseout(function(){timer=setTimeout(function(){change();},opts.interval);});
			
			// if rotator includes a menu, define input/outputs
			if(opts.menu != null) {
				var $menu = $(opts.menu);
				// add click events
				$menu.find('li a').each(function(n){
					$(this).click(function(){
						change(n);
						return false;
					});
				});
				
				// set selected state on change
				$this.bind('rotator.change',function(e,v){
					$menu.find('li').removeClass('selected').eq(v).addClass('selected');
				});
			}
			
			// skip to next slide		
			$('.next').bind('click', function() {
				change();
			});

			// skip to next slide		
			$('.previous').bind('click', function() {
				change(active-1);
			});

			// pause		
			$('.pause').bind('click', function() {
				if(paused==true) {
					paused=false;
					timer=setTimeout(function(){change();},opts.interval);
				} else {
					clearTimeout(timer);
					paused=true;
				}
			});

			
			// start timer
			if(opts.interval) {
				setTimeout(function(){change();},opts.interval);
			}
			
			function change(i) {
				if(typeof i == 'undefined') {
					i = active+1;
				}
				i = i%num;
				
				if(i!=active) {
					clearTimeout(timer);
					$content.eq(active).fadeOut(opts.speed);
					$content.eq(i).fadeIn(opts.speed,function(){
						active=i;
						if(opts.interval) {
							timer=setTimeout(function(){change();},opts.interval);
						}
					});
					$this.trigger('rotator.change',[i,$content.eq(i)]);
				}
			}
			
		});
		
	}

})(jQuery);

