$(document).ready(function() {
    var NUM_STORIES_SHOWING = 4;
	var SCROLLABLE_SPEED = 800; //in ms
	var NUM_TOTAL_STORIES;
	var INDEX_FROM;
	var INDEX_TO;
	
	var APPLICATIONCLASS = '#solutionApplicationScroller';
	if($('#solutionApplicationScroller').length == 0)
		APPLICATIONCLASS = '#solutionApplicationScrollerSmall';
	
	

    $('#solutionsCarousel').jcarousel({
		scroll: NUM_STORIES_SHOWING,
		visible: NUM_STORIES_SHOWING,
		animation: SCROLLABLE_SPEED
	});

	NUM_TOTAL_STORIES = $('#solutionsCarousel li').size();
	
	INDEX_FROM = 1;
	
	if(NUM_TOTAL_STORIES <= NUM_STORIES_SHOWING) {
		$(APPLICATIONCLASS +' .jcarousel-prev-disabled-horizontal').css('background-position', '20px 0px').css('width', '12').css('height', '34').css('cursor','default');
		$(APPLICATIONCLASS +' .jcarousel-next-disabled-horizontal').css('background-position', '20px 0px').css('width', '12').css('height','34').css('cursor','default');
	}

	if(NUM_STORIES_SHOWING < NUM_TOTAL_STORIES) {
		INDEX_TO = NUM_STORIES_SHOWING;
	} else {
		INDEX_TO = NUM_TOTAL_STORIES;
	}

	updateSliderIndex();

	$('#solutionsContainer div').hide(); //hide all application content

	//event listeners for applications
	$('#solutionsCarousel > li').each(function() {
		var current = this;
		var currentItem = $(current).attr('id');
		
		$(this).bind('click', function(event) {
			$('#solutionsContainer>div').hide(); //hide any content 

			$("#solutionsContainer #" + currentItem).show();
			$("#solutionsContainer #" + currentItem + " div").show();

		});
	});


	$(APPLICATIONCLASS +' .jcarousel-prev-horizontal').bind('click', function(event) {
		var modRemainingStories = NUM_TOTAL_STORIES % NUM_STORIES_SHOWING;
			
		if(((INDEX_FROM - NUM_STORIES_SHOWING) >=1) && (INDEX_TO != NUM_TOTAL_STORIES)) { //at every end, going backwards now			
			INDEX_FROM -= NUM_STORIES_SHOWING;
			INDEX_TO -= NUM_STORIES_SHOWING;
			updateSliderIndex();
		} else {
			/* TODO: adjust scroll items when previous is clicked from the very end
			if((INDEX_TO == NUM_TOTAL_STORIES) && (modRemainingStories != 0)) {
				$('#solutionsCarousel').jcarousel({
						scroll: modRemainingStories,
						buttonPrevCallback: testFunction()
				});
			} else {
				$('#solutionsCarousel').jcarousel({
						scroll: NUM_STORIES_SHOWING,
				});
			}
			*/
			if(INDEX_FROM > 1) {
				INDEX_FROM -= modRemainingStories;
				INDEX_TO -= modRemainingStories;		
				updateSliderIndex();
			}
		}

		
		});

	$(APPLICATIONCLASS +' .jcarousel-next-horizontal').bind('click', function(event) {
		if((INDEX_TO + NUM_STORIES_SHOWING) <= NUM_TOTAL_STORIES ) { //normal forward
			INDEX_TO += NUM_STORIES_SHOWING;
			INDEX_FROM += NUM_STORIES_SHOWING;
			updateSliderIndex();
		} else { //handle odd number of applications
			var modRemainingStories = NUM_TOTAL_STORIES % NUM_STORIES_SHOWING;
			if(INDEX_TO != NUM_TOTAL_STORIES) {
				INDEX_FROM += modRemainingStories;
				INDEX_TO += modRemainingStories;
				updateSliderIndex();
			}
		}
		
	});


	var indexStart; //used to calculate where user is:  1-4 of 9
    var indexEnd; //used to calculate where user is:  1-4 of 9
/*
    $(APPLICATIONCLASS +'').scrollable({
		hoverClass: 'hover', 
		size: NUM_STORIES_SHOWING,
		speed: SCROLLABLE_SPEED,
		clickable: false,
		onSeek: function() {
            updateSliderIndex(this.getVisibleItems())
           
		}
    }).mousewheel();
	


	updateSliderIndex($(APPLICATIONCLASS +'').scrollable().getVisibleItems()); //update the slider index (1-4 of 9);
  */  
    function updateSliderIndex() {

       $('#solutionsSliderIndex').text(INDEX_FROM + ' to ' + INDEX_TO + ' of ' + NUM_TOTAL_STORIES);
        
    }
	
	var lastClickedSolution;
	$(APPLICATIONCLASS+" #solutionsCarousel li").click(function() { //when a application is clicked on..
		$('#solutionsContainer>div').hide(); //hide any content 
		
		var currentItem = $(this).attr('id'); 
		var solutionApplicationContainer = 'div.' + APPLICATIONCLASS;

		ntptEventTag('solutionApplication=' + currentItem)//UNICA CALL
		trackOmnitureLink(currentItem) //OMNITURE

		$(APPLICATIONCLASS +' #solutionsCarousel li').removeClass('active'); //anything active? reset all of them

		if(lastClickedSolution != null) {
			$('li[id=' + lastClickedSolution + '] div p.solutionApplicationTitle').css('color', '#E00009'); //change the active title to red
		}

		if(NUM_TOTAL_STORIES <= NUM_STORIES_SHOWING) {
			$(APPLICATIONCLASS +' #solutionsCarousel div.solutionApplicationSmall p.solutionApplicationTitle').css('color', '#E00009');	//change the app titles back to red
		} else {
			$(APPLICATIONCLASS +' #solutionsCarousel div.solutionApplication p.solutionApplicationTitle').css('color', '#E00009');	//change the app titles back to red
		}
 
		$('li[id=' + currentItem + ']').addClass('jcarousel-item active');
		$('li[id=' + currentItem + '] div p.solutionApplicationTitle').css('color', '#000000'); //change the active title to black	
 
		$("#solutionsContainer #" + currentItem).show();
		$("#solutionsContainer #" + currentItem + " div").show();
		lastClickedSolution = currentItem;
	});

	//handle deep linking
	var activeSolutionOnLoad = $.query.get('solutionId');
	
	if(activeSolutionOnLoad == '') {
		$(APPLICATIONCLASS + " #solutionsCarousel li:first").click();
	} else {
		var apps = $('#solutionsCarousel li');
		var appFound = false; //handle malformed id's
		$.each( apps , function(i, l){		
			if($(l).attr('id') == activeSolutionOnLoad) { 
				appFound = true;
				var numScrolls = Math.floor( i / NUM_STORIES_SHOWING );  
				switch(numScrolls) {
					case (numScrolls = 2):
						$(APPLICATIONCLASS + ' .jcarousel-next-horizontal').click();
						setTimeout ( "$(APPLICATIONCLASS + ' .jcarousel-next-horizontal').click()", 1000 );
						break;
					case (numScrolls = 1):
						$(APPLICATIONCLASS + ' .jcarousel-next-horizontal').click()
						break;
				}

				$(APPLICATIONCLASS + ' #solutionsCarousel #' + activeSolutionOnLoad).click(); //highlight it
				
			} //end if
		}); //end each
		if(!appFound) {
			$(APPLICATIONCLASS + " #solutionsCarousel li:first").click();
		}
	}




	function getFirstApplication(visibleItems) {
        var firstItemIndex;
		visibleItems.each(function(index) {
            if(index == 0) {
                var temp = $(this).attr('id').split('_');
                firstItemIndex = temp[1];
			}
          });
		return firstItemIndex;
	}

	function trackOmnitureLink(linkEvent) {
		var accountName;
		if(window.location.hostname == 'www.polycom.ca') {
			if((window.location.pathname).indexOf('/ca_fr/') != -1) { 
				accountName = 'polycomwww.polycom.ca.fr,polycomglobal.public2';
			} else {	
				accountName = 'polycomwww.polycom.ca,polycomglobal.public2';
			}
		} else {
			accountName = 'polycom' + window.location.hostname + ',polycomglobal.public2';
		}

		var s=s_gi(accountName);
		s.linkTrackVars='eVar9,eVar10,events'; 
		s.linkTrackEvents='event10'; 
		s.eVar9=linkEvent;
		s.eVar10='solutionApplication';
		s.events='event10';
		if((window.location.pathname).indexOf('/iw/') == -1) { //exclude TS traffic
			s.tl(s,'o','appEvent'); 
		}

	}
}); //end document.ready



