(function () {

     var WebcamsDashboardManager = {

          containers : {
               active : null,
               history : null,
               navigation : null,
               defaults : null
          }
          ,
          historyItemsLimit : 5,
          history : [],
          activeWebcam : null,
          toString : function () {
               return 'WebcamsDashboardManager';
          }
          ,

          init : function (settings) {
               _loadSettings.call(this, settings);
               if (this.containers.defaults) {
                    _loadDefaults.call(this);
               }
               _attachBehaviour.call(this);
               _render.call(this);
          }
          ,

		  swapWebcam : function (descriptor) {
			repeated = 0;
			TurnOff();
			document.getElementById('campicture').className = "campictureLoad";
			
			if(isNaN(parseInt(descriptor.itemHolder))) {descriptor.itemHolder=0}
			
			// set the active webcam
			tmpActiveWebcam = this.activeWebcam;
			tmpActiveWebcamSrc = tmpActiveWebcam.src.split("&");
            tmpActiveWebcamSrc = tmpActiveWebcamSrc[0] + tmpActiveWebcamSrc[1];
			activeWebcamSrc = this.activeWebcam.src.split("&");
            activeWebcamSrc = activeWebcamSrc[0] + activeWebcamSrc[1];
			
			this.activeWebcam = descriptor;
			this.containers.active.getElementsByTagName('div')[0].innerHTML = "<div class=locationTxt>" + descriptor.location + "</div>";
			_renderActiveWebcam.call(this);
			// - end of set
			
			var historyItemHolders = this.containers.history.getElementsByTagName('li');
			var item = tmpActiveWebcam;

				var historySrc = new Array();
	            
				for(a=0;a<this.historyItemsLimit;a++) {
					historySrc[a] = this.history[a].src.split("&");
					historySrc[a] = historySrc[a][0] + historySrc[a][1];
					if(tmpActiveWebcamSrc == historySrc[a]) repeated++;
                }
				
				if(repeated==0) {
					historyItemHolders[descriptor.itemHolder].src = tmpActiveWebcam.src;
					this.history[descriptor.itemHolder] = tmpActiveWebcam;
					_renderWebcam({
		                    container 	: historyItemHolders[descriptor.itemHolder],
		                    location 	: tmpActiveWebcam.location,
		                    url 		: "javascript:NI.ShareWebcam.WebcamsDashboardManager.swapWebcam({url:'"+item.url+"', src:'"+item.src+"', location:'"+item.location+"', itemHolder:'"+descriptor.itemHolder+"'});preloadHistory('"+item.location+"');TurnOff();", //item.url,
		                    src 		: tmpActiveWebcam.src+"&size=small", 
							idd			: descriptor.itemHolder
							}
					);
				}
		  }
     }
     ;
	 

     var _loadSettings = function (settings) {
          this.containers = {
               active 		: settings.containers.active,
               history 	    : settings.containers.history,
               navigation 	: settings.containers.navigation,
               defaults 	: settings.containers.defaults
          }
          ;
          this.historyItemsLimit = settings.historyItemsLimit || this.historyItemsLimit;
     }
     ;

     var _attachBehaviour = function () {
          var navigationLinks = _getNavigationLinks.call(this);
          for (var i = 0, length = navigationLinks.length;
          i < length;
          i++) {
               _attachWebcamLinkClickHandler.call(this, navigationLinks[i]);
          }
     }
     ;

     var _loadDefaults = function () {
          var webcamLinks = this.containers.defaults.getElementsByTagName('a');
          for (var i = 0, length = webcamLinks.length;
          i < length;
          i++) {
               if (i < this.historyItemsLimit) {
                    var descriptor = _getLinkDescriptor(webcamLinks[i]);

                    if(i!=4) {
                         _addWebcamToHistory.call(this, descriptor);
                    }

               }
               else {
                    break;
               }
          }
          if (this.history.length > 0) {

               this.activeWebcam = descriptor;
               this.containers.active.getElementsByTagName('div')[0].innerHTML = "<div id=locationTxt class=locationTxt>" + descriptor.location + "</div>";
          }
     }
     ;

     var _getNavigationLinks = function () {
          var linksListHolder = this.containers.navigation.getElementsByTagName('ol')[0];
          if (linksListHolder != null) {
               return linksListHolder.getElementsByTagName('a');
          }
     }
     ;

     var _attachWebcamLinkClickHandler = function (link) {
          link.onclick = (function (webcamsDashboardManager) {
               return function () {
					webcamsDashboardManager.swapWebcam(_getLinkDescriptor(this));
					preloadHistory(webcamsDashboardManager.activeWebcam.location);
                    return false;
               }
               
               ;
          }
		  	
          )(this);
		  link.onmousemove = function (evt) {popupMove(evt, link, true)};
		  link.onmouseover = function (evt) {showCamPopup(link, true)};
		  link.onmouseout  = function (evt) {hideCamPopup()};
     }
     ;

     var _getLinkDescriptor = function (link) {
          var parentContainer = (function (name) {
               var node = this.parentNode;
               while (node != null) {
                    if (node.nodeName.toLowerCase() == name.toLowerCase()) {
                         break;
                    }
                    else {
                         node = node.parentNode;
                    }
               }
               return node;
          }
          ).call(link, 'li');

          var descriptor = {
               url : link.getAttribute('href'),

               src : NI.Utils.getElementsByClassName({
                    target : parentContainer,
                    className : 'Source',
                    isRecursive : true
               }
               )[0].value,

               location : NI.Utils.getElementsByClassName({
                    target : parentContainer,
                    className : 'Location',
                    isRecursive : true
               }
               )[0].value
          }
          ;

          return descriptor;
     }
     ;

     var _addWebcamToHistory = function (descriptor) {

          if (this.history.length > this.historyItemsLimit) {
               this.history.pop();
          }

          if(this.history.length<=3) {
               this.history.unshift(descriptor);
          }

          else {
               h1 = this.activeWebcam.src.split("&");
               h2 = this.history[0].src.split("&");

               h1 =h1[0]+"&"+h1[1];
               h2 =h2[0]+"&"+h2[1];

               if(h1 != h2) {
                    this.history.unshift(this.activeWebcam);
               }
          }
     }
     ;

     var _render = function () {
        _renderActiveWebcam.call(this);
        _renderHistory.call(this);
     }
     ;

     var _renderActiveWebcam = function () {
          if (this.activeWebcam) {
               _renderWebcam({
                    container 	: this.containers.active,
                    location 	: this.activeWebcam.location,
                    url 		: this.activeWebcam.url,
                    src 		: this.activeWebcam.src+"&size=medium"
               }
               );
          }
     }
     ;

     var _renderHistory = function () {
        var historyItemHolders = this.containers.history.getElementsByTagName('li');
		
          for (var i = 0, length = historyItemHolders.length; i < length; i++) {
               if (i < this.history.length) {

                    var item = this.history[i];
                    _renderWebcam({
                         container 	: historyItemHolders[i],
                         location 	: item.location,
                         url 		: "javascript:document.getElementById('campicture').className='campictureLoad';NI.ShareWebcam.WebcamsDashboardManager.swapWebcam({url:'"+item.url+"', src:'"+item.src+"', location:'"+item.location+"', itemHolder:'"+i+"'});preloadHistory('"+item.location+"');", //item.url,
                         src 		: item.src+"&size=small",
                         idd     : i

                    }
                    );
               }
               else {
                    break;
               }
          }
     }
     ;
	
     var _renderWebcam = function (descriptor) {

          var locationHolder = NI.Utils.getElementsByClassName({
               target : descriptor.container, className : 'Location'
          }
          )[0];
			

          var webcam = document.createElement('a');
          webcam.className = 'Webcam';
          webcam.id = 'cam'+descriptor.idd+'';
          webcam.setAttribute('href', descriptor.url);
          //webcam.setAttribute('title', descriptor.location);
          var webcamImage = webcam.appendChild(document.createElement('img'));
          webcamImage.src = descriptor.src;
		  
		  if(descriptor.container.className != 'Container Active') {
			webcam.onmousemove = function (evt) {popupMove(evt, descriptor.src)};
			webcam.onmouseover = function (evt) {showCamPopup(descriptor.src, false)};
			webcam.onmouseout  = function (evt) {hideCamPopup()};
		  }
		  
		
          if(descriptor.container.className == 'Container Active') {
               webcamImage.id = 'campicture';
			   //webcamImage.className = 'campictureLoad';
               document.getElementById('currentCam').value = descriptor.src;
          }
          else {
               locationHolder.innerHTML = "<div id=locationTxt class=locationTxt>" + descriptor.location + "</div>";
          }


          var webcamPlaceHolder =  NI.Utils.getElementsByClassName({
               target : descriptor.container, className : 'Webcam'
          }
          )[0];
		  
          descriptor.container.replaceChild(webcam, webcamPlaceHolder);
     }
     ;

     NI.ShareWebcam.importComponent(WebcamsDashboardManager);

}
)();
