var startIDGlobal;
var fillSku;
var coll;
var currentlySelectedDevice = '';

function carouselLib(builderFile, activeFlag) {
	var activeFlag = activeFlag;
	this.loadDependencies = function() {
		if(activeFlag) {
			this.incFile('/global/scripts/effects_182.js');
			this.incFile('/global/scripts/carousel.js');
			this.incFile('/global/scripts/backButtonSetup.js');
			this.incFile('/global/styles/carousel.css');
		}
	}

	
	// method loads css or js files needed
	this.incFile = function(filename) {
		var head = document.getElementsByTagName('head')[0];
		if(filename.indexOf('.js') != -1) {
			var filePtr = document.createElement('script');
			filePtr.setAttribute('src',filename);
			filePtr.setAttribute('type', 'text/javascript');
		} else if(filename.indexOf('.css') != -1) {
			var filePtr = document.createElement('link');
			filePtr.setAttribute('href', filename);
			filePtr.setAttribute('rel', 'stylesheet');
			filePtr.setAttribute('type', 'text/css');
		}
		head.appendChild(filePtr)
	}
	
	// parse the JSON struct
	this.init = function() {
		coll = applications.appConstructs;
		if(coll.length < 5 || !activeFlag) {
			$('next_button').hide();
			$('previous_button').hide();
			$('carouselContainer').setStyle({backgroundImage: 'url(null)'});
		}
		$('horizontal_carousel').show();
	}
	
	// method resets carousel to last viewed position
	this.backScroller = function() {
		if(window.location.hash) {
			startIDGlobal = parseInt(window.location.hash.substr(1));
		}
	}
	
	// method sets last position in history
	this.linkSetUp = function(id, href) {
		window.location = '#' + id;
		window.location = href;
	}
		
	// method parses JSON struct and calls factory to create DOM elements
	this.imgProxy = function(parent) {
		for(i = 0; i < coll.length; i++) {
			this.imgFactory(coll[i], parent);
		}
	}
	
	// method parses JSON struct and calls factory to create DOM elements
	this.listProxy = function(parent) {
		var pCurrentListId;
		for(i = 0; i < coll.length; i++) {
			pCurrentListId = this.listFactory(coll[i], parent);
			this.imgFactory(coll[i], pCurrentListId);
		}
	}
	
	// method creates DOM elements in carousel
	this.imgFactory = function(imageCol, parent) {
		var image = $(document.createElement('img'));
		var oc = 'carousel.updateDetails('+imageCol.id+')';
		var omover = 'carousel.hoverState('+imageCol.id+')';
		var omout = 'carousel.unHoverState('+imageCol.id+')';
		image.setAttribute('id', 'img'+imageCol.id);
		image.setAttribute('src', imageCol.iconPath);
		//image.setAttribute('longdesc', imageCol.longDesc);
		image.setAttribute('alt', imageCol.shortDesc);
		image.setAttribute('border', 0);
		image.setAttribute('onclick', oc);
		image.setAttribute('onmouseover', omover);
		image.setAttribute('onmouseout', omout);
		image.setAttribute('style', 'padding-left:12px;padding-top:12px;');
		if(document.all) {
			image.style.setAttribute('cssText', 'padding-left:12px;padding-top:12px;');
		}
		document.getElementById(parent).appendChild(image);
		if(imageCol.hasDetail == 'true') {	
			var detail = $(document.createElement('div'));
			detail.setAttribute('id', 'd'+imageCol.id);
			detail.setAttribute('class', 'appText');
			detail.setAttribute('style', 'font-size:10px;color:#000000;text-align:left;padding-left:12px;padding-top:6px;');
			if(document.all) {
				detail.style.setAttribute('cssText', 'font-size:10px;color:#000000;text-align:left;padding-left:12px;padding-top:6px;');
			}
			document.getElementById(parent).appendChild(detail);	
			document.getElementById('d'+imageCol.id).innerHTML = '<span onclick="carousel.updateDetails('+imageCol.id+')" onmouseover="carousel.hoverState('+imageCol.id+')" style="color:#0a94d6;cursor:pointer;" id="applink' + imageCol.id + '">' + imageCol.shortDesc + '</span>' + '<br/>' + 'Category: ' + imageCol.genre + '<br>Price: <strong>' + imageCol.price + '</strong>';
		}
	}
	
	// method creates DOM elements in carousel
	//<li>
	//	<img alt="Window" src="images/phones/apple_iphone_3g_16gb_white_s.gif" /><br />
	//</li>
	this.listFactory = function(listCol, parent) {
		var oc = 'carousel.updateDetails('+listCol.id+');';
		var omover = 'carousel.hoverState('+listCol.id+');';
		var omout = 'carousel.unHoverState('+listCol.id+');';
		var listItem = $(document.createElement('li'));
		if(listCol.hasDetail) {
			var listItemLink = $(document.createElement('a'));
			listItemLink.setAttribute('href', 'javascript:'+oc);
		}
		listItem.setAttribute('id', 'list'+listCol.id);
		if(listCol.hasDetail) {
			listItemLink.setAttribute('id', 'listLink'+listCol.id);
			listItemLink.setAttribute('onclick', oc);
			listItemLink.setAttribute('onMouseOver', omover);
			listItemLink.setAttribute('onMouseOut', omout);
			if(document.all) {
				listItemLink.onMouseOver=omover;
				listItemLink.onMouseOut=omout;
			}
		}
		document.getElementById(parent).appendChild(listItem);
		if(listCol.hasDetail) {
			document.getElementById('list'+listCol.id).appendChild(listItemLink);
			if(document.all) {
				$('listLink'+listCol.id).onmouseover = new Function("carousel.hoverState("+listCol.id+");");
				$('listLink'+listCol.id).onmouseout = new Function("carousel.unHoverState("+listCol.id+");");
			}
		}
		return 'listLink'+listCol.id;
	}
	
	// on click make the right item selected and all the other unselected
	this.updateDetails = function(id) {
		if(typeof setButtonActions !== 'undefined') {
			if(id == '1') {
				$('screenshot').setStyle({height: '750px'});
			} else {
				$('screenshot').setStyle({height: '450px'});
			}
		}
		if(typeof unitPrice != 'undefined' && coll[id-1].price != '') {
			unitPrice = coll[id-1].price;
		}
		$('appInfo').innerHTML=coll[id-1].longDesc;
		if(coll[id-1].imagePath.indexOf('.swf') != '-1') {
			$('appImage').src= '';
			$('appImage').hide();
			$('appSwf').show();
			$('appSwf').innerHTML='<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="417" height="313" id="Acer_Overview" align="middle" style="z-index: -1;"><param name="allowScriptAccess" value="sameDomain" /><param name="allowFullScreen" value="false" /><param name="movie" value="'+coll[id-1].imagePath+'" /><param name="quality" value="high" /><param name="bgcolor" value="#ffffff" /><embed src="'+coll[id-1].imagePath+'" quality="high" bgcolor="#ffffff" width="417" height="313" name="Acer_Overview" align="middle" allowScriptAccess="sameDomain" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></object>';
		} else {
			if($('appSwf')) {
				$('appSwf').innerHTML='';
				$('appSwf').hide();
			}
			$('appImage').show();
			$('appImage').src=coll[id-1].imagePath;
		}
		$('img'+id).src=coll[id-1].iconPathSelected;
		if($('applink'+id) != null) { 
			$('applink'+id).style.color='#f27d00';
		}
		for(var l = 1; l <= coll.length;l++) {
			if(l != id) {
				$('img'+l).src=coll[l-1].iconPath;
				if($('applink'+l) != null) {
					$('applink'+l).style.color='#0a94d6';
				}
			}
		}
		if(typeof setButtonActions !== 'undefined') {
			setButtonActions();
			currentlySelectedDevice = id;
		}
	}
	
	this.hoverState = function(id) {
		if(typeof setButtonActions !== 'undefined' && id != currentlySelectedDevice) {
			$('img'+id).src=coll[id-1].iconPathOver;
		}
	}
	
	this.unHoverState = function(id) {
		if(typeof setButtonActions !== 'undefined' && id != currentlySelectedDevice) {
			$('img'+id).src=coll[id-1].iconPath;
		} else if(typeof setButtonActions !== 'undefined') {
			$('img'+id).src=coll[id-1].iconPathSelected;
		}
	}
	
	// load JSON struct
	if(builderFile != 'inline') {
		this.incFile(builderFile);
	}
	this.loadDependencies();
}












