// JavaScript Document
function DivCanvas(config){
	this.container = $(config.container);
	this.resizeWithWindow = config.resizeWithWindow || true;
	this.autoAlign = config.autoAlign || true;
	this.resize = new Signal(this, "onResize");
	this.startpreload = new Signal(this, "onPreload");
	this.preloadcomplete = new Signal(this, "onPreloadComplete");
	this.elements = [];
	this.currentW;
	this.currentH;
	this.longestW = 0;
	this.longestH = 0;
	this.background;
	this.backgroundImage;
	this.middleWidth;
	this.topOffset;
	this.displayIndex = 0;
	this.dataProvider;
	this.firstRun = true;
	this.resting = true;
	this.slideEaseIn = "easeOutQuint";
	this.slideEaseOut = "easeInQuint";
	this.baseSpeed = 450;
	this.baseDelay= 100;
	this.minimumHeight = config.minimumHeight || 880;
	this.minimumWidth = config.minimumWidth || 980;
	this.hideOffset = 300;
	this.currentDepth = 2;
	this.dataID;
	this.hasNavigation = false;
	this.navigation;
	this.leftButton = $(config.leftButton);
	this.rightButton = $(config.rightButton);
	this.sideNavOffset = 20;
	this.backgroundDelay = 100;
	this.hiddenPreloader;
	this.cleared = false;
	this.isPreloading = false;
	this.swfLayer;
	this.cachedDP;
	this.preloader;
	this.hasPreloader = false;
	this.navId;
	this.hasFloatBar = false;
	this.hasOverviewNav = false;
	this.overviewNav;
	this.floatBar;
	this.thumbBar;
	this.hasThumbBar = false;
	this.widthOffset = 18;
	this.initialize();
	this.clean = true;
	this.sx;
	this.cx;
	this.cachedStyle;
	this.queuedStyle;
	this.touchmoved = new  Signal(this, "onTouchMove");
	this.touchListener = false;

}
DivCanvas.prototype.initialize = function(){
	$(window).resize($lavdelegate(this, this.onResize));
	this.prepareContainer();
	
	var dim = this.getViewportDimensions($(window)[0]);
	this.currentW = dim.width;
	this.currentH = dim.height > this.minimumHeight ? this.minimumHeight : dim.height;
	if($isIDevice()){
		this.handleTouches();
	}
}
DivCanvas.prototype.handleTouches = function(){
	this.touchListener = true;
	document.getElementById(this.container.attr("id")).ontouchstart = $lavdelegate(this, this.onTouchStart);
	document.getElementById(this.container.attr("id")).ontouchmove = $lavdelegate(this, this.onTouchMove);
	document.getElementById(this.container.attr("id")).ontouchend = $lavdelegate(this, this.onTouchEnd);
	
}
DivCanvas.prototype.killTouches = function(){
	this.touchListener = false;
	document.getElementById(this.container.attr("id")).ontouchstart = null;
	document.getElementById(this.container.attr("id")).ontouchmove = null;
	document.getElementById(this.container.attr("id")).ontouchend = null;
}
DivCanvas.prototype.onTouchEnd = function(evt){
	//alert(this.cx);
	if(this.clean) return;
	//evt.preventDefault();
	var val = 0;
	var dir;
	
	if(this.cx < this.sx){
		val = this.sx-this.cx;
		dir = "right";
	}else if(this.cs > this.sx){
		val = this.cx-this.sx;
		dir = "left";
	}

	if(val > 200){

		if(dir == "left"){
			this.slidePrevious();			
		}else{
			this.slideNext();
		}
		this.touchmoved.dispatch({index:this.displayIndex});
		
	}
	this.sx = this.cx = evt.touches[0].pageX;
	

}
DivCanvas.prototype.onTouchMove = function(evt){
	if(this.clean) return;
	if(this.cx < this.sx){
		val = this.sx-this.cx;
		dir = "right";
	}else{
		val = this.cx-this.sx;
		dir = "left";
	}
	if(val > 100){
		evt.preventDefault();
	}
	if ( evt.touches.length == 1 ) {
		this.cx = evt.touches[0].pageX;
	}
}

