/**
 * requires:
 * 		Prototype.js, or other JS-library with basic set of extended built-in classes
 * 		seam/resource/remoting/resource/remote.js - Seam js-file
 * 		seam/resource/remoting/interface.js?profile - Seam js-file
 */

/** 
 * @namespace User Contacts
 */
if (typeof(u_contacts) == 'undefined') {
	var u_contacts = {};
}

/**
 * The path to user's images.
 * 
 * @namespace UserContacts
 * @property USERSIMAGESLIBRARY {String} the path to images library
 * @constant
 */
u_contacts.USERSIMAGESLIBRARY = contextPath + "/resources/profile/images/18x24/";

/**
 * The default image.
 * 
 * @namespace UserContacts
 * @property IMAGESNOBOBY {String} the default image
 * @constant
 */
u_contacts.IMAGESNOBOBY = contextPath + "/img/screen/nobody_m_s2.gif";

/**
 * The image of play button.
 * 
 * @namespace UserContacts
 * @property PLAYBTNIMAGE {String} the image of play button
 * @constant
 */
u_contacts.PLAYBTNIMAGE = contextPath + "/img/screen/btn_connection_play.gif";

/**
 * The image of stop button.
 * 
 * @namespace UserContacts
 * @property STOPBTNIMAGE {String} the image of stop button
 * @constant
 */
u_contacts.STOPBTNIMAGE = contextPath + "/img/screen/btn_connection_pause.gif";

/**
 * The path to user's profile.
 * 
 * @namespace UserContacts
 * @property USERPROFILEURL {String} the path to user's profile
 * @constant
 */
u_contacts.USERPROFILEURL = contextPath + "/page/loggedIn/profile/profile.seam?shortName=";


/**
 * u_contacts.Player component logic.
 * @namespace UserContacts
 * @class u_contacts.Player
 */
(function () {
	
	/**
	 * u_contacts.Player constructor.
	 * @return {u_contacts.Player} the new instance
	 * @constructor
	 */
	u_contacts.Player = function (initialElement, img, options) {		
		this.initialElement = initialElement;		
		this.img = img;
		this.options = options;
		this.playing = false;
		this.step = 0;
		this.elements = [];
		/* Seam component */
		this.connections = Seam.Component.getInstance("profile");
		this.interval = 1.0;
		this.height = $(this.initialElement).offsetHeight;
		Element.setStyle($(this.initialElement).parentNode, { width: $(this.initialElement).offsetWidth + "px", height: this.height + "px", overflow: "hidden"});
		seam_extends.changeLoadingMessage($(this.initialElement).cumulativeOffset());
		this.page = 0;
		this.totalItems = -1;
		this.isNextAvialable = true;
		this.curConversation = null;
		return this;
	};
	
	u_contacts.Player.prototype = {
		play: function Player_play() {
			this.playing = true;
			this.img.src= u_contacts.STOPBTNIMAGE;
			this.showNext();
		},
		
		stop: function Player_stop() {
			this.playing = false;
			this.img.src = u_contacts.PLAYBTNIMAGE;
		},
		
		playpause: function Player_playpause() {
			this.img.parentNode.blur();
			if (this.playing) {
				this.stop();
			} else {
				this.play();
			}
		},
		
		showNext: function Player_showNext() {
			if (!this.playing) {
				Element.setStyle($(this.initialElement), {height: this.height + "px"});
				$(this.initialElement).show();
				return;
			}
			if (this.step == this.totalItems) {
				this.isNextAvialable = false;
				this.totalItems = -1;
			}
			this.changeSlide();
		},
		
		changeSlide: function Player_changeSlide() {
			if ((this.step >= this.elements.length) && (this.isNextAvialable)) {				
				Seam.Remoting.log("Loads user connections");
				// remote calls within the current conversation scope
				Seam.Remoting.getContext().setConversationId(this.curConversation);				
				this.connections.searchUsersConnection(this.page, this.searchResults.bind(this));
				return;
			} else if ((this.step >= this.elements.length) && (!this.isNextAvialable)) {
				this.step = 0;
			}			
			this.startPlayDiv();
		},
		
		fillDivs: function Player_fillDivs() {
	//		this.step++;// = (this.step + 1) % this.elements.length;
	//		var user = this.elements[this.step];
			var childs = $(this.initialElement).childElements()[0].childElements();
			for (var i = 0, len = childs.length; i < len; i++) {
				this.fillChildDiv(childs[i], this.elements[this.step]);
				
			}
			$(this.initialElement).show();
		},
		
		fillChildDiv: function Player_fillChildDiv(childElement, user) {
			if (!user) {
				return;
			}
			var photo = (user.photo) ? u_contacts.USERSIMAGESLIBRARY + user.photo: u_contacts.IMAGESNOBOBY;
			var photoLink = childElement.select('a[class="link-photo"]')[0];
			photoLink.href = u_contacts.USERPROFILEURL + user.shortName;
			photoLink.childElements()[0].src = photo;
			var connectionLink = childElement.select('a[class="link-connection"]')[0];
			connectionLink.href = u_contacts.USERPROFILEURL + user.shortName;
			connectionLink.childElements()[0].innerHTML = user.fullName;
			childElement.select('span[class="description"]')[0].innerHTML = user.company;
			if (user.premium) {
				childElement.select('a[class="link-premium"]')[0].show();
			} else {
				childElement.select('a[class="link-premium"]')[0].hide();
			}
			if (user.moderator) {
				childElement.select('a[class="link-moderator"]')[0].show();
			} else {
				childElement.select('a[class="link-moderator"]')[0].hide();
			}
			this.step++;
		},
		
		startPlayDiv: function Player_startPlayDiv() {						
			var me = this;
			Effect.SlideUp($(this.initialElement), {
				duration: this.interval, 
				transition: Effect.Transitions.linear,
				delay: 0.3,				
				afterFinish: function() {
					me.fillDivs();
					me.showNext();
				}
			});
		},
		
		searchResults: function Player_searchResults(result) {
			if (result) {
				for (var i = 0, ln = result.length; i < ln; i++) {
					for(var k = 0, len = result[i].length; k < len; k++) {
						this.elements.push(result[i][k]);
						if (result[i][k].photo) {
							(new Image(18, 30)).src = u_contacts.USERSIMAGESLIBRARY + result[i][k].photo;
						} else {
							(new Image(18, 30)).src = u_contacts.IMAGESNOBOBY;
						}
					}
				}
				this.page++;
				// skip the first element
				this.step++;				
				this.startPlayDiv();
			}
		},
		
		setTotalItems: function Player_setTotalItems(totalItems) {
			this.totalItems = totalItems;
		},
		
		setCurConversation: function  Player_setCurConversation(curConversation) {
			this.curConversation = curConversation;
		}
	};	
})();
