window.onerror = function(){return true;};
if(typeof(jQuery) != "undefined"){
	$jQuery = jQuery;
	jQuery.noConflict();
}

var intItemWidth = 162;
var intControlOffset = 41;

Effect.Combo = function(element) {
	element = $(element);
	if(element.style.display == 'none') { 
		new Effect.Appear(element, arguments[1] || {}); 
	}else { 
		new Effect.BlindUp(element, arguments[1] || {}); 
	}
	if (PtgWindowBase && element.id == PtgWindowBase+"_detail1") {
		setButton(PtgWindowBase+"_tabControl");
	} else {
		setButton(TtgWindowBase+"_tabControl");
	}
}

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;
  }

function setButton(whichButtonID) {
	if ($(whichButtonID).className == "down") {
		$(whichButtonID).className = "up";
	} else {
		$(whichButtonID).className = "down";
	}
}

var intCurrentPtgItem = 0;
var intCurrentTtdItem = 0;
var PtgWindowBase = '';
var PtgWindowList = '';
var PtgWindowNext = '';
var PtgWindowPrev = '';

var TtgWindowBase = '';
var TtgWindowList = '';
var TtgWindowNext = '';
var TtgWindowPrev = '';

function initializePtgWindow(baseName) {	//baseName will be eitehr FeaturedLocations or FeaturedActivities
	intCurrentPtgItem = 1;
	PtgWindowBase = baseName;
	PtgWindowList = baseName+'_ptgWindowList';
	PtgWindowNext = baseName+'_ptgWindowNext';
	PtgWindowPrev = baseName+'_ptgWindowPrevious';	
	PtgWindowCar = baseName+'_ptgWindowptgCarouselContainer';
	PtgWindowItem = baseName+'_ptgWindowItem_';
	updateScrollInterface(PtgWindowList);
	var currimage = $(PtgWindowItem + intCurrentPtgItem).style.backgroundImage;
	currimage = currimage.replace("grey", "blue");
	$(PtgWindowItem + intCurrentPtgItem).style.backgroundImage=currimage;
	$(PtgWindowItem + intCurrentPtgItem).className = "active";
}

function initializeTtdWindow(baseName) {
	intCurrentTtdItem = 1;
	TtgWindowBase = baseName;
	TtgWindowList = baseName+'_ptgWindowList';
	TtgWindowNext = baseName+'_ptgWindowNext';
	TtgWindowPrev = baseName+'_ptgWindowPrevious';
	TtgWindowCar = baseName+'_ptgWindowptgCarouselContainer';	
	TtgWindowItem = baseName+'_ptgWindowItem_';
	updateScrollInterface(TtgWindowList);
	var currimage = $(TtgWindowItem + intCurrentTtdItem).style.backgroundImage;
	currimage = currimage.replace("grey", "blue");
	$(TtgWindowItem + intCurrentTtdItem).style.backgroundImage=currimage;
	$(TtgWindowItem + intCurrentTtdItem).className = "active";
}

function setItem(baseName, whichItem) {
	if(PtgWindowBase && baseName == PtgWindowBase)
		setPtgItem(whichItem);
	else
		setTtdItem(whichItem);
}

function setPtgItem(whichItem) {
	var currimage = $(PtgWindowItem + intCurrentPtgItem).style.backgroundImage;
	currimage = currimage.replace("blue", "grey");
	$(PtgWindowItem + intCurrentPtgItem).style.backgroundImage=currimage;
	$(PtgWindowItem + intCurrentPtgItem).className = "";
	intCurrentPtgItem = whichItem;
	// reload contents of 'detail1' container via AJAX
	
	currimage = $(PtgWindowItem + intCurrentPtgItem).style.backgroundImage;
	currimage = currimage.replace("grey", "blue");
	$(PtgWindowItem + intCurrentPtgItem).style.backgroundImage=currimage;
	$(PtgWindowItem + intCurrentPtgItem).className = "active";
	
	//check if the selected item is fully visible; if not, then scroll the view over to show the item
	intCurrentWindowWidth = $(PtgWindowCar).offsetWidth - intControlOffset;
	intThisListItemRightXPosition = $(PtgWindowList).offsetLeft + (intCurrentPtgItem * intItemWidth);
	intThisListItemLeftXPosition = $(PtgWindowList).offsetLeft + ((intCurrentPtgItem - 1) * intItemWidth);
	if (intThisListItemRightXPosition > intCurrentWindowWidth) {
		new Effect.Move(PtgWindowList, {x:(intCurrentWindowWidth - intThisListItemRightXPosition), duration: 0.5});
	} else if (intThisListItemLeftXPosition < 0) {
		new Effect.Move(PtgWindowList, {x:(-1 * intThisListItemLeftXPosition), duration: 0.5});
	}
	updateScrollInterface(PtgWindowList);
	if ($(PtgWindowBase+'_detail1').style.display == "none") {
		Effect.Combo(PtgWindowBase+'_detail1');
	}
}