DivCanvas.prototype.onTouchStart = function(evt){

	if(this.clean) return;
	//evt.preventDefault();
	if(evt.touches.length == 1){
		this.cx = this.sx = evt.touches[0].pageX;
	}
}
DivCanvas.prototype.checkIfImageExists = function(arr, img){
	if(img.indexOf("%7B") != -1 && img.indexOf("%7D") != -1) return true;
	for(var i=0;i<arr.length;i++){
		if(arr[i] == img) return true;
	}
	return false;
}
DivCanvas.prototype.preloadImages = function (source, cb) {
    this.isPreloading = true;
    var ilist = [];
    for (var i = 0; i < source.length; i++) {
        if (source[i].background.image.attr("src") != undefined) {
            if (!this.checkIfImageExists(ilist, source[i].background.image.attr("src"))) {
                ilist.push(source[i].background.image.attr("src"));
            }
        } else {
            if (source[i].background.image.find("img").attr("src") != undefined) {
                if (!this.checkIfImageExists(ilist, source[i].background.image.find("img").attr("src"))) {
                    ilist.push(source[i].background.image.find("img").attr("src"));
                }
            } 
        }
        for (var k = 0; k < source[i].elements.length; k++) {
            if (source[i].elements[k].div.find("img").attr("src") != undefined) {
                if (!this.checkIfImageExists(ilist, source[i].elements[k].div.find("img").attr("src")) && source[i].elements[k].div.find("img").length) {
                    ilist.push(source[i].elements[k].div.find("img").attr("src"));
                }
            }
        }
    }
	
    this.startpreload.dispatch({});
    if (ilist.length == 0) {
        
		setTimeout($lavdelegate(this, function(){
											   cb(source);
											   }), 10);
    } else {
        $.preLoadImages(ilist, $lavdelegate(this, function () {
            cb(source);
        }));
    }
}

DivCanvas.prototype.setData = function(dp, ndp){
	this.removeThumbBar();
	if(ndp){
		this.displayIndex = ndp;
	}else{
		this.displayIndex = 0;
	}
	this.preloadImages(dp, $lavdelegate(this, this.resolveLoadedData));
}
DivCanvas.prototype.resolveLoadedData = function(dp){
	this.clean = false;
	this.preloadcomplete.dispatch({});
	this.cachedDP = dp;
	
}
DivCanvas.prototype.resolveDisplay = function(n){
	
	if(this.cachedDP == null || this.cachedDP == "undefined" || this.cachedDP == undefined){
		return;
	}

	if(n > -1 && n < this.cachedDP.length){
		this.displayIndex = n;
	}
	if(this.dataProvider != null){
		this.dataProvider = this.cachedDP;
		if(this.cachedDP.length){
			this.replaceCurrentSet();

		}
		
	}else{
		this.dataProvider = this.cachedDP;
		if(this.cachedDP.length){
			this.displayCurrentSet();
			this.handleResizing($(window)[0]);
			
		}
		
	}
	
	
}

