﻿/*
* @Name: CarouselManager Class v1.0
* @Usage: Oxfam Shop (Unwrapped and Donate Second Hand Store)
* @Date: 2008-09-07
* @Author: Stuart Thorne
*/
var CarouselManager = new Class({	
	initialize: function(id, type, starterCategory, width, height, iconWidth, navImgWidth, navImgHeight) {
		this.container = id;
		this.imageslide = new ImageSlide_shop(this,id, width, height, iconWidth, navImgWidth, navImgHeight, true); /* Load the carousel from 'imageslide_shop.js' */
		this.removeTips(); /* Remove the non-JS text associated with each icon */
		this.existsQueryOrCookie(this.container,type,starterCategory);
	},
	existsQueryOrCookie: function(id,type,starterCategory) { /* Check if there is a querystring category, cookie category or set the default category */
		var category = starterCategory;
		var cookieName = "carousel"+type;
		var pass = false;
		var setIconPosition = this.setIconPosition;
		var imageslide = this.imageslide;
		var cookieMonster = new CookieMonster(cookieName);
		if(cookieMonster.address('campaign') != '') { 
			var queryValue = cookieMonster.address('campaign');
			$$("#"+this.container+" li").each(function(icon, i) {
				if(icon.getProperty('id') == queryValue) { 
					pass = true;
					cookieMonster.trash(cookieName); 
					cookieMonster.bake(cookieName,queryValue,100); 
					category = queryValue;
					setIconPosition(id,type,queryValue,imageslide);
				}
			});
		}
		if(cookieMonster.grab(cookieName) && pass == false) {
			category = cookieMonster.grab(cookieName);
			this.setIconPosition(id,type,cookieMonster.grab(cookieName),this.imageslide); 
		} else if (pass == false){ 
			category = starterCategory;
			this.setIconPosition(id,type,starterCategory,this.imageslide); 
		}
		this.setIconEvents(id,category);
		this.setBillBoard(id,category);
	},
	setIconEvents: function(id,category) { /* Add events to the icon to set the billboard display */
		var fireEvent = this.setBillBoard;
		$$("#"+this.container+" a").each(function(icon, i) {
			icon.addEvent('click', function(e) {
				new Event(e).stop();
				fireEvent(id,icon.getParent().getProperty('id'));
			})/*.addEvent('keydown', function(e) {
				new Event(e).stop();
	            fireEvent(id,icon.getParent().getProperty('id'));
			})*/;
		});
	},	
	removeTips: function(){ /* Hide all of the text links associated with each icon */
		$$('*.block-link').each(function(tip) {
			tip.addClass('hide-tip');
		});
	},
	setIconPosition: function(id, type, category, imageslide) { /* In case the icon is not centered, roll it forward or back to the center position */
		if(type != 'SHS') {
			var arrLength = $$("#"+id+" li").length;
			$$("#"+id+" li").each(function(icon, i) {
				if(icon.getProperty('id') == category) {
					if(i == arrLength){
						imageslide.moveLeft(i-4);
				    } 
					else if(i == 8){
						imageslide.moveLeft(i-4);
					}
				    else{
				        if(i<=3){
				           	imageslide.moveLeft(3-i);
				        }
				        else if(i>=4){
				            imageslide.moveRight(i-3);
				        }
				    }
				}
			});
		}
	},
	setBillBoard: function(id,category) { /* If a billboard exists, change update it's properties. Else create one */
		$$("#"+id+" li").each(function(item) {
			item.removeClass("selected");
		});
		if(category){
			$(category).addClass("selected");
			var imgSrc = "/shop/static/img/carousel_billboard_" + category + ".jpg"; 
		    if($('billboard-image') != null){
		        $('billboard-image').setProperties({
					'src': imgSrc,
					'alt': 'Go to the ' + $(category).getFirst().title + ' promotional page'
				});
				$('billboard-image').getParent().setProperty('href',$(category).getFirst().href);
			} else {
			    new Element('img',{ 
	                'src' : imgSrc,
	                'id' : 'billboard-image'
	            }).injectInside(new Element('a',{
			        'href' : $(category).getFirst().href,
			        'title' : "Go to the " + $(category).getFirst().title + " promotional page"
			    }).inject($('imageview_large')));
			}
		}
	}
});
