/**
* Youmake.tv Javascript library
* ---------------------------------
* requires: prototype 1.50
*
*/

You = Class.create();

You.prototype = {	
	initialize: function () {}
}

You.Make = Class.create();

You.Make.prototype = {
	initialize: function () {}
}

You.Make.Pager = Class.create();

You.Make.Pager.prototype = {
	
	container: null, // name of the div to put the pager into
	pageSize: null, // number of items to show per page
	totalItems: null, // total number of items
	curPage: null, // current page
	nbPages: null, // total number of pages, calculated automatically ...
	itemsVisible: null, // total number of pages to show at a time ...
	
	initialize: function (container, total_items, page_size, itemsVisible) { 
		this.container = container;
		this.pageSize = page_size;
		this.totalItems = total_items;
		this.nbPages = Math.ceil(this.totalItems / this.pageSize);
		this.curPage = 1;	
		this.itemsVisible = (itemsVisible == undefined) ? 3 : itemsVisible;

		this.render();
	},
	
	render: function() {

		if(this.nbPages<=1) return;
		var i = 1;
		
		var start = ((Math.ceil(this.curPage / this.itemsVisible)  * this.itemsVisible) - this.itemsVisible) + 1;	
		if(start < 1) start = 1;
		var end   = start + this.itemsVisible - 1;	
		
		if(end > this.nbPages) { end = this.nbPages; start = this.nbPages - 2; }
		if(start < 1 ) start = 1;
		i = start;

		$(this.container).innerHTML = '';
		
		if(this.curPage > 1) {
			anchor = document.createElement("a");
			anchor.href="javascript:void(0)";
			anchor.pager = this;
			anchor.onclick = function () {this.pager.show(this.rel); }
			anchor.rel = this.curPage - 1;
			anchor.style.paddingLeft = "2px";
			anchor.style.paddingRight = "2px";
			anchor.appendChild(document.createTextNode("<"))
			$(this.container).appendChild(anchor);
			$(this.container).appendChild(document.createTextNode("  "));
		} else {
			$(this.container).appendChild(document.createTextNode("<  "));
		}
			
		while(true) {

			anchor = document.createElement("a");
			anchor.href="javascript:void(0);";
			anchor.pager = this;
			anchor.onclick = function () {this.pager.show(this.rel);  }
			anchor.rel=i;
			anchor.appendChild(document.createTextNode(i));
			if(i == this.curPage) {
				anchor.className += ' active';
			}
			$(this.container).appendChild(anchor);
			$(this.container).appendChild(document.createTextNode(" "));
		
			i++;
			if(i > end) break;
			if(i > this.nbPages) break;
			
		}
		
		if(this.curPage < this.nbPages) {
			$(this.container).appendChild(document.createTextNode("  "));
			anchor = document.createElement("a");
			anchor.href="javascript:void(0)";
			anchor.pager = this;
			anchor.rel = this.curPage+1;

			anchor.onclick = function () {this.pager.show(this.rel);  }
			anchor.appendChild(document.createTextNode(">"));

			$(this.container).appendChild(anchor);			
		} else {
			$(this.container).appendChild(document.createTextNode("  >"));
		}
	},
	
	show: function(active_page) {
		this.curPage = parseInt(active_page);
		this.render();
		this.onShowPage(this.curPage, active_page);
	},
	
	onShowPage: function(new_page) {;},
	
	setTotal: function(new_total) {
		this.totalItems = new_total;
		this.nbPages = Math.ceil(this.totalItems / this.pageSize);		
	}	
}

You.Make.Repeater = Class.create();

You.Make.Repeater.prototype = {
	container: null,
	itemTemplate: null,
	
	initialize: function (container) {
		this.container = container;	
	},
	
	render: function ( arrayOfItems ) {
		var size = arrayOfItems.length;
		var output = '';
			
		for(var i = 0; i < size; i++) {
			var item = arrayOfItems[i];
			output += this.itemTemplate.evaluate(item);
		}
		
		$(this.container).innerHTML = output;		
	}
}


You.Make.Rating = Class.create();

