/* Mousemove Hover */
function moveBanner(e) {
	var IE = document.all?true:false;
	if (!IE) document.captureEvents(Event.MOUSEMOVE)
	
	if (IE) { // grab the x-y pos.s if browser is IE
		tempX = event.clientX + document.body.scrollLeft + 5;
		tempY = event.clientY + document.body.scrollTop + 25;
	} else {  // grab the x-y pos.s if browser is NS
		tempX = e.pageX + 5;
		tempY = e.pageY - window.pageYOffset + 25;
	}
	tempX=tempX; tempX=tempX+'px';
	tempY=tempY; tempY=tempY+'px';
	
	jQuery(this).find('.hover-message').css('left',tempX);
	jQuery(this).find('.hover-message').css('top',tempY);
}

function moveBannerSpecial(e) {
	
	var IE = document.all?true:false;
	if (!IE) document.captureEvents(Event.MOUSEMOVE)
	
	if (IE) { // grab the x-y pos.s if browser is IE
		tempX = event.clientX + document.body.scrollLeft + 5;
		tempY = event.clientY + document.body.scrollTop + 25;
	} else {  // grab the x-y pos.s if browser is NS
		tempX = e.pageX + 5;
		tempY = e.pageY - window.pageYOffset + 25;
	}
	tempX=tempX; tempX=tempX+'px';
	tempY=tempY; tempY=tempY+'px';
	
	jQuery('#hpAdminListsCarousel-next-outer').css('left',tempX);
	jQuery('#hpAdminListsCarousel-next-outer').css('top',tempY);
	jQuery('#hpAdminListsCarousel-next-outer').css('display',jQuery('.jcarousel-next .hover-message').css('display'));
}

	/**
	 * We use the initCallback callback
	 * to assign functionality to the controls
	 */
	function hpAdminListsCarousel_initCallback(carousel) {
		jQuery('#hpAdminListsCarousel-next').bind('click', function() { carousel.next(); return false; });
		jQuery('#hpAdminListsCarousel-prev').bind('click', function() { carousel.prev(); return false; });
		
		jQuery('.jcarousel-prev').hover(function() { jQuery('.jcarousel-next').removeClass("active"); jQuery(this).addClass("active"); }, function() { jQuery(this).removeClass("active"); } );
		jQuery('.jcarousel-next').hover(function() { jQuery('.jcarousel-prev').removeClass("active"); jQuery(this).addClass("active"); }, function() { jQuery(this).removeClass("active"); } );
		
		if(jQuery('.hover-message').css('position') != 'absolute'){
			jQuery('.jcarousel-prev').mousemove(moveBanner);
			jQuery('.jcarousel-next').mousemove(moveBanner);
			
			jQuery('.adminList-container').after('<div id="hpAdminListsCarousel-next-outer" class="hover-message" style="display:none"><img height="1" width="1" src="/images/spacer.gif"/>Next</div>');
			jQuery('.adminList-container .jcarousel-next').hover(
				function() { jQuery('.jcarousel-prev').removeClass("active"); if(!jQuery(this).hasClass('jcarousel-next-disabled')){ jQuery('#hpAdminListsCarousel-next-outer').css('display','block'); } },
				function() { jQuery(this).removeClass("active"); jQuery('#hpAdminListsCarousel-next-outer').css('display','none'); }
			);
			jQuery('.adminList-container .jcarousel-next').mousemove(moveBannerSpecial);
			jQuery('.adminList-container .jcarousel-next .hover-message').css('visibility','hidden');
		}
		
		return;
	};

	function hpAdminListsCarousel_itemLoadCallback(carousel, state) {
		// Since we get all URLs in one file, we simply add all items
		// at once and set the size accordingly.
		if (state != 'init') {
			/*$('hpAdminListsCarouselDiv').style.height = "auto";
			$('hpAdminListsDiv').style.display = "none";*/
			return;
		}
		adminItemsCarousel = carousel;
		var toIndex = parseInt((noOfItems + 1)/2)
		for (i = 1; i <= toIndex; i++) {
			carousel.add(i, $('adminListCarouselItem_' + i))
		}
		carousel.size(adminListCarouselSize);
	};

	/**
	 * This is the callback function which receives notification
	 * when an item becomes the first one in the visible range.
	 */
	function hpAdminListsCarousel_itemFirstInCallback(carousel, item, idx, state) {
		//alert("Carouse state: " + state);
		if (state == "init" || newTab) {
			adminItemsPageNo = 1;
		}
		else if (state == "next") {
			adminItemsPageNo = adminItemsPageNo + 1;
			newTab = false;
		}
		else {
			adminItemsPageNo = adminItemsPageNo - 1;
			newTab = false;
		}
		//alert("Page No:" + adminItemsPageNo);
	};

	/**
	 * This is the callback function which receives notification
	 * when an item becomes the last one in the visible range.
	 */
	function hpAdminListsCarousel_itemLastInCallback(carousel, item, idx, state) {
		//alert("idx: " + idx + " columns: " + columns + " noOfItems: " + noOfItems + " totalAdminListItems:" + totalAdminListItems);
		if ((((idx + columns) * 2) > noOfItems) && (noOfItems < totalAdminListItems)) {
			//alert("getting more users..");
			if(adminListId == 'all') {
				getAllProducts(type, adminItemsPageNo + 1, 8);
			} else {
				getAdminItems(adminItemsPageNo + 1, 8);
			}
		}
		return false;
	};

	/**
	 * This is the callback function which receives notification
	 * when an item becomes the first one in the visible range.
	 * Triggered before animation.
	 */
	function hpAdminListsCarousel_itemVisibleInCallback(carousel, item, idx, state) {
		jQuery(item).find('.thumbnail-primaryHref').each(function(index, obj) {
			smartTrunc(this,32,12);
		});
		if (state != 'init') {
			jQuery('img', item).fadeIn('slow');
		}
	};

	/**
	 * This is the callback function which receives notification
	 * when an item is no longer the first one in the visible range.
	 * Triggered before animation.
	 */
	function hpAdminListsCarousel_itemVisibleOutCallbackBeforeAnimation(carousel, item, idx, state) {
		jQuery('img', item).fadeOut('slow');
	};

	function reinitializeAdminListCarousel() {
		adminItemsPageNo = 1;
		noOfItems = 0;
		totalAdminListItems = 0;
		adminListCarouselSize = 0;
		adminItemsCarousel.scroll(1, false);
		adminItemsCarousel.list.empty();
	}

	function changeLink(link) {
		newTab = true;
		activeLink = link;
		type = link;
		/*Re-initialize a carousel for the new admin list*/
		reinitializeAdminListCarousel();

		//sfPageView('/usage/carousel/ChangeLink/tab-' + activeTabName + '/link-' + link);
		if(link == 'featured' && adminListId != 'all') {
			getAdminItems(1, 16);
		} else {
			getAllProducts(type, 1, 16);
		}

		selectLink(link);
		newTab = false;
	}

	var activeTab = null;
	var activeLink = null;
	function showAdminList(tabId, tabName) {
		newTab = true;
		adminListId = tabId;
		activeTab = tabId;
		activeTabName = tabName;
		activeLink = 'featured';

		// remove links
		$('adminList-headline-links').style.display='none';
		$('adminList-headline-links-default').style.display='none';
		$('adminList-headline-links-default-featured').style.display='none';

		/*Re-initialize a carousel for the new admin list*/
		reinitializeAdminListCarousel();
		
		if(activeTab != null){selectTab(activeTab);}

		if(tabId == 'all') {
			type = 'popular';
			activeLink = 'popular';
			getAllProducts(type, 1, 16);
		} else {
			getAdminItems(1, 16);
		}

		newTab = false;
	}

	function displayAdminListTab(activeTab, activeLink) {
		// display the headline and footer
		$("adminList-headline").innerHTML = $('headline').value;
		if($('footer') == null || $('footer').value == '') {
			$('adminList-footer').innerHTML = $('adminList-footer-default').innerHTML;
		} else {
			$('adminList-footer').innerHTML = $('footer').value;
		}
		// use default on the links?
		if (adminListId == "all") {
			// also show links
			$('adminList-headline-links-default').style.display='inline';
			selectLink(activeLink);
		}
		else if($('links') == null || $('links').value == '') {
			$('adminList-headline-links').style.display='none';
			$('adminList-headline-links-default-featured').style.display='inline';
			$('adminList-headline-links-default').style.display='inline';
			selectLink(activeLink);
		} else {
			$('adminList-headline-links').innerHTML = $('links').value;
			$('adminList-headline-links-default-featured').style.display='none';
			$('adminList-headline-links-default').style.display='none';
			$('adminList-headline-links').style.display='inline';
		}
	}

	function getAllProducts(type, pageNo, pageSize) {
		var url = "/homeAdminItems.html?action=getProducts" + "&adminListId=" + adminListId + "&pageNo=" + pageNo + "&pageSize=" + pageSize + "&type=" + type;
		var pars =  null;
		var myAjax = new Ajax.Request(url, {method: 'get', parameters: pars, onLoading: showLoad, onComplete: allProductsResp} );
	}

	function allProductsResp(res) {
		var data = res.responseText;
		$('adminListDataDiv').innerHTML = data;
		//alert(data);
		populateCarousel(adminItemsCarousel);
		selectTab(activeTab);
		selectLink($('type').value);
		$("adminList-headline").innerHTML = $('headline').value;
	}

	function getAdminItems(pageNo, pageSize) {
		var url = "/homeAdminItems.html?action=getAdminListItems" + "&adminListId=" + adminListId + "&pageNo=" + pageNo + "&pageSize=" + pageSize;
		var pars = null;
		var myAjax = new Ajax.Request(url, {method: 'get', parameters: pars, onLoading: showLoad, onComplete: moreAdminItemsResp} );
	}

	function moreAdminItemsResp(res) {
		var data = res.responseText;
		$('adminListDataDiv').innerHTML = data;
		//alert(data);
		populateCarousel(adminItemsCarousel);
	}

	function populateCarousel(carousel) {
		var startIdx = parseInt($('fromIndex').value);
		var count = parseInt($('noOfItems').value);

		noOfItems = noOfItems + count;
		totalAdminListItems = parseInt($('totalAdminListItems').value);
		//alert("total: " + totalAdminListItems);
		var toIndex = startIdx +  parseInt((count+ 1)/2);
		for (i = startIdx; i <= toIndex; i++) {
			carousel.add(i, $('adminListCarouselItem_' + i));
		}

		adminListCarouselSize = parseInt((totalAdminListItems + 1)/2);
		carousel.size(adminListCarouselSize);
		//alert("caousel size: " + adminListCarouselSize);

		displayAdminListTab(activeTab, activeLink);
	}

	function showLoad () {

	}