function setTtdItem(whichItem) {
	var currimage = $(TtgWindowItem + intCurrentTtdItem).style.backgroundImage;
	currimage = currimage.replace("blue", "grey");
	$(TtgWindowItem + intCurrentTtdItem).style.backgroundImage=currimage;
	$(TtgWindowItem + intCurrentTtdItem).className = "";
	intCurrentTtdItem = whichItem;
	// reload contents of 'detail2' container via AJAX
	
	currimage = $(TtgWindowItem + intCurrentTtdItem).style.backgroundImage;
	currimage = currimage.replace("grey", "blue");
	$(TtgWindowItem + intCurrentTtdItem).style.backgroundImage=currimage;
	$(TtgWindowItem + intCurrentTtdItem).className = "active";
	
	//check if the selected item is fully visible; if not, then scroll the view over to show the item
	intCurrentWindowWidth = $(TtgWindowCar).offsetWidth - intControlOffset;
	intThisListItemRightXPosition = $(TtgWindowList).offsetLeft + (intCurrentTtdItem * intItemWidth);
	intThisListItemLeftXPosition = $(TtgWindowList).offsetLeft + ((intCurrentTtdItem - 1) * intItemWidth);
	if (intThisListItemRightXPosition > intCurrentWindowWidth) {
		new Effect.Move(TtgWindowList, {x:(intCurrentWindowWidth - intThisListItemRightXPosition), duration: 0.5});
	} else if (intThisListItemLeftXPosition < 0) {
		new Effect.Move(TtgWindowList, {x:(-1 * intThisListItemLeftXPosition), duration: 0.5});
	}
	updateScrollInterface(TtgWindowList);
	if ($(TtgWindowBase+'_detail1').style.display == "none") {
		Effect.Combo(TtgWindowBase+'_detail1');
	}
}


function updateScrollInterface(whichControl) {
	if (PtgWindowList && whichControl == PtgWindowList) {
		intThisWindowListRightXPosition = $(PtgWindowList).offsetLeft + (intPtgWindowListCount * intItemWidth);
		intCurrentWindowWidth = $(PtgWindowCar).offsetWidth - intControlOffset;
		if ($(PtgWindowList).offsetLeft < 0) {
			$(PtgWindowPrev).className = "";
			$(PtgWindowPrev).className = "prevControl";
		} else {
			$(PtgWindowPrev).className = "";
			$(PtgWindowPrev).className = "prevControl inactive";
		}
		if (intThisWindowListRightXPosition > intCurrentWindowWidth) {
			$(PtgWindowNext).className = "";
			$(PtgWindowNext).className = "nextControl";
		} else {
			$(PtgWindowNext).className = "";
			$(PtgWindowNext).className = "nextControl inactive";
		}
	} else {
		intThisWindowListRightXPosition = $(TtgWindowList).offsetLeft + (intTtdWindowListCount * intItemWidth);
		intCurrentWindowWidth = $(TtgWindowCar).offsetWidth - intControlOffset;
		if ($(TtgWindowList).offsetLeft < 0) {
			$(TtgWindowPrev).className = "prevControl";
		} else {
			$(TtgWindowPrev).className = "prevControl inactive";
		}
		if (intThisWindowListRightXPosition > intCurrentWindowWidth) {
			$(TtgWindowNext).className = "nextControl";
		} else {
			$(TtgWindowNext).className = "nextControl inactive";
		}
	}
}