DivCanvas.prototype.replaceCurrentSet = function(){
	
	
	if(this.dataID != this.dataProvider[this.displayIndex].id){
		this.killCurrentSet();
		this.activateCurrentSet();
		this.startReplaceTransition();
	}
}
DivCanvas.prototype.activateCurrentSet = function(){
	var activeObject = this.dataProvider[this.displayIndex];

	this.elements = [];
	this.dataID = activeObject.id;
	
	for(var i=0;i<activeObject.elements.length;i++){
		this.addElement(activeObject.elements[i]);
	}
	this.addBackgroundElement(activeObject.background);
	
	if(activeObject.swf){
		this.addSWF(activeObject.swf);
	}
	this.checkSideNav();
	if(this.queuedStyle){
		this.applyElementStyle(this.queuedStyle.style,this.queuedStyle.wclass, this.queuedStyle.bclass);
		this.queuedStyle = null;
	}
	
}
DivCanvas.prototype.addSWF = function(sc){
	//this.container.append(sc);
	
	if(!$isIDevice()){
		swfobject.embedSWF(sc.swf, sc.name, "100%", "100%", "10.1.0", false, sc.flashvars, {bgcolor:"#000000", allowscriptaccess: 'always', wmode: 'opaque', play: false, loop: false, base: 'common/swf/' }, {id:sc.name});
	}else{
		$("#"+sc.name).append($("<iframe class='youtube-player' type='text/html' width='"+$(window).width()+"px' height='"+$(window).height()+"px' src='http://www.youtube.com/embed/"+sc.flashvars.videoURL+"?html5=1c' frameborder='0' id='"+sc.name+"'></iframe>"));
		$("#"+sc.name).css({top:0, left:0});
	}
	this.swfLayer = $("#"+sc.name);
	this.swfLayer.css({width:$(window).width(), height:$(window).height()});
	

}
DivCanvas.prototype.checkSideNav = function(){
	if(this.dataProvider.length < 2){
		if($("#canvas_left_nav").length){
			$("#canvas_left_nav").remove();
			$("#canvas_right_nav").remove();
		}
	}else{
		if($("#canvas_left_nav").length == 0){
			this.rightButton.attr("id", "canvas_right_nav");
			this.leftButton.attr("id", "canvas_left_nav");
			this.leftButton.css({position:"absolute"});
			this.rightButton.css({position:"absolute"});
			this.rightButton.css("z-index", 9999999);
			this.leftButton.css("z-index", 9999998);
			this.container.append(this.rightButton);
			this.container.append(this.leftButton);
			this.positionSideNavs();
			this.leftButton.unbind("click");
			this.rightButton.unbind("click");
			this.leftButton.bind("click", $lavdelegate(this, this.slidePrevious));
			this.rightButton.bind("click", $lavdelegate(this, this.slideNext));
		}
	}
}
DivCanvas.prototype.displayCurrentSet = function(){
	this.activateCurrentSet();
	this.startInitialTransition();
}
DivCanvas.prototype.prepareContainer = function(){
	this.container.css({backgroundColor:"transparent", overflow:"hidden", width:"100%", height:"0px", position:"relative"});
	//this.handleResizing($(window)[0]);
}
DivCanvas.prototype.handleResizing = function(ref){
	if(this.clean) return;
	var dim = this.getViewportDimensions(ref);
	if(this.resizeWithWindow){
		this.currentW = dim.width;
		this.currentH = dim.height;
		if(this.container.height() == 0){
			this.container.css({width:"100%"});
			this.container.stop().animate({height:this.currentH}, {duration:600, ease:"easeInOutQuint"});
		}else{
			this.container.css({width:"100%", height:this.currentH >= $(window).height() ? this.currentH : $(window).height()});
		}
		if(this.background){
			this.handleBackgroundAdjustments();
		}
		if(this.swfLayer){
			this.swfLayer.css({width:$(window).width(), height:$(window).height()});
		}
	}
	this.alignElements();
}
DivCanvas.prototype.getViewportDimensions = function(ref){
	var o = {};
	if(!$.browser.msie){
		o.width = ref.innerWidth < this.longestW ? this.longestW : ref.innerWidth;
		o.height = ref.innerHeight < this.longestH ? this.longestH : ref.innerHeight;
	}else{
		if (document.body && document.body.offsetWidth) {
		 winW = document.body.offsetWidth;
		 winH = document.body.offsetHeight;
		}
		if (document.compatMode=='CSS1Compat' && document.documentElement && document.documentElement.offsetWidth) {
		 winW = document.documentElement.offsetWidth;
		 winH = document.documentElement.offsetHeight;
		}
		
		//o.width = winW < this.longestW ? this.longestW : winW;
		//o.height = winH < this.longestH ? this.longestH : winH;	
		
		o.width = winW;
		o.height = winH;	
	}
	if(o.height < this.minimumHeight){
		o.height = this.minimumHeight;
		//o.width -= 16; //scrollbar shit
	}
	if(o.width < this.minimumWidth){
		o.width = this.minimumWidth;
	}
	return o;
}
DivCanvas.prototype.getBackgroundCenter = function(){
	return (this.currentW/2)-(this.backgroundImage.width()/2)
}
DivCanvas.prototype.handleBackgroundAdjustments = function(){
	
	this.background.css({width:this.currentW, height:this.currentH});
	this.backgroundImage.css({left:this.getBackgroundCenter()});
	if(this.minimumWidth < $(window).width()){
		//this.resizeImageElements();
	}
}
DivCanvas.prototype.handleBackgroundElement = function(ele){
	this.background = ele.bg;
	this.background.css("z-index", this.currentDepth);
	this.background.css("overflow", "hidden");
	this.background.css("position", "absolute");
	this.background.css({top:0, left:0});
	this.currentDepth++;
	this.backgroundImage = ele.image;
	this.backgroundImage.css("z-index", this.currentDepth);
	this.backgroundImage.css("position", "absolute");
	this.backgroundImage.css({top:0, left:0});
	
	this.background.append(this.backgroundImage);
	this.container.append(this.background);
	
	$(window).trigger("resize");
}
DivCanvas.prototype.handleNewElement = function(ele){
	
	this.elements.push(ele);


	ele.origCoords = this.getElementsCoords(ele);
	ele.div.css({position:"absolute"});
	ele.div.css("z-index", this.elements.length+this.currentDepth);
	this.container.append(ele.div);
	
	if(ele.bg){
		if(ele.div.find("div").length){
			ele.div.css({height:ele.div.find("div").height()+10});
		}else if(ele.div.find("img").length){
			ele.div.css({height:ele.div.find("img").height()});
		}
	}
	this.checkElementHeights();
	this.alignElements();
}
DivCanvas.prototype.checkElementHeights = function(){
	//trace("checking elements");
	for(var i=0;i<this.elements.length;i++){
		//trace(this.elements[i].div.height());
		if(this.elements[i].div.height()+150 > this.minimumHeight){
			this.minimumHeight = this.elements[i].div.height()+200;
		}
	}
	
}
DivCanvas.prototype.getCenter = function(){
	return this.currentW/2;
}
DivCanvas.prototype.getLeftEdge = function(){
	return this.getCenter()-(this.middleWidth/2);
}
DivCanvas.prototype.getRightEdge = function(){
	return this.getCenter()+(this.middleWidth/2);
}
DivCanvas.prototype.getMiddle = function(){
	return this.currentH/2;
}
DivCanvas.prototype.resizeImageElements = function(){
	var bgimage = $(this.backgroundImage.find("img")[0]);
	var cw = bgimage.width();
	var ch = bgimage.height();
	var o = {w:$(window).width(), h:$(window).height()}
	var nh = Math.ceil((Number(o.w)/Number(cw))*Number(ch));
	if(Number(nh) < Number(o.h)){
		var nw = Math.ceil((Number(o.h)/Number(ch))*Number(cw));
		bgimage.width(Number(nw));
		bgimage.height(Number(o.h));	
	}else{
		bgimage.width(Number(o.w));
		bgimage.height(Number(nh));
	}
	this.backgroundImage.css({top:0, left:this.getCenter()-bgimage.width()/2});
	bgimage.css({top:0, left:0});
}
DivCanvas.prototype.sideTopMiddle = function(){
	return ($(window).height())/2;
}
DivCanvas.prototype.positionSideNavs = function(){
	//this.rightButton.css({left:this.currentW-this.sideNavOffset-(this.rightButton.width()+15), top:this.currentH/2 - (this.rightButton.height()/2)});
	//this.leftButton.css({left:this.sideNavOffset, top:this.currentH/2-(this.leftButton.height()/2)});
	this.rightButton.css({left:this.currentW-this.sideNavOffset-(this.rightButton.width()+5), top:this.sideTopMiddle() - (this.rightButton.height()/2)});
	this.leftButton.css({left:this.sideNavOffset-12, top:this.sideTopMiddle()-(this.leftButton.height()/2)});
}
DivCanvas.prototype.alignElements = function(){
	//if(!this.resting) return;
	for (var i = 0;i<this.elements.length;i++){
		this.elements[i].div.css(this.getElementsCoords(this.elements[i]));			
	}
	if(this.hasNavigation){
		this.navigation.css({left:this.getLeftEdge(), top:0});
	}
	if(this.rightButton){
		this.positionSideNavs();
	}
	if(this.hasFloatBar){
		this.positionFloatBar();
	}
	if(this.hasOverviewNav){
		this.positionOverviewNav();
	}
	
	if(this.hasThumbBar){
		this.positionThumbBar();
	}
}
DivCanvas.prototype.positionOverviewNav = function(){
	this.overviewNav.css({left:this.getLeftEdge(), top:80});
}
DivCanvas.prototype.getElementsCoords = function (ele){
	switch(ele.align.toUpperCase()){
		case "TOP_LEFT":
			return {top:this.topOffset, left:this.getLeftEdge()};			
		case "TOP_RIGHT":
			return 	{top:this.topOffset, left:this.getLeftEdge()+550}	
		case "CENTER_TOP":
			return 	{top:this.topOffset, left:this.getLeftEdge()+206}	
		case "CENTER_BOTTOM":
			return {top:this.topOffset+470, left:this.getLeftEdge()+206};
		case "BOTTOM_LEFT":
			return 	{top:this.topOffset+227, left:this.getLeftEdge()}
		case "BOTTOM_RIGHT":
			return 	{top:this.topOffset+361, left:this.getLeftEdge()+637}	
		case "ABSOLUTE":
			return {top:ele.top, left:this.getLeftEdge()+ele.left};
		case "CENTER_SPREAD":
			return {top:this.topOffset, left:this.getLeftEdge()+($isIDevice() ? 65 : 5)};
		case "BOTTOM_LEFT_LAYER":
			return {top:this.topOffset+401, left:this.getLeftEdge()};
		case "BOTTOM_RIGHT_LAYER":
			return {top:this.topOffset+401, left:this.getLeftEdge()+597};
		case "GRID_TL":
			return {top:this.topOffset+15, left:this.getLeftEdge()};
		case "GRID_TR":
			return {top:this.topOffset+15, left:this.getLeftEdge()+283};
		case "GRID_BL":
			return {top:this.topOffset+207, left:this.getLeftEdge()};
		case "GRID_BR":
			return {top:this.topOffset+207, left:this.getLeftEdge()+283};
		
	}
	return {top:this.topOffset, left:this.getLeftEdge()};
}
DivCanvas.prototype.onResize = function(evt){
	this.handleResizing(evt.target);
	this.resize.dispatch({});
}
DivCanvas.prototype.listenToResize = function(b){
	this.resizeWithWindow = b;
}
DivCanvas.prototype.moveElementsTo = function(type){
	var dim = this.getViewportDimensions($(window)[0]);
	switch(type){
		case "BOTTOM":
			for(var i=0;i<this.elements.length;i++){
				this.elements[i].div.css({top:dim.height});
			}
			this.background.css({top:dim.height});
			this.backgroundImage.css({top:dim.height});
		break;
		case "RIGHT":
			for(var i=0;i<this.elements.length;i++){
				this.elements[i].div.css({left:dim.width});
			}
			this.background.css({left:dim.width});
			this.backgroundImage.css({left:dim.width});
		break;
		case "LEFT":
			for(var i=0;i<this.elements.length;i++){
				this.elements[i].div.css({left:-dim.width});
			}
			this.background.css({left:-dim.width});
			this.backgroundImage.css({left:-dim.width});
		break;
		case "TOP":
			for(var i=0;i<this.elements.length;i++){
				this.elements[i].div.css({top:-dim.height});
			}
			this.background.css({top:-dim.height});
			this.backgroundImage.css({top:-dim.height});
		break;
	}
}
DivCanvas.prototype.slideNext = function(){
	
	if($isIDevice()){
		this.killTouches();
	}
	
	this.sx = this.cx;
	if(this.displayIndex<this.dataProvider.length-1){
		this.hideCurrentSet("RIGHT");
		this.displayIndex++;
	}else{
		this.hideCurrentSet("RIGHT");
		this.displayIndex = 0;
	}
	this.showNewSet("RIGHT");
	
}
DivCanvas.prototype.slidePrevious = function(){
	if($isIDevice()){
		this.killTouches();
	}
	this.sx = this.cx;
	if(this.displayIndex>0){
		this.hideCurrentSet("LEFT");
		this.displayIndex--;
	}else{
		this.hideCurrentSet("LEFT");
		this.displayIndex = this.dataProvider.length-1;
	}
	this.showNewSet("LEFT");
}
DivCanvas.prototype.slideTo = function(n){
	if(this.displayIndex == n) return;
	if(n < this.dataProvider.length && n > -1){
		var dir = n > this.displayIndex ? "RIGHT" : "LEFT";
		this.hideCurrentSet(dir);
		this.displayIndex = n;
		this.showNewSet(dir);
	}
}
DivCanvas.prototype.showNewSet = function(dir){
	
	this.activateCurrentSet();
	this.startElementTransition(dir);
}
/* TRANSITION COMING IN */
DivCanvas.prototype.startElementTransition = function(dir){
	this.resting = false;
	var del = 0;
	this.moveElementsTo(dir);
	this.backgroundImage.stop().delay(del).animate({left:this.getBackgroundCenter()}, {duration:this.baseSpeed+this.backgroundDelay, ease:this.slideEaseIn});
	del+=this.baseDelay;
	this.background.stop().delay(del).animate({left:0}, {duration:this.baseSpeed+this.backgroundDelay, ease:this.slideEaseIn});		
	del+=this.baseDelay;
	for(var i=0;i<this.elements.length;i++){
		this.elements[i].div.css({top:this.elements[i].origCoords.top});
		this.elements[i].div.stop().delay(del).animate({left:this.elements[i].origCoords.left}, {duration:this.baseSpeed+100, ease:this.slideEaseIn, complete:(i==this.elements.length-1) ? $lavdelegate(this, this.onLastItemTransitioned) : null });
		del-=this.baseDelay;
	}
	
}
DivCanvas.prototype.startInitialTransition = function(){
	this.resting = false;	
	var del = 0;
	this.moveElementsTo("BOTTOM");
	this.background.stop().delay(del).animate({top:0}, {duration:this.baseSpeed+this.backgroundDelay, ease:this.slideEaseIn});		
	del+=this.baseDelay;
	this.backgroundImage.stop().delay(del).animate({top:0}, {duration:this.baseSpeed+this.backgroundDelay, ease:this.slideEaseIn});
	del+=this.baseDelay;
	for(var i=0;i<this.elements.length;i++){
		this.elements[i].div.css({left:this.elements[i].origCoords.left});
		this.elements[i].div.stop().delay(del).animate({top:this.elements[i].origCoords.top}, {duration:this.baseSpeed-100, ease:this.slideEaseIn, complete:(i==this.elements.length-1) ? $lavdelegate(this, this.onLastItemTransitioned) : null });
		del+=this.baseDelay;
	}
}
DivCanvas.prototype.startReplaceTransition = function(){
	this.resting = false;
	var del = 0;
	this.moveElementsTo("BOTTOM");
	this.background.stop().delay(del).animate({top:0}, {duration:this.baseSpeed+this.backgroundDelay, ease:this.slideEaseIn});		
	del+=this.baseDelay;
	this.backgroundImage.stop().delay(del).animate({top:0}, {duration:this.baseSpeed+this.backgroundDelay, ease:this.slideEaseIn});
	del+=this.baseDelay;
	
	if(this.elements.length == 0){
		this.onLastItemTransitioned();
	}
	for(var i=0;i<this.elements.length;i++){
		this.elements[i].div.css({left:this.elements[i].origCoords.left});
		this.elements[i].div.stop().delay(del).animate({top:this.elements[i].origCoords.top}, {duration:this.baseSpeed-100, ease:this.slideEaseIn, complete:(i==this.elements.length-1) ? $lavdelegate(this, this.onLastItemTransitioned) : null });
		del+=this.baseDelay;
	}

}