You.Make.Rating.prototype = {
	
	/**
	 * Mouse X position
	 * @var {Number} options
	 */
	_x: 0,
	/**
	 * Mouse X position
	 * @var {Number} options
	 */
	_y: 0,
	/**
	 * Constructor
	 * @param {Object} options
	 */
	initialize: function(options)
	{

		/**
		 * Initialized?
		 * @var (Boolean)
		 */
		this._initialized = false;

		/**
		 * Base option values
		 * @var (Object)
		 */
		this.options = {
			bindField: null,			// Form Field to bind the value to
			maxRating: 5,				// Maximum rating, determines number of stars
			container: null,			// Container of stars
			imagePath: 'http://static.youmake.tv/images/rating/',		// Path to star images
			callback: null,				// Callback function, fires when the stars are clicked
			actionURL: null,			// URL to call when clicked. The rating will be appended to the end of the URL (eg: /rate.php?id=5&rating=)
			value: 0,					// Initial Value
			locked: false
		};
		Object.extend(this.options, options);
		this.locked = this.options.locked ? true : false;
		/**
		 * Image sources for hover and user-set state ratings
		 */
		this._starSrc = {
			empty: this.options.imagePath + "star-empty.gif",
			full: this.options.imagePath + "star.gif",
			half: this.options.imagePath + "star-half.gif"
		};
		/**
		 * Preload images
		 */
		for(var x in this._starSrc)
		{
			var y = new Image();
			y.src = this._starSrc[x];
		}

		//document.getElem

		/**
		 * Images to show for pre-set values, changes when hovered, if not locked.
		 */
		this._setStarSrc = {
			empty: this.options.imagePath + "star-ps-empty.gif",
			full: this.options.imagePath + "star-ps.gif",
			half: this.options.imagePath + "star-ps-half.gif"
		};

		/**
		 * Preload images
		 */
		for(var x in this._setStarSrc)
		{
			var y = new Image();
			y.src = this._setStarSrc[x];
		}

		this.value = -1;
		this.stars = [];
		this._clicked = false;


		if(this.options.container)
		{
			this._container = $(this.options.container);
			this.id = this._container.id;
		}
		else
		{
			this.id = 'starsContainer.' + Math.random(0, 100000);
			document.write('<span id="' + this.id + '"></span>');
			this._container = $(this.id);
		}
		this._display();
		this.setValue(this.options.value);
		this._initialized = true;
	},
	_display: function()
	{
		for(var i = 0; i < this.options.maxRating; i++)
		{
			var star = new Image();
			star.src = this.locked ? this._starSrc.empty : this._setStarSrc.empty;
			star.style.cursor = 'pointer';
			star.title = 'Rate as ' + (i + 1);
			!this.locked && Event.observe(star, 'mouseover', this._starHover.bind(this));
			!this.locked && Event.observe(star, 'click', this._starClick.bind(this));
			!this.locked && Event.observe(star, 'mouseout', this._starClear.bind(this));
			this.stars.push(star);
			this._container.appendChild(star);
		}
	},
	_starHover: function(e)
	{
		if(this.locked) return;
		if(!e) e = window.event;
		var star = Event.element(e);

		var greater = false;
		for(var i = 0; i < this.stars.length; i++)
		{
			this.stars[i].src = greater ? this._starSrc.empty : this._starSrc.full;
			if(this.stars[i] == star) greater = true;
		}
	},
	_starClick: function(e)
	{
		if(this.locked) return;
		if(!e) e = window.event;
		var star = Event.element(e);
		this._clicked = true;
		for(var i = 0; i < this.stars.length; i++)
		{
			if(this.stars[i] == star)
			{
				this.setValue(i+1);
				break;
			}
		}
	},
	_starClear: function(e)
	{
		if(this.locked && this._initialized) return;
		var greater = false;
		for(var i = 0; i < this.stars.length; i++)
		{
			if(i > this.value) greater = true;
			if((this._initialized && this._clicked) || this.value == -1)
				this.stars[i].src = greater ? (this.value + .5 == i) ? this._starSrc.half : this._starSrc.empty : this._starSrc.full;
			else
				this.stars[i].src = greater ? (this.value + .5 == i) ? this._setStarSrc.half : this._setStarSrc.empty : this._setStarSrc.full;
		}
	},
	setValue: function(val)
	{
		if(this.locked && this._initialized) return;
		this.value = val-1;
		if(this.options.bindField)
			$(this.options.bindField).value = val;
		if(this._initialized)
		{
			if(this.options.actionURL)
				new Ajax.Request(this.options.actionURL + val, {onComplete: this.options['callback'], method: 'POST'});
			else
				if(this.options.callback)
					this.options['callback'](val);
		}
		this._starClear();
	}
};

