// JavaScript Document


var iSHOW = {

	Init: 		function(_id, _iconID, _url, _width, _height, _delay, _mode) {
//cDebug("Init");
					if (!_id) { alert("iSHOW requires an element ID!"); return false; } 
					this._id = _id;

					if (!_url) { alert("iSHOW requires an ajax/json URL!"); return false; } 
					this.url = _url;					
					
					if (!_width || !_height) { alert("iSHOW requires a width and height!"); return false; } 
					this._width = _width;
					this._height = _height;
					
					this._delay = _delay;
					if (!_delay) { this._delay = 3000; }
					
					this.array = [];
					this.i = 0;
					
					this.id = "iSHOW-"+ Math.round(10000 * Math.random(100000));
					this._iconID = _iconID;
					this.mode = _mode;
					
					this.Load();
	},
	
	Mode: 		function(_mode) { this.mode = _mode; },
	
	Load:		function() {
cDebug( this.url +"?mode="+ this.mode );
					$.get(this.url +"?mode="+ this.mode,
						function(xml) {
cDebug(xml);
							$(xml).find("item").each(
							function(i) {
								var _img = $(this).find("image").text();
								var _url = $(this).find("url").text();
								var _title = $(this).find("alt").text();
								var _icon = $(this).find("icon").text();
								iSHOW.array.push([_img, _url, _title, _icon]);
							});
						if (xml) {
							iSHOW.Icons();
							iSHOW.Play();
						}
					});
	},
	
	Play:		function() {
//cDebug(this.array);

					//$("div[id^="+ this.id +"-]").hide();

					if (!$("#"+ this.id).attr("id")) {
						$(this._id).html(''+
							'<div id="'+ this.id +'" style="display:inline-block; position:relative; width:'+ this._width +'px; '+
							'height:'+ this._height +'px; overflow:hidden;"></div>');
					}
				
					if (!$("#"+ this.id +'-'+ this.i).attr("id")) {
						$("#"+ this.id)
							.append(''+
							'<div id="'+ this.id +'-'+ this.i +'" style="position:absolute; left:'+ $(this._id).width() +'px;">'+
							'<a href="'+ this.array[this.i][1] +'" title="'+ this.array[this.i][2] +'">'+
							'<img src="'+ this.array[this.i][0] +'" border="0" />'+
							'</a>');
					}
					
					
					
//					if ($("#"+ this.id +'-'+ (this.i-1))) {
//						$("#"+ this.id +'-'+ (this.i-1))
//							.animate({left: '-'+ $(this._id).width()});
//					}
					
					$("#"+ this.id +'-'+ this.i)
						.css({'left':$(this._id).width() +'px'})
						.animate({ 
  							"opacity": "show",
							left: 0
						}, 
						{ 
						duration: "slow", 
						complete:
							function() {
							var _i = iSHOW.i - 1;
							if (_i < 0) { _i = iSHOW.array.length - 1; }
							$("#"+ iSHOW.id +'-'+ _i).empty().remove();
											
							iSHOW.i++;
							if (iSHOW.i >= iSHOW.array.length) { iSHOW.i = 0; }

							iSHOW.timeoutID = setTimeout(
							function() {
								iSHOW.Play();
							}, iSHOW._delay);
						}
						});

	},
	
	
	Icons:		function() {
//cDebug(this.array);
					for (var i = 0; i < this.array.length; i++) {
						$(this._iconID)
							.append(''+
							'<a class="'+ this.id +'-icon" href="javascript:;" title="'+ this.array[i][2] +'" '+
							'style="margin-right:10px;" ishowid="'+ i +'" >'+
							'<img src="'+ this.array[i][3] +'" border="0"/>'+
							'</a>');
					}
					
					$("a."+ this.id +"-icon")
						.bind("click",
							function() {
							clearTimeout(iSHOW.timeoutID);
							iSHOW.Show($(this).attr("ishowid"))	
							})			
						.find("> img")
						.hover(
							function() {
								var o = $(this).attr("src");
								$(this).attr("src", o.replace(".jpg", ".o.jpg"));
							},
							function() {
								var o = $(this).attr("src");
								$(this).attr("src", o.replace(".o.jpg", ".jpg"));
							}
						);
					
	},
	
	Show:		function(_i) {
					var _id = this.id +'-'+ _i;
					$("#"+ this.id)
						.append(''+
						'<div id="'+ _id +'" style="position:absolute; left:'+ $(this._id).width() +'px;">'+
						'<a href="'+ this.array[_i][1] +'" title="'+ this.array[_i][2] +'">'+
						'<img src="'+ this.array[_i][0] +'" border="0" />'+
						'</a>');					
						
					$("div[id="+ _id +"]:last")
						.animate({ 
  							"opacity": "show" 
						}, 
						{ 
						duration: "slow", 
						complete:
						function() {
							var _i = $("div[id="+ _id +"]").length;
							$("div[id="+ _id +"]").each(
							function(i) {
								if (i+1 != _i) {
									$(this).empty().remove();
								}
							});
						}
						});
	},
	
	LoadStyle:	function (_path) {
					var _ok = 1;
					var _head = document.getElementsByTagName('head').item(0);
					var _script = _head.getElementsByTagName('link');
					for (var i=0; i < _script.length; i++) {
						if (_script[i].href) {
							var _src = _script[i].href.split('/');
							if (_src) {
								if (_src[_src.length -1].toString().indexOf(_path) > -1) {
									_ok = 0;
								}
							}
						}
					}	
					if (_ok) {
						var Node = document.createElement('link');
						Node.type = 'text/css';
						Node.rel = 'stylesheet';
						Node.href = _path;
						Node.media = 'screen';
						_head.appendChild(Node);
					}
					return true;
	}
	
	
	
	
};