/* END TRANSITION COMING IN */


/* TRANSITION GOING OUT */
DivCanvas.prototype.hideCurrentSet = function(dir){
	var del = (this.elements.length+2)*this.baseDelay;
	
	var dim = this.getViewportDimensions($(window)[0]);
	this.backgroundImage.stop().delay(del).animate({left:(dir == "RIGHT" ? -dim.width : dim.width)+this.hideOffset}, {duration:this.baseSpeed+this.backgroundDelay, ease:this.slideEaseOut});		
	
	del-=this.baseDelay;
	this.background.stop().delay(del).animate({left:(dir == "RIGHT" ? -dim.width : dim.width)+this.hideOffset}, {duration:this.baseSpeed+this.backgroundDelay, ease:this.slideEaseOut});
	del-=this.baseDelay;

	var tokill = {};
	tokill.swfLayer = this.swfLayer;
	
	try{
		this.background.attr("id", "bg_"+this.dataID);
		this.backgroundImage.attr("id", "bgimage_"+this.dataID);
		tokill.background = this.background.attr("id");
		tokill.backgroundImage = this.backgroundImage.attr("id");
		tokill.elements = [];
		
		for(var i=0;i<this.elements.length;i++){
			
			this.elements[i].div.attr("id", "element"+i+"_"+this.dataID);
			tokill.elements[i] = this.elements[i].div.attr("id");

			this.elements[i].div.css({top:this.elements[i].origCoords.top});
			this.elements[i].div.stop().delay(del).animate({left:(dir == "RIGHT" ? -dim.width : dim.width)+this.hideOffset}, {duration:this.baseSpeed+((dir == "RIGHT") ? 500 : 0), ease:this.slideEaseOut, complete:(i==this.elements.length-1) ? $lavdelegate(this, function() {this.killOldItems(tokill);}) : null });
			del-=this.baseDelay;
		}

		
		if(tokill.elements.length == 0 && tokill.swfLayer != undefined && tokill.swfLayer != null){
			//$("#"+tokill.swfLayer.attr("id")).stop().animate({left:(dir == "RIGHT" ? -dim.width : dim.width)+this.hideOffset}, {duration:this.baseSpeed+((dir == "RIGHT") ? 500 : 0), ease:this.slideEaseOut, complete: $lavdelegate(this, function() {this.killOldItems(tokill);}) });
			//$("#"+tokill.swfLayer.attr("id")).stop().animate({opacity:0}, {duration:100, ease:this.slideEaseOut })
			setTimeout($lavdelegate(this, function() {this.killOldItems(tokill);}), 400);
		}
	}catch(err){
		trace(err + " on hide");
	}

}
DivCanvas.prototype.killCurrentSet = function(){
	var del = (this.elements.length+2)*this.baseDelay;
	var dim = this.getViewportDimensions($(window)[0]);
	this.backgroundImage.stop().delay(del).animate({top:-(dim.height+this.hideOffset)}, {duration:this.baseSpeed+this.backgroundDelay, ease:this.slideEaseOut, complete:$lavdelegate(this, function() {this.killOldItems(tokill);})});
	del-=this.baseDelay;
	this.background.stop().delay(del).animate({top:-(dim.height+this.hideOffset)}, {duration:this.baseSpeed+this.backgroundDelay, ease:this.slideEaseOut});		
	del-=this.baseDelay;
	var tokill = {};
	tokill.swfLayer = this.swfLayer;
	try{
		this.background.attr("id", "bg_"+this.dataID);
		this.backgroundImage.attr("id", "bgimage_"+this.dataID);
		tokill.background = this.background.attr("id");
		tokill.backgroundImage = this.backgroundImage.attr("id");
		tokill.elements = [];
		for(var i=0;i<this.elements.length;i++){
			this.elements[i].div.attr("id", "element"+i+"_"+this.dataID);
			tokill.elements[i] = this.elements[i].div.attr("id");
			this.elements[i].div.css({top:this.elements[i].origCoords.top});
			this.elements[i].div.stop().delay(del).animate({top:-(dim.height+this.hideOffset)}, {duration:this.baseSpeed+this.backgroundDelay, ease:this.slideEaseOut});
			del-=this.baseDelay;
		}
		
	}catch(err){
		trace(err + " on kill");
	}
}