You.Make.Visit = Class.create();

You.Make.Visit.prototype = {
	initialize: function(video_id) {
		new Ajax.Request('/video/log/visit?id=' + video_id);
	}
}


You.Make.IEFlickerFix = Class.create();

You.Make.IEFlickerFix.prototype = { // IE6+: Flickering CSS Rollover background switch FIX
	initialize: function() {
		if(document.all) {
			try {
	  			document.execCommand("BackgroundImageCache", false, true);
			} catch(err) {;}
		}
	}
}

You.Make.disableAutoComplete = Class.create();

You.Make.disableAutoComplete.prototype = {
	initialize: function() {
		var inputs = document.getElementsByTagName('input');
		for(var i = 0; i < inputs.length; i++) {
			inputs[i].setAttribute('autocomplete', 'off');
		}
	}
}

You.Make.ChannelVisit = Class.create();

You.Make.ChannelVisit.prototype = {
	initialize: function() {
		new Ajax.Request(
			'/json/channel/logVisit',  {
				parameters: $H({
				})
			}
		);
	}
}

You.Make.CountryPanel = Class.create();

You.Make.CountryPanel.prototype = {
	
	container: null,
	
	initialize: function(container) {
		var buffer = '';
		
		this.container = container;
		
		this.container.style.display = 'none';
		this.container.onmouseout = null;
		
		buffer += '<div class="country">';
		buffer += '<div class="name">Belgique</div>';
		buffer += '<div class="languages"><a href="?country=be&language=fr">fr</a> <a href="?country=be&language=en">en</a> <a href="?country=be&language=nl">nl</a></div>';
		buffer += '<div class="clear"></div>';
		buffer += '</div>';
		
		buffer += '<div class="country">';
		buffer += '<div class="name">Luxembourg</div>';
		buffer += '<div class="languages"><a href="?country=lu&language=lu">lu</a> <a href="?country=lu&language=fr">fr</a> <a href="?country=lu&language=en">en</a></div>';
		buffer += '<div class="clear"></div>';
		buffer += '</div>';
        	
		buffer += '<div class="country">';
		buffer += '<div class="name">Russia</div>';
		buffer += '<div class="languages"><a href="?country=ru&language=ru">ru</a> <a href="?country=ru&language=en">en</a></div>';
		buffer += '<div class="clear"></div>';
		buffer += '</div>';
	
		buffer += '<div class="tools">[<a href="javascript:country_panel.hide()">close</a>]</div>';
		
		this.container.innerHTML = buffer;
				
	},
	
	display: function(x,y) {		
		this.container.style.top = y+10 + "px";
		this.container.style.left = x-160 + "px";
		this.container.style.display = 'block';		
	},
	
	hide: function () {
		this.container.style.display = 'none';
	}
}

You.Make.CountryPanelLight = Class.create();

You.Make.CountryPanelLight.prototype = {
	
	container: null,
	
	initialize: function(container) {
		var buffer = '';
		
		this.container = container;
		
		this.container.style.display = 'none';
		this.container.onmouseout = null;
		
		buffer += '<div class="country">';
		buffer += '<div class="image"><img src="http://static.youmake.tv/images/header/countries/be.gif" border="0" /></div><div class="name"><a href="?country=be&language=en">Belgique</a></div>';
		buffer += '<div class="clear"></div>';
		buffer += '</div>';
		
		buffer += '<div class="country">';
		buffer += '<div class="image"><img src="http://static.youmake.tv/images/header/countries/lu.gif" border="0" /></div><div class="name"><a href="?country=lu&language=en">Luxembourg</a></div>';
		buffer += '<div class="clear"></div>';
		buffer += '</div>';
		
		buffer += '<div class="tools">[<a href="javascript:country_panel.hide()">close</a>]</div>';
		
		this.container.innerHTML = buffer;
				
	},
	
	display: function(x,y) {		
		this.container.style.top = y+10 + "px";
		this.container.style.left = x-160 + "px";
		this.container.style.display = 'block';		
	},
	
	hide: function () {
		this.container.style.display = 'none';
	}
}

