//2012-02-22, shayes5: this tracking code is for a campaign and can be removed once it's finished. Also remove <noscript> tag in homepage2012 template.
var ebRand = Math.random()+'';
ebRand = ebRand * 1000000;
document.write('<scr'+'ipt src="HTTP://bs.serving-sys.com/BurstingPipe/ActivityServer.bs?cn=as&amp;ActivityID=169520&amp;rnd=' + ebRand + '"></scr' + 'ipt>');

jQuery(document).ready(function($){
	//Galleries
	var galleries = $('.gallery ul');
	
	galleries.each(function(i, gallery){
		gallery = $(gallery);
		
		var imgs = gallery.find('img');
		var img, img_jq, new_height, new_width;
		var gallery_height = gallery.height();
		var gallery_width = gallery.width();
		var ratio = gallery_width/gallery_height;
		
		var current_image = $(gallery.find('a')[0]);
		var maximized = false;
		
		imgs.each(function(j, img_el){
			//scale and center images
			img = new Image();
			img.src = img_el.src;/* webkit hack to get real width & height instead of 0 for images with display:none; */
			img_jq = $(img_el);
			
			new_height = gallery_height;
			new_width = gallery_width;
			
			if (img.width / img.height > ratio){
				new_width = img.width * gallery_height / img.height;
				img_jq.css('margin-left', (gallery_width-new_width)/2);
			}
			else{
				new_height = img.height * gallery_width / img.width;
				img_jq.css('margin-top', (gallery_height-new_height)/2);
			}
			img_jq.height(new_height);
			img_jq.width(new_width);
		});
		
		var enlarge_button = $('<a class="arrowlink enlarge">Enlarge</a>').insertAfter(gallery);
		enlarge_button.click(function(){
			current_image.click();
		});
		
		var controls = $('<div class="controls"><span class="previous"></span><span class="playpause"></span><span class="next"></span></div>').insertAfter(gallery);
		controls.find('.playpause').click(function(){
			gallery.cycle('toggle', true);
		});
		
		//enable FancyBox for each linked image in the gallery
		//http://fancybox.net/api
		gallery.find('a').fancybox({
			centerOnScroll: true,
			overlayShow: false,
			titleShow: false,
			onClosed: function(){
				$('iframe').css('visibility', '');
				maximized = false;
				$('#fancybox-wrap').find('.controls').remove();
			},
			onStart: function(){
				if (!maximized){
					$('iframe').css('visibility', 'hidden');
					//only pause gallery when opened for first time (to prevent pasuing after user presses play)
					gallery.cycle('pause');
					
					//the cycle buttons are cloned (along with their events),
					//and displayed on the FancyBox.
					controls.clone(true).appendTo($('#fancybox-wrap'));
					
					maximized = true;
				}
			}
		});
		
		//a callback used to change the pause/play icon
		var playPauseCallback = function(symbol_key){
			var playpause = controls.find('.playpause');
			var cssClass = 'play-icon';
			var func = symbol_key == 'play' ? 'addClass' : 'removeClass';
			return function(cont, opts, byHover){
				if (byHover == undefined || !byHover){
					//the play/pause button on the fancybox has to be retrieved dynamically since it can change
					var playpause2 = $('#fancybox-wrap').find('.playpause');
					//console.debug('playPauseCallback', playpause, playpause2);
					
					playpause[func]('play-icon');
					playpause2[func]('play-icon');
				}
			}
		}
		
		//enable the jQuery Cycle plugin for the gallery
		//http://jquery.malsup.com/cycle/options.html
		gallery.cycle({
			fx: 'fade',
			prev: controls.find('.previous'),
			next: controls.find('.next'),
			width: gallery.width(),
			before: function(currSlideElement, nextSlideElement, options, forwardFlag){
				//updates the href of the enlarge button with the url for the current image
				
				current_image = $(nextSlideElement).find('a');
				//if the fancy box is open, switch to the next image by clicking on it
				if (maximized){
					current_image.click();
				}
			},
			paused: playPauseCallback('play'),
			resumed: playPauseCallback('pause')
		});
	});
	
	
	//Features
	var features = $('#features');
	var feature_divs = features.find('div');
	var feature_nav = $('#feature-nav');
	var feature_nav_dots = feature_nav.find('ul li a');
	var currentClass = 'current';
	
	var getFeatureDivIndex = function(feature){
		for (var i = 0; i < feature_divs.length; i++){
			if (feature_divs[i] == feature){
				return i;
			}
		}
		return null;
	}
	var getFeatureDotIndex = function(feature_nav_dot){
		for (var i = 0; i < feature_nav_dots.length; i++){
			if (feature_nav_dots[i] == feature_nav_dot){
				return i;
			}
		}
		return null;
	}
	
	var cur = getFeatureDotIndex(feature_nav.find('.current')[0]);
	
	//enable the jQuery Cycle plugin for the features
	features.cycle({
		before: function(currSlideElement, nextSlideElement, options, forwardFlag){
			var index = getFeatureDivIndex(nextSlideElement);
			feature_nav_dots.removeClass(currentClass);
			$(feature_nav_dots[index]).addClass(currentClass);
		},
		startingSlide: cur,
		timeout: 7000,
		speed: 2000
	});
	$('#feature').hover(
		function(){
			features.cycle('pause');
		},
		function(){
			features.cycle('resume', false);
		}
	);
	
	feature_nav.find('.previous').click(function(){
		features.cycle('prev');
	});
	feature_nav.find('.next').click(function(){
		features.cycle('next');
	});
	
	feature_nav_dots.each(function(i, e){
		$(e).click(function(){
			features.cycle(i);
		});
	});
});