/* END TRANSITION COMING OUT */
DivCanvas.prototype.killOldItems = function(tokill){
	//should clear and remove previous display elements
	if($isIDevice()){
		this.handleTouches();
	}
	$("#"+tokill.background).remove();
	$("#"+tokill.backgroundImage).remove();
	try{
		document.getElementById(tokill.swfLayer.attr("id")).killVideo();
		tokill.swfLayer.remove();
	}catch(err){
		//trace("could not kill video");
	}
	for(var i=0;i<tokill.elements.length;i++){
		$("#"+tokill.elements[i]).remove();
	}

}
DivCanvas.prototype.clearOldItems = function(tokill){
	this.dataProvider = null;
	$("#"+tokill.background).remove();
	$("#"+tokill.backgroundImage).remove();
	$("#"+tokill.leftButton).remove();
	$("#"+tokill.rightButton).remove();
	if(this.hasNavigation){
		this.navigation.remove();
		this.hasNavigation = false;
	}
	if(this.hasFloatBar){
		this.floatBar.remove();
		this.hasFloatBar = false;
	}
	if(this.hasOverviewNav){
		this.overviewNav.remove();
		this.hasOverviewNav = false;
	}
	if(this.hasThumbBar){
		this.thumbBar.remove();
		this.hasThumbBar = false;
	}
	try{
		document.getElementById(tokill.swfLayer.attr("id")).killVideo();
		tokill.swfLayer.remove();
	}catch(err){
	}
	for(var i=0;i<tokill.elements.length;i++){
		$("#"+tokill.elements[i]).remove();
	}
}

