var CAROUSEL_ITEMS_PER_PAGE = 3;

var homeHeroRotator_defaults = {
	duration: 1000,
	linger: 3000
}

var homeHeroRotator_carousel;

var homeHeroRotator_options;
var homeHeroRotator_dipData;

var homeHeroRotator_rotContainer;
var homeHeroRotator_rotHtmlOverlay;
var homeHeroRotator_rotImage;
var homeHeroRotator_rotBackimage;

var homeHeroRotator_currentItemIdx;
var homeHeroRotator_timer;
var homeHeroRotator_visitedLast = false;

function homeHeroRotator(rotContainer, jsonDipData, options) {
	homeHeroRotator_options = $.extend(homeHeroRotator_defaults, options);
	homeHeroRotator_dipData = jsonDipData;

	homeHeroRotator_rotContainer	= rotContainer.eq(0);
	homeHeroRotator_rotHtmlOverlay	= $('#heroImageHtmlOverlay').eq(0);
	homeHeroRotator_rotImage		= $('#heroImage').eq(0);
	homeHeroRotator_rotBackimage	= $('#heroImageBack').eq(0);

	homeHeroRotator_resetRotator();
} // homeHeroRotator

function homeHeroRotator_resetRotator() {
	var list = $('ul',homeHeroRotator_rotContainer);

	list.empty();
	$(homeHeroRotator_dipData.nodes).each( function(i) {
		list.append('<li><a href="#" id="advanceToFrameButton'+(i+1)+'" class="thumbNailLink"><span class="content"><h6>'+this.node_title+'</h6><img src="/'+this.node_data_field_panel_image_field_panel_image_thumbnail_filepath+'" alt="" width="198" height="88" border="0" /></span></a></li>');
		$('#advanceToFrameButton'+(i+1)).click(function(e){
			homeHeroRotator_view(i);
			e.preventDefault();
		}); // click
	}); // each

	$('ul',homeHeroRotator_rotContainer).jcarousel({
		visible: CAROUSEL_ITEMS_PER_PAGE,
		itemFirstInCallback: function(carousel, item, idx, state) {
			$('.pagination-indicator').removeClass('active');
			$('#carousel-pagination-indicator-'+Math.round(idx / CAROUSEL_ITEMS_PER_PAGE)).addClass('active');
		} // itemFirstInCallback
		,
		initCallback: function(carousel) {
			homeHeroRotator_carousel = carousel;
			var pageCount = Math.ceil(homeHeroRotator_dipData.nodes.length / CAROUSEL_ITEMS_PER_PAGE);
			for(i=0;i < pageCount;i++) {
				$('#promoBubbleList').append(
					$('<a></a>')
						.attr('class','pagination-indicator')
						.attr('id','carousel-pagination-indicator-'+i)
						.attr('ref',i)
						.html('<img src="/sites/all/themes/sram_2011/_images/general-skin/icon-library/spacer.gif" alt="" width="8" height="8" class="inline" />')
						.click( function(e) {
							homeHeroRotator_view($(this).attr('ref')*CAROUSEL_ITEMS_PER_PAGE);
							e.preventDefault();
						} // click
					) // create
				); // append
			} // for
		} // initCallback
	}); // jcarousel

	homeHeroRotator_currentItemIdx = 0;

	//-- setup "on change image" behavior
	jQuery(homeHeroRotator_rotImage).imagesLoaded( function(){
		var cd = homeHeroRotator_dipData.nodes[homeHeroRotator_currentItemIdx];

		$('.thumbNailLink').removeClass('active');
		$('#advanceToFrameButton'+(homeHeroRotator_currentItemIdx+1)).addClass('active');
		homeHeroRotator_carousel.scroll(homeHeroRotator_currentItemIdx);
		if(cd.node_data_field_panel_image_field_panel_video_overlay_filepath) {
			homeHeroRotator_rotHtmlOverlay.html('<a href="#" style="top:0;left:0;" id="videoClickPanel"><img src="/sites/all/themes/sram_2011/_images/general-skin/icon-library/spacer.gif" alt="" width="744" height="418" border="0" /></a>');
			$('#videoClickPanel').bind('click', function() {
				clearInterval(homeHeroRotator_timer);
				homeHeroRotator_rotImage.stop();
				homeHeroRotator_rotImage.css('opacity',1);
				homeHeroRotator_rotHtmlOverlay.html('<div id="mediaspace"></div>');

				var so = new SWFObject('/en/XX/_includes/jw-player-4.4/player-licensed.swf','ply','760','423','9','#ffffff');
				so.addParam('allowfullscreen','true');
				so.addParam('allowscriptaccess','always');
				so.addParam('wmode','opaque');
				so.addVariable('file','/'+cd.node_data_field_panel_image_field_panel_video_overlay_filepath);
				so.addVariable('skin','/en/XX/_includes/jw-player-4.4/modieus-skin.swf');
				so.addVariable('autostart','true');//change this to false to play on click
				so.addVariable('controlbar','over');
				so.addVariable('fullscreen','true');
				so.addVariable('stretching','none');
				so.write('mediaspace');

				return false;
			});
		} else {
			var	coords = cd.node_data_field_panel_link_coordinates_field_panel_link_coordinates.split('x');
			var	dims = cd.node_data_field_panel_link_box_size_field_panel_link_box_size.split('x');

			var aTarget = '_self';
			if(typeof(cd.node_data_field_url_target_blank_field_url_target_blank)!="undefined") {
				if(cd.node_data_field_url_target_blank_field_url_target_blank!='F') {
					aTarget = '_blank';
				} // if
			} // if

			homeHeroRotator_rotHtmlOverlay.html(
				$('<a></a>')
					.attr({
						'href': cd.node_data_field_panel_link_url_field_panel_link_url,
						'target': aTarget
					})
					.click(function() {
						var pageTracker = _gat._getTracker("UA-3351558-1");
						pageTracker._trackEvent('DIP', 'Clicked', cd.node_data_field_panel_link_url_field_panel_link_url)
					})
					.css({
						'top': coords[1]+'px',
						'left': coords[0]+'px',
						'display': 'block'
					})
					.html(
						$('<img></img>')
							.attr('src','/_media/images/common/spacer.gif')
							.css({
								'height': dims[1]+'px',
								'width': dims[0]+'px',
								'display': 'block'
							})
					)
			); // html
		} // if..else

		$(homeHeroRotator_rotImage).fadeTo(homeHeroRotator_options.duration,1);
	});

	//-- setup "on change image" behavior
	jQuery(homeHeroRotator_rotBackimage).imagesLoaded( function(){
		$(homeHeroRotator_rotImage).css('opacity',0);
		homeHeroRotator_present();
	});

	homeHeroRotator_present();
	homeHeroRotator_start();
} // homeHeroRotator_resetRotator

