﻿function initSearch()
{
	$(function () {
		$("#tbxSearch").autocomplete({
			source: "/webservices/autocomplete.aspx",
			minLength: 1    
		});
    });
    hookKeyDown();
}
function searchEvents()
{
    var tbxSearch = document.getElementById("tbxSearch");
	if(trim(tbxSearch.value) != "")
		window.location = "/viewevents.aspx?eventname=" + tbxSearch.value
}   
function lTrim( value ) {
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
}
function rTrim( value ) {
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
}
function trim( value ) {
	return lTrim(rTrim(value));
}
//this function allows any link that has a rel attriute set to external to open in a new window
//this is done to allow for a link to open in a new window and to keep the page XHTML strict complient
//you can't use the target attribute on an XHTML strict complient page
function externalLinks()
{
	if (!document.getElementsByTagName) return;
	
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++) 
	{
		var anchor = anchors[i];
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external")
			anchor.target = "_blank";
	}
} 
function hookKeyDown(){document.onkeydown = alertkey;    }
function alertkey(e) 
{
	if(!e) 
	{
		if( window.event ) //Internet Explorer
		  e = window.event;
		else 
		  return;
	}
	if( typeof( e.keyCode ) == 'number') //DOM
		e = e.keyCode;
	else if( typeof( e.which ) == 'number') //NS 4 compatible
		e = e.which;
	else if( typeof( e.charCode ) == 'number') //also NS 6+, Mozilla 0.9+
		e = e.charCode;
	else 
		return;
		
	if(e==13) 
		return searchEvents();
 }

var iCurrentDisplayIndex = 1;
var _bAutoRotate = true;
var _iContentCount = 0;
var _iUpcomingContentCurrentIndex = 1;
function previousContent() {
	iCurrentDisplayIndex = iCurrentDisplayIndex - 1;
	if (iCurrentDisplayIndex == 0)
		iCurrentDisplayIndex = _iContentCount;

	rotateContent(iCurrentDisplayIndex, _iContentCount, true, 0);
}
function nextContent() {
	iCurrentDisplayIndex = iCurrentDisplayIndex + 1;
	if (iCurrentDisplayIndex > _iContentCount)
		iCurrentDisplayIndex = 1;

	rotateContent(iCurrentDisplayIndex, _iContentCount, true, 0);
}
function startContentRotate(iContentCount) {
	_iContentCount = iContentCount;
	iCurrentDisplayIndex++;
	if(iCurrentDisplayIndex == iContentCount+ 1)
		iCurrentDisplayIndex = 1;

	if (_bAutoRotate)
		setTimeout ('rotateContent(' + iCurrentDisplayIndex + ', ' + iContentCount+ ',false, 1);startContentRotate(' + iContentCount+ ')', 5000);
}
function rotateContent(iDisplayIndex, iContentCount, bStopAutoRotate, iSource) {
	if(!_bAutoRotate && iSource == 1)
		return;

	iCurrentDisplayIndex = iDisplayIndex;
	$("#h1FeaturedTitle").html($("#title" + iDisplayIndex).val())
	$("#divFeaturedContent").html($("#content" + iDisplayIndex).val());
	$("#imgMainPic").fadeOut(500, function () {
		$("#imgMainPic").attr("src", "/contentimages/" + $("#image" + iDisplayIndex).val());
		$("#imgMainPic").fadeIn(500);
	});
	
	$("#aBuyNowLink").attr("href", $("#link" + iDisplayIndex).val());

	for(var i = 1; i <= iContentCount; i++)
	{
		$("#control" + i).attr("class", "noneCurrentContent");
	}
	$("#control" + iDisplayIndex).attr("class", "currentContent");
	
	if(bStopAutoRotate == true)
		_bAutoRotate = false
}
function prevUpComing() {
	var iMaxCount = getMaxUpComingIndex()
	if ($("#liUpcomingCntrCnt" + (_iUpcomingContentCurrentIndex - 1)).length == 0)
		_iUpcomingContentCurrentIndex = iMaxCount;
	else
		--_iUpcomingContentCurrentIndex;
	showUpComing(_iUpcomingContentCurrentIndex);
}
function  nextUpComing() {
	var iMaxCount = getMaxUpComingIndex()
	if ($("#liUpcomingCntrCnt" + (_iUpcomingContentCurrentIndex + 1)).length == 0)
		_iUpcomingContentCurrentIndex = 1;
	else
		++_iUpcomingContentCurrentIndex;
	showUpComing(_iUpcomingContentCurrentIndex);
}
function getMaxUpComingIndex()
{
	var j = 1;
	while ($("#liUpcomingCntrCnt" + j).length > 0)
		j++;

	return j-1;
}
function showUpComing(iIndex) {
	_iUpcomingContentCurrentIndex = iIndex;
	iDisplayIndex = (iIndex * 2) - 1
	$("#upcomingCntrCnt").fadeOut(400, function () {
		for (var i = 0; i <= 1; i++) {
			if ($("#title" + (iDisplayIndex + i)).length > 0) {
				$("#upcomingCnt" + (i + 1)).show();
				$("#strUpcomingTitle" + (i + 1)).html($("#upComingTitle" + (iDisplayIndex + i)).val())
				$("#spnUpcomingDesc" + (i + 1)).html($("#upComingContent" + (iDisplayIndex + i)).val());
				$("#imgUpcomingImage" + (i + 1)).attr("src", "/contentimages/" + $("#upComingImage" + (iDisplayIndex + i)).val());
				$("#aUpcomingLink" + (i + 1)).attr("href", $("#upComingLink" + (iDisplayIndex + i)).val());
			}
			else
				$("#upcomingCnt" + (i + 1)).hide();
		}
		var j = 1;
		while ($("#liUpcomingCntrCnt" + j).length > 0) {
			$("#liUpcomingCntrCnt" + j).attr("class", "noneCurrentUpcomingCntr");
			j++;
		}
		$("#liUpcomingCntrCnt" + iIndex).attr("class", "currentUpcomingCntr");
		$("#upcomingCntrCnt").fadeIn(400);
	});
}
function newComment(iPostID) {
	var newCommentWindow = window.open("/blogNewComment.aspx?postid=" + iPostID, 'newComment', 'location=0,status=1,scrollbars=1,menubar=0,resizable=1,width=850,height=550');
	newCommentWindow.focus();
}
function showGuarantee() {
	window.open('/guarantee.html', 'guarantee', 'scrollbars=0,status=1,menubar=0,resizable=0,width=450,height=550');
}