You.Make.TabBox = Class.create();

You.Make.TabBox.prototype = {
	
	container: null,
	selected: null,
	
	initialize: function(container) {
		this.container = container;
	},
	
	switchTo: function(id) {
	
		if(this.selected != null) {
			$(this.selected).removeClassName('active');
		}
		
		$(id).addClassName('active');
		this.selected = id;
		
		this.onSwitchCallback(id);

	},
		
	onSwitchCallback: function(id) {}

}

You.Make.AjaxRequest = Class.create();

You.Make.AjaxRequest.prototype = {	
	container: null,
	channel: null,

	initialize: function(container) {
		this.container = container;
	},
	
	fetchVideos: function(type, page, video) {
		
		if(type != 'recent' && type != 'popular' && type != 'featured' && type != 'user' && type != 'similar' && type != 'latestvideo' && type != 'viewed') return;
		

		var ajax = new Ajax.Updater(
			this.container,
			'/ajax/video_listing/' + type,
			{
				parameters: $H({
					p: page,
					v: video
				}),
				evalScripts: true				
			}
		)	
	},
	
	fetchVideosChannel: function(type, page, channel,category, module) {
		
		if(type != 'user_recent' && type != 'user_popular' && type != 'user_favorite' && type != 'user_viewed' && type!='user_recent2' && type != 'cCat_recent' && type != 'cCat_popular' && type != 'cCat_viewed') return;

		if(undefined == module) module = '';
		
		var ajax = new Ajax.Updater(
			this.container,
			'/ajax/video_listing/' + type,
			{
				parameters: $H({
					p: page,
					c: channel,
					cat: category,
					module: module
				}),
				evalScripts: true				
			}
		)	
	},
	
	
	fetchVideosContest: function(type, page, channel, video, module) {
		
		if(type != 'contest_recent' && type != 'contest_popular' && type != 'contest_viewed') return;

		if(undefined == video) video = 0;
		if(undefined == module) module = '';
		
		var ajax = new Ajax.Updater(
			this.container,
			'/ajax/video_listing/' + type,
			{
				parameters: $H({
					page: page,
					c: channel,
					cat: 'all',
					video: video,
					module: module
				}),
				evalScripts: true				
			}
		)	
	}

}

You.Make.Guestbook = Class.create();

You.Make.Guestbook.prototype = {
	container: null,
	owner: null,
	lastpage: null,
	
	initialize: function(container, owner) {
		this.container = container;
		this.owner = owner;
	},
	
	show: function(page) {
		var ajax = new Ajax.Updater(
			this.container,
			'/ajax/guestbook/',
			{
				parameters: $H({
					p: page,
					channel: this.owner
				}),
				evalScripts: true				
			}
		);		
		this.lastpage = page;
	},
	
	add: function(comment) {
		var ajax = new Ajax.Updater(
			this.container,
			'/ajax/guestbook/post',
			{
				parameters: $H({
					comment: comment,
					channel: this.owner,
					lastpage: this.lastpage
				}),
				evalScripts: true				
			}
		);	
	}
	
	
}


You.Make.VideoComments = Class.create();

You.Make.VideoComments.prototype = {
	container: null,
	id: null,
	lastpage: null,
	
	initialize: function(container, id) {
		this.container = container;
		this.id = id;
	},
	
	show: function(page) {
		var ajax = new Ajax.Updater(
			this.container,
			'/ajax/video_comments/',
			{
				parameters: $H({
					p: page,
					id: this.id
				}),
				evalScripts: true				
			}
		);		
		this.lastpage = page;
	},
	
	add: function(comment) {
		var ajax = new Ajax.Updater(
			this.container,
			'/ajax/video_comments/post',
			{
				parameters: $H({
					comment: comment,
					id: this.id,
					lastpage: this.lastpage
				}),
				evalScripts: true				
			}
		);	
	}
	
	
}