function scrollWindowLeft(whichElement) {
	if (PtgWindowList && whichElement == PtgWindowList) {
		intThisWindowListRightXPosition = $(PtgWindowList).offsetLeft + (intPtgWindowListCount * intItemWidth);
		intCurrentWindowWidth = $(PtgWindowCar).offsetWidth - intControlOffset;
		if (intThisWindowListRightXPosition > intCurrentWindowWidth) {
			if ((intThisWindowListRightXPosition - intCurrentWindowWidth) > intItemWidth) {
				new Effect.Move(whichElement, {x:-intItemWidth, duration: 0.5, afterFinish:function callback(obj){updateScrollInterface(PtgWindowList);}});
			} else {
				new Effect.Move(whichElement, {x:(intCurrentWindowWidth - intThisWindowListRightXPosition), duration: 0.5, afterFinish:function callback(obj){updateScrollInterface(PtgWindowList);}});
			}
		}
	} else {
		intThisWindowListRightXPosition = $(TtgWindowList).offsetLeft + (intTtdWindowListCount * intItemWidth);
		intCurrentWindowWidth = $(TtgWindowCar).offsetWidth - intControlOffset;
		if (intThisWindowListRightXPosition > intCurrentWindowWidth) {
			if ((intThisWindowListRightXPosition - intCurrentWindowWidth) > intItemWidth) {
				new Effect.Move(whichElement, {x:-intItemWidth, duration: 0.5, afterFinish:function callback(obj){updateScrollInterface(TtgWindowList);}});
			} else {
				new Effect.Move(whichElement, {x:(intCurrentWindowWidth - intThisWindowListRightXPosition), duration: 0.5, afterFinish:function callback(obj){updateScrollInterface(TtgWindowList);}});
			}
		}
	}
}

function scrollWindowRight(whichElement) {
	if (whichElement == PtgWindowList) {
		intThisWindowListLeftXPosition = $(PtgWindowList).offsetLeft;
		if (intThisWindowListLeftXPosition < 0) {
			if (intThisWindowListLeftXPosition < -intItemWidth) {
				new Effect.Move(whichElement, {x:intItemWidth, duration: 0.5,  afterFinish: function callback(obj){updateScrollInterface(PtgWindowList);}});
			} else {
				new Effect.Move(whichElement, {x:(-1 * intThisWindowListLeftXPosition), duration: 0.5, afterFinish: function callback(obj){updateScrollInterface(PtgWindowList);}});
			}
		}
	} else {
		intThisWindowListLeftXPosition = $(TtgWindowList).offsetLeft;
		if (intThisWindowListLeftXPosition < 0) {
			if (intThisWindowListLeftXPosition < -intItemWidth) {
				new Effect.Move(whichElement, {x:intItemWidth, duration: 0.5,  afterFinish: function callback(obj){updateScrollInterface(TtgWindowList);}});
			} else {
				new Effect.Move(whichElement, {x:(-1 * intThisWindowListLeftXPosition), duration: 0.5, afterFinish: function callback(obj){updateScrollInterface(TtgWindowList);}});
			}
		}
	}
}

function initializeSlidingTiles() {
	if (intExperienceTileCount == 3) {
		$("tile1").style.left = 0 + "px";
		$("tile2").style.left = 74 + "px";
		$("tile3").style.left = 450 + "px";
	} else if(intExperienceTileCount == 2){
		$("tile1").style.left = 0 + "px";
		$("tile2").style.left = 160 + "px";
	}
	else{
		$("tile1").style.left = 74 + "px";
	}
}

function slideTiles(whichTile) {
	if (intExperienceTileCount == 3) {
		intTile2LeftPos = parseInt($("tile2").style.left);
		intTile3LeftPos = parseInt($("tile3").style.left);
		switch(whichTile) {
			case 1:
				if (intTile2LeftPos < 372) {
					new Effect.Move("tile2", {x:372, y:0, mode: 'absolute'});
				}
				if (intTile3LeftPos < 450) {
					new Effect.Move("tile3", {x:450, y:0, mode: 'absolute'});
				}
				break;
			case 2:
				if (intTile2LeftPos >= 372) {
					new Effect.Move("tile2", {x:74, y:0, mode: 'absolute'});
				}
				if (intTile3LeftPos < 450) {
					new Effect.Move("tile3", {x:450, y:0, mode: 'absolute'});
				}
				break;
			case 3:
				if (intTile2LeftPos >= 372) {
					new Effect.Move("tile2", {x:74, y:0, mode: 'absolute'});
				}
				if (intTile3LeftPos >= 450) {
					new Effect.Move("tile3", {x:151, y:0, mode: 'absolute'});
				}
				break;
		}
	} else {
		intTile2LeftPos = parseInt($("tile2").style.left);
		switch(whichTile) {
			case 1:
				if (intTile2LeftPos < 372) {
					new Effect.Move("tile2", {x:372, y:0, mode: 'absolute'});
				}
				break;
			case 2:
				if (intTile2LeftPos >= 372) {
					new Effect.Move("tile2", {x:160, y:0, mode: 'absolute'});
				}
				break;
		}
	}
}

Event.observe(window, 'resize', function() {
	updateScrollInterface(PtgWindowList);
	updateScrollInterface(TtgWindowList);
});