DivCanvas.prototype.cleanUp = function(){	
	this.clean = true;
	var tokill = {};
	tokill.swfLayer = this.swfLayer;
	try{
		this.background.attr("id", "bg_"+this.dataID);
		this.backgroundImage.attr("id", "bgimage_"+this.dataID);
		tokill.background = this.background.attr("id");
		tokill.backgroundImage = this.backgroundImage.attr("id");
	}catch(err){}
	this.leftButton.attr("id", "left_button_"+this.dataID);
	this.rightButton.attr("id", "right_button_"+this.dataID);
	
	tokill.leftButton = this.leftButton.attr("id");
	tokill.rightButton = this.rightButton.attr("id");
	
	tokill.elements = [];
	for(var i=0;i<this.elements.length;i++){
		this.elements[i].div.attr("id", "element"+i+"_"+this.dataID);
		tokill.elements[i] = this.elements[i].div.attr("id");
	}
	this.clearOldItems(tokill);
}
DivCanvas.prototype.onLastItemTransitioned = function(){

	this.resting = true;
}
DivCanvas.prototype.activate = function(){
	this.container.css({height:0});
}
DivCanvas.prototype.deactivate = function(){
	this.container.css({height:0});
}
DivCanvas.prototype.addBackgroundElement = function(ele){
	this.handleBackgroundElement(ele);
}
DivCanvas.prototype.addElement = function(ele){
	this.handleNewElement(ele);
}
DivCanvas.prototype.centerAlignElement = function(targ){
	targ.css({left:(this.currentW/2)-targ.width()/2});
}
DivCanvas.prototype.addNavigation = function(nav, nid){
	if(this.hasNavigation){
		this.navigation.remove();
	}
	this.navId = nid;
	this.navigation = nav;
	this.navigation.css({position:"absolute"});
	this.navigation.css("z-index", 999999);
	this.container.append(this.navigation);
	this.alignElements();
	this.navigation.css({left:this.getLeftEdge()});
	this.navigation.css({top:-this.navigation.height()});
	this.navigation.stop().animate({top:0}, {duration:this.baseSpeed/2, ease:this.slideEaseIn});
	this.hasNavigation = true;
	
}
DivCanvas.prototype.addPreloader = function(p){
	if(this.hasPreloader){
		this.preloader.remove();
	}
	this.preloader = p;
	this.preloader.css({position:"absolute", background:"#ebebeb"});
	this.preloader.css("z-index", 9999999);
	this.container.append(this.preloader);
	this.preloader.css({width:this.currentW});
	this.hasPreloader = true;
}
DivCanvas.prototype.onScrollWithThumb = function(evt){
	
	if(this.hasThumbBar){
		this.positionThumbBar();
	}
}
DivCanvas.prototype.getCanvasBottom = function(offset){
	if(!offset){
		offset = 0;
	}
	return Math.abs($(window).scrollTop()-this.container.offset().top);
}
DivCanvas.prototype.addThumbBar = function(tb){
	$(window).bind("scroll", $lavdelegate(this, this.onScrollWithThumb));
	if(this.hasThumbBar){
		this.thumbBar.remove();
	}
	this.thumbBar = tb;
	this.thumbBar.css({position:"absolute"});
	this.thumbBar.css("z-index", 1500);
	this.container.append(this.thumbBar);
	this.positionThumbBar();
	this.hasThumbBar = true;
}
DivCanvas.prototype.removeThumbBar = function(){
	if(this.hasThumbBar){
		this.thumbBar.remove();
		this.hasThumbBar = false;
	}
	$(window).unbind("scroll", $lavdelegate(this, this.onScrollWithThumb));
	
}
DivCanvas.prototype.positionThumbBar = function(){
	var diff = ($(window).scrollTop()-this.container.offset().top);
	var bot = diff+($(window).height()-this.thumbBar.height());
	this.thumbBar.css({left:this.getCenter()-(this.thumbBar.width()/2), top:bot});
	
	if((this.thumbBar.offset().top+this.thumbBar.height())-this.container.offset().top < 500){
		this.thumbBar.css({left:this.getCenter()-(this.thumbBar.width()/2), top:bot});
	}
	
	if((this.thumbBar.offset().top+this.thumbBar.height())-this.container.offset().top > this.container.height()){
		this.thumbBar.css({left:this.getCenter()-(this.thumbBar.width()/2), top:this.container.height()-this.thumbBar.height()});
	}
	
}
DivCanvas.prototype.addFloatBar = function(fb){
	if(this.hasFloatBar){
		this.floatBar.remove();
	}
	this.floatBar = fb;
	this.floatBar.css({position:"absolute"});
	this.floatBar.css("z-index", 99999999);
	this.container.append(this.floatBar);
	this.positionFloatBar();
	this.hasFloatBar = true;
	
}
DivCanvas.prototype.positionFloatBar = function(){
	//this.floatBar.css({left:(this.currentW-this.floatBar.width()*1.5)-15, top:this.currentH/2 + 35});
	this.floatBar.css({left:(this.currentW-this.floatBar.width()*1.5)+1, top:this.sideTopMiddle() + 65});
}
DivCanvas.prototype.addOverviewNav = function(ovn){
	if(this.hasOverviewNav){
		this.overviewNav.remove();
	}
	this.overviewNav = ovn;
	this.overviewNav.css({position:"absolute"});
	this.overviewNav.css("z-index", 999999999);
	this.container.append(this.overviewNav);
	//this.overviewNav.css({left:382, top:10});
	this.positionOverviewNav();
	this.hasOverviewNav = true;
}
DivCanvas.prototype.removeOverviewNav = function(){
	if(this.hasOverviewNav){
		this.overviewNav.remove();
	}
	this.hasOverviewNav = false;
}
DivCanvas.prototype.applyElementStyle = function(style, wclass, bclass){
	if(this.elements.length == 0){
		this.queuedStyle = {style:style, wclass:wclass, bclass:bclass};
	}
	var standardItems = [this.overviewNav, this.floatBar, this.navigation, this.leftButton, this.rightButton]
	for(var i=0;i<standardItems.length;i++){

		if(standardItems[i]){
			if (standardItems[i].hasClass(bclass)) standardItems[i].removeClass(bclass);
		    if (standardItems[i].hasClass(wclass)) standardItems[i].removeClass(wclass);
			standardItems[i].addClass(style);
		}
	}
	for(var i=0;i<this.elements.length;i++){	
		if (this.elements[i].div.hasClass(bclass)) this.elements[i].div.removeClass(bclass);
	    if (this.elements[i].div.hasClass(wclass)) this.elements[i].div.removeClass(wclass);
		
		this.elements[i].div.addClass(style);
	}
	
	this.queuedStyle = {style:style, wclass:wclass, bclass:bclass};
}
DivCanvas.prototype.setNewHeight = function(h){
	this.minimumHeight = h;
	this.handleResizing($(window)[0]);
}