function homeHeroRotator_present() {
	if(!homeHeroRotator_visitedLast) try{
		var pageTracker = _gat._getTracker("UA-3351558-1");
		pageTracker._trackEvent('RotatorStats', 'Home-Page', 'Viewed panel - '+homeHeroRotator_currentItemIdx);
	} catch(err) {}

	//-- setting src fires imagesLoaded behavior
	homeHeroRotator_rotImage.attr('src', '/'+homeHeroRotator_dipData.nodes[homeHeroRotator_currentItemIdx].node_data_field_panel_image_field_panel_image_filepath);
} // homeHeroRotator_present

function homeHeroRotator_step() {
	homeHeroRotator_visitedLast = (homeHeroRotator_dipData.nodes.length==(homeHeroRotator_currentItemIdx+1)) || homeHeroRotator_visitedLast;
	homeHeroRotator_currentItemIdx = (homeHeroRotator_currentItemIdx+1) % homeHeroRotator_dipData.nodes.length;

	//-- setting src fires imagesLoaded behavior
	$(homeHeroRotator_rotBackimage).attr('src', homeHeroRotator_rotImage.attr("src"));
} // homeHeroRotator_step

function homeHeroRotator_start() {
	homeHeroRotator_currentItemIdx = 0;
	homeHeroRotator_timer = window.setInterval(homeHeroRotator_step, homeHeroRotator_options.linger+homeHeroRotator_options.duration);
} // homeHeroRotator_start

function homeHeroRotator_view(idx) {
	clearInterval(homeHeroRotator_timer);
	homeHeroRotator_rotImage.stop();
	homeHeroRotator_rotImage.css('opacity',1);
	homeHeroRotator_currentItemIdx = idx;

	homeHeroRotator_present();
} // homeHeroRotator_view

