// JavaScript Document to declare homepage routines

$(document).ready( function(){
	// Over-ride clicking events
	$('ul.ad_tabs a,div#ad_content a.ad_nav_btn').click( function(){
		// Return result from our actual event function
		return scrollAd( this );
	});// End onclick over-ride
	// Start the auto scrolling advertisements
	adTimers = setInterval("scrollAd( $('div#ad_content a.ad_next').get(0), true );", 4000);
});// End document onReady

// Function: scrollAd; Takes in the DOM object, and will promote to a jQuery equivalent
window.scrollAd = function(currObj) {
	// First make sure we have a jQuery object
	currObj = $(currObj);
	// Here we want to show the relevant content as per the user selection
	if( arguments.length == 1 ){
		clearInterval(adTimers);
	}// End argument length check
	arrTabs = ['chopper_ad','newitems_ad','closeouts_ad'];
	// Determine if we are currently inside the LI items or the links at the botom
	if( currObj.parent().attr('id') == 'ad_content' ){
		// User clicked on lower button link
		parentObj = currObj.parent().parent();
		// Since the user won't be clicking on the actual target ID, we must derive the "next" or "last"
		// Determine which direction we will go in
		arrClass = currObj.attr('className').split(' ');
		if( arrClass.length == 2 ){
			// Create variable to indicate our direction
			if( arrClass[1] == 'ad_next' )
				dir = 1;
			else
				dir = -1;
			// Store our current type
			strCurrentID = currObj.parent().parent().attr('id');
			// Loop over result
			for( index in arrTabs ){
				// Determine if we are on the correct index
				if( arrTabs[index] == strCurrentID ){
					// Localize current index
					intCurrentIndex = ( +index + +dir );
					// Now document which index will be our target
					if( arrTabs.length > intCurrentIndex && intCurrentIndex >= 0 )
						strParentID = arrTabs[intCurrentIndex];
					else if( dir == 1 )
						strParentID = arrTabs[0];
					else
						strParentID = arrTabs[arrTabs.length-1];
				}// End ID string equality check
			}// End array loop
		}// End length check
		// Before we continue, make sure we have our parent ID on hand, otherwise leave
		if( typeof(window.strParentID) == 'undefined' )
			return false;
	}// End truth clause for link type
	else{
		// User clicked on top variation( w/LI items preceeding content )
		strParentID = currObj.parent().attr('id');
		parentObj = currObj.parent().parent().parent();
	}// End false clause for link type
	objAdType = new Object();
	// Switch over current link type to assign our appropriate values
	switch( strParentID ){
		case 'closeouts':
		case 'closeouts_ad':
			objAdType = { Alt:'Cooking Closeouts - While supplies last', Image:'/site_images/front/closeouts-ad.png', Link:'/specializedpage.cfm?index=7', OuterDivID:'closeouts_ad', Text:'More Cooking Closeouts! &raquo;' };
			break;
		case 'chopper':
		case 'chopper_ad':
			objAdType = { Alt:'Prices Reduced - limited time', Image:'/site_images/front/chopper-ad.png', Link:'/specials.html', OuterDivID:'chopper_ad', Text:'More Price Chopper Deals! &raquo;' };
			break;
		case 'newitems':
		case 'newitems_ad':
			objAdType = { Alt:'Check out our newest items!', Image:'/site_images/front/new_items-ad.png', Link:'/specializedpage.cfm?index=58', OuterDivID:'newitems_ad', Text:'More New Items! &raquo;' };
			break;
	}// End link type switch
	// Make sure we have the correct object, than update content accordingly
	if(  objAdType.Alt != null && objAdType.Image != null && objAdType.Link != null && objAdType.OuterDivID != null && objAdType.Text != null ){
		// First update the outer div ID
		parentObj.attr('id',objAdType.OuterDivID);
		parentObj.show();
		// Next update the banner image and link
		parentObj.find('div#ad_content a').eq(0).attr('href',objAdType.Link).attr('alt',objAdType.Alt).attr('title',objAdType.Alt).find('img').eq(0).attr('src',objAdType.Image).attr('alt',objAdType.Alt).attr('title',objAdType.Alt);
		// Next update the link at the bottom
		parentObj.find('a.ad_more').eq(0).attr('href',objAdType.Link).attr('alt',objAdType.Text).html(objAdType.Text);
		// Next we need to get our current product
		for( index in arrTabs ){
			// Determine if this is the current tab
			if( arrTabs[index] == objAdType.OuterDivID && typeof(window.productsObj) != 'undefined' && productsObj.length > index ){
				// Here we should update our current product snippet, localize current product
				currProduct = productsObj[index];
				// Start with the prdouct image
				parentObj.find('li.item_image a').attr('href',productsObj[index].link).attr('title',currProduct.alt).find('img').eq(0).attr('src','/images/'+currProduct.thumbnail);
				// Continue with the description
				parentObj.find('li.descriptor a').attr('href',currProduct.link).attr('title',currProduct.alt).html( currProduct.description );
				// Finish with the price and UOM
				parentObj.find('li.item_price span.the_price').html( currProduct.price).formatCurrency({ useHtml: true }).append(' / '+currProduct.uom);
			}// End current tab index check
		}// End loop to find our current tab
	}// End ad type structure integiry check
	// Return false to caller because we have this as an onClick event
	return false;
};// End ad tabs