/*

	// naming convention references
	
window.globalVariable = true;

var ClassName = function(){	// class object (upper case)
	var privateProperty = true;
	this.publicProperty = true;
	var privateMethod = function(){};
	this.publicMethod = function(){};
	
	var privateMethod2 = function(){
		alert(privateProperty);	// works
		alert(this.publicProperty);	// doesn't work - private methods can't see public properties
	};
}
ClassName.publicStaticMethod = function(){};
ClassName.publicStaticProperty = true;
ClassName.PUBLICSTATICKEYWORD = 'public static keyword';

ClassName.prototype.publicMethod2 = function(){
	// another way to do public methods
};

var className = new ClassName(); // instance of class (lower case)

variable = [1,2,3,4];	// normal variable
$variable = $([1,2,3,4]);	// jQuery variable

*/

var SNS = function(_loader, _content, _xmlURL, _optionsObject){
	
	var xmlURL = _xmlURL;
	var optionsObject = (_optionsObject) ? _optionsObject : new SNS.OptionsObject();
	
	this.init = function(){
		this.showLoader();
		_this.$content.html('');
		loadXML(xmlURL);
	};
	
	var $xmlDoc;
	
	var loadXML = function(_xmlURL){
		$.ajax({
			url:_xmlURL,
			dataType:'xml',
			success:function(data, textStatus, jqXHR){
				parseXML($xmlDoc = $(data));
			}
		});
	}
	
	var loaderCount = -1;
	var parseXML = function(_$xmlDoc){

		var $rootNode = _$xmlDoc.find('slides');
		optionsObject.timerDelay = $rootNode.attr('rotateDelay') * 1000;
		
		if($rootNode.attr('showNav') == 'true'){
			optionsObject.showNav = true;
			optionsObject.navContainer.html('');
		}else{
			optionsObject.showNav = false;
			if(optionsObject.navContainer != undefined){
				optionsObject.navContainer.remove();
			}
		}
		
		var $itemsXML = _$xmlDoc.find('item');
		loaderCount = $itemsXML.length;
		$itemsXML.each(function(){
			var $itemXML = $(this);
			var $itemHTML = new SNS.$SlideItem();
			
			var $itemTitle = $itemXML.find('title');
			$('p.title a', $itemHTML).css({color:$itemTitle.attr('color')}).html($itemTitle.text());
			
			var $itemDescription = $itemXML.find('description');
			$('p.description a', $itemHTML).css({color:$itemDescription.attr('color')}).html($itemDescription.text());
			
			$('p a', $itemHTML).attr('href', $itemXML.attr('linkURL'));
			
			if(optionsObject.showNav == true){
				var $navHTML = new SNS.$NavItem();
				
				function navClickHandler(){
					var _currentSlideIndex = $(this).parent().index();
					if (_currentSlideIndex != _this.currentSlideIndex) _this.rotateTo(_currentSlideIndex);
				};
				
				$('button', $navHTML).text($itemXML.index()+1).click(navClickHandler);
				optionsObject.navContainer.append($navHTML);
			}
			
			var $img = $(new Image());
			var imgSrc = $itemXML.attr('bgImage');
			$img.load(function(){
				loaderProgressCheck($(this));
			}).attr('src', imgSrc);
			$itemHTML.append($img);
			_this.$content.append($itemHTML);
		});
	}
	
	var loaderProgressCheck = function($img){
		loaderCount--;
		var imgSrc = $img.attr('src');
		var $li = $img.parent('li');
		$li.css('background-image', 'url("'+imgSrc+'")');
		$img.remove();
		if(loaderCount == 0){
			$.proxy(optionsObject.onInit, _this)();
			startRotation();
		}
	}
	
	var autoRotate = optionsObject.autoRotate;
	var timerIndex;
	this.currentSlideIndex = 0;
	
	var startRotation = function(){
		var $items = $('li', _this.$content);
		$items.hide();
		_this.rotateTo(_this.currentSlideIndex);
		timerIndex = setInterval(autoRotateTick,optionsObject.timerDelay)
		_this.showContent(true);
	}
	
	this.pauseAuto = function(yesPauseIt){
		autoRotate = (yesPauseIt == true || yesPauseIt == undefined) ? false : true;
	}
	
	var autoRotateTick = function(timerLag){
		if(autoRotate == true){
			var _currentSlideIndex = _this.currentSlideIndex + 1;
			var maxIndex = $('li', _this.$content).length - 1;
			if(_currentSlideIndex > maxIndex){
				_currentSlideIndex = 0;
			}
			_this.rotateTo(_currentSlideIndex);
			initialRotation = false;
		}
	}
	
	this.rotateTo = function(_currentSlideIndex){
		var $items = $('li', _this.$content);
		
		var $previousItem = $items.eq(_this.currentSlideIndex);
		
		_this.currentSlideIndex = _currentSlideIndex;
		
		var $currentItem = $items.eq(_this.currentSlideIndex);
		
		$.proxy(optionsObject.onRotate, _this)($previousItem, $currentItem);
	}
	
	this.$loader = $(_loader);
	var loaderState = false;
	this.showLoader = function(_loaderState){
		showView(_this.$loader, loaderState, _loaderState);
		showView(_this.$content, contentState, (_loaderState == true || _loaderState == undefined) ? false : true);
	}
	
	this.$content = $(_content);
	var contentState = false;
	this.showContent = function(_contentState){
		showView(_this.$content, contentState, _contentState);
		showView(_this.$loader, loaderState, (_contentState == true || _contentState == undefined) ? false : true);
	}
	
	var showView = function($view, stateVar, stateVal){
			// set state and hide or show
		if(stateVar = (stateVal == false) ? false : true){
			$view.fadeIn();
		}else{
			$view.fadeOut();
		}
	}
	
	var _this = this;
	this.init();
}
SNS.$SlideItem = function(){return $('<li class="sns-item"><div class="overlay"></div><p class="title"><a href="">Title Text Line</a></p><p class="description"><a href="">Subject short description text paragraph multiline</a></p></li>');}
SNS.$NavItem = function(){return $('<li><button type="button">0</button></li>');}
SNS.OptionsObject = function(){
	this.timerDelay = 6500;
	this.showNav = true;
	this.navContainer;
	this.autoRotate = true;
	this.onInit = function(){};	// these are proxied through jQuery so 'this' returns the relevant sns instance
	this.onRotate = function(){};
}
