window.onerror = function(){return true;};
if(jQuery){
	$jQuery = jQuery;
	jQuery.noConflict();
}

window.onload = function() {
	resetSpecialOffersWindowWidth();
	updateScrollInterface(SOWindowList);
};

var intItemWidth = 169;
var intControlOffset = 42;

Effect.Combo = function(element) {
	element = $(element);
	if(element.style.display == 'none') { 
		new Effect.Appear(element, arguments[1] || {}); 
	}else { 
		new Effect.BlindUp(element, arguments[1] || {}); 
	}
}

function findPosX(obj) {
    var curleft = 0;
    if(obj.offsetParent)
        while(1) {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
  }

var intCurrentSpecialOffersScrollerItem = 1;

var SOWindowBase = '';
var SOWindowList = '';
var SOWindowNext = '';
var SOWindowPrev = '';

function initializeSpecialOffersScrollerWindow(baseName) {
	SOWindowBase = baseName;
	SOWindowList = baseName+'_offersWindowList';
	SOWindowNext = baseName+'_offersWindowNext';
	SOWindowPrev = baseName+'_offersWindowPrevious';
	SOWindowCar	 = baseName+'_offersWindowCarouselContainer';
	SOWindowItem = baseName+'_offersWindowItem_';
	updateScrollInterface(SOWindowList);
}

function resetSpecialOffersWindowWidth() {
	var windowWidth;
	windowWidth = (jQuery(window).width() * 0.8) - $("rightColumn").offsetWidth - $("leftNavigationContainer").offsetWidth - 50;
	if(windowWidth < 357) windowWidth = 357;
	
	$("SpecialOffersWindow").style.width = windowWidth + "px";
}




function clearSelectedItem(){	
	var currimage = $(SOWindowItem + intCurrentSpecialOffersScrollerItem).style.backgroundImage;
	if(currimage)
		currimage = currimage.replace("blue", "white");
	$(SOWindowItem + intCurrentSpecialOffersScrollerItem).style.backgroundImage=currimage;
	$(SOWindowItem + intCurrentSpecialOffersScrollerItem).className = "inactive";	
}

function setSOItem(whichItem) {
	var currimage = $(SOWindowItem + intCurrentSpecialOffersScrollerItem).style.backgroundImage;
	currimage = currimage.replace("blue", "white");
	$(SOWindowItem + intCurrentSpecialOffersScrollerItem).style.backgroundImage=currimage;
	$(SOWindowItem + intCurrentSpecialOffersScrollerItem).className = "";
	intCurrentSpecialOffersScrollerItem = whichItem;
	// reload contents of 'detail2' container via AJAX
	
	currimage = $(SOWindowItem + intCurrentSpecialOffersScrollerItem).style.backgroundImage;
	currimage = currimage.replace("white", "blue");
	$(SOWindowItem + intCurrentSpecialOffersScrollerItem).style.backgroundImage=currimage;
	$(SOWindowItem + intCurrentSpecialOffersScrollerItem).className = "active";
	
	//check if the selected item is fully visible; if not, then scroll the view over to show the item
	intCurrentWindowWidth = $(SOWindowCar).offsetWidth - intControlOffset;
	intThisListItemRightXPosition = $(SOWindowList).offsetLeft + (intCurrentSpecialOffersScrollerItem * intItemWidth);
	intThisListItemLeftXPosition = $(SOWindowList).offsetLeft + ((intCurrentSpecialOffersScrollerItem - 1) * intItemWidth);
	if (intThisListItemRightXPosition > intCurrentWindowWidth) {
		new Effect.Move(SOWindowList, {x:(intCurrentWindowWidth - intThisListItemRightXPosition), duration: 0.5});
	} else if (intThisListItemLeftXPosition < 0) {
		new Effect.Move(SOWindowList, {x:(-1 * intThisListItemLeftXPosition), duration: 0.5});
	}
	updateScrollInterface(SOWindowList);
}

function updateScrollInterface(whichControl) {
	if (SOWindowList && whichControl == SOWindowList) {
		intThisWindowListRightXPosition = $(SOWindowList).offsetLeft + (intSpecialOffersWindowListCount * intItemWidth);
		intCurrentWindowWidth = $(SOWindowCar).offsetWidth - intControlOffset;
		if ($(SOWindowList).offsetLeft < 0) {
			$(SOWindowPrev).className = "prevControl";
		} else {
			$(SOWindowPrev).className = "prevControl inactive";
		}
		if (intThisWindowListRightXPosition > intCurrentWindowWidth) {
			$(SOWindowNext).className = "nextControl";
		} else {
			$(SOWindowNext).className = "nextControl inactive";
		}
	}
}

function scrollWindowLeft(whichElement) {
	if (SOWindowList && whichElement == SOWindowList) {
		intThisWindowListRightXPosition = $(SOWindowList).offsetLeft + (intSpecialOffersWindowListCount * intItemWidth);
		intCurrentWindowWidth = $(SOWindowCar).offsetWidth - intControlOffset;
		if (intThisWindowListRightXPosition > intCurrentWindowWidth) {
			if ((intThisWindowListRightXPosition - intCurrentWindowWidth) > intItemWidth) {
				new Effect.Move(whichElement, {x:-intItemWidth, duration: 0.5, afterFinish:function callback(obj){updateScrollInterface(SOWindowList);}});
			} else {
				new Effect.Move(whichElement, {x:(intCurrentWindowWidth - intThisWindowListRightXPosition), duration: 0.5, afterFinish:function callback(obj){updateScrollInterface(SOWindowList);}});
			}
		}
	}
}

function scrollWindowRight(whichElement) {
	if (whichElement == SOWindowList) {
		intThisWindowListLeftXPosition = $(SOWindowList).offsetLeft;
		if (intThisWindowListLeftXPosition < 0) {
			if (intThisWindowListLeftXPosition < -intItemWidth) {
				new Effect.Move(whichElement, {x:intItemWidth, duration: 0.5,  afterFinish: function callback(obj){updateScrollInterface(SOWindowList);}});
			} else {
				new Effect.Move(whichElement, {x:(-1 * intThisWindowListLeftXPosition), duration: 0.5, afterFinish: function callback(obj){updateScrollInterface(SOWindowList);}});
			}
		}
	}
}


Event.observe(window, 'resize', function() {
	resetSpecialOffersWindowWidth();
	updateScrollInterface(SOWindowList);
});
