(function($){
	$.fn.button = function () {
		$(this).each(function(){
			var btn = $(this);

			// this is submit button
			if (btn.hasClass('submit')){
				btn.click(function(){
					if (!$(this).hasClass('disabled')){
						$(this).parents('form:first').submit()
					}
				})
			}	
			if ($.browser.msie){
				btn.unselectable()
			}
			btn
				.mousedown(function(){
					if (!$(this).hasClass('disabled')){
						$(this).addClass('active')
					}
				})
				.mouseup(function(){ $(this).removeClass('active') })
				.blur(function(){ $(this).removeClass('active') })
				//.bind('change', function(){})
		})
		return this;
	},
	$.fn.disable = function () {
		$(this)
			.addClass('disabled')
			//.trigger('change')
	},
	$.fn.enable = function () {
		$(this)
			.removeClass('disabled')
			//.trigger('change')
	}
})(jQuery);

$(function(){
	$('a.skew_button').button();
			
	FadeUtils.init();

	var mv = new MoviePreview();
	var f = function () { mv.close(); }
	FadeUtils.attachEvent(f);
	
	try {
		$('ul.gallery li a').each(function(i){
			elem = $(this)
			elem.attr('rel', 'gallery'+1);
			elem.fancybox({
				'transitionIn'	: 'elastic',
				'transitionOut'	: 'elastic'
			});
		})
	} catch(e){}
});


// -----------------------------------------------------------------------------------


function MoviePreview () {

	var o = this;
	this.player = $('#preview');
	this.movies = {};
	this.indexes = [];
	this.params = {bgcolor: '#000000', allowFullScreen: 'true', allowScriptAccess: 'always'};
	this.selected = null;
	this.selectedIdx = -1;
	this.total = 0;
	this.opened = false;
	
	this.icons = this.player.find('div.icons');
	this.nav = this.player.find('ul.navigation');
	this.prevLink = this.nav.find('li.l a');
	this.nextLink = this.nav.find('li.r a');
	this.prevIndex = 0;
	this.nextIndex = 0;

	$("a.movie_link").each(function(i){
		var link = $(this);
		var name = link.attr('name');
		var url = link.attr('file');
		var width = link.attr('width');
		var height = link.attr('height');
		o.movies[name] = {
			url: url,
			index: i,
			link: link,
			width: (width ? width : 480),
			height: (height ? height : 360),
			loaded: false
		}
		link.attr('href', '#' + name);
		link.click(function() {
			o.show(name);
			return false;
		})
	})

	this.player.find('ul.panes li').each(function(i){
		var l = $(this);
		var name = l.attr('class');
		var icon = o.icons.find('img.' + name)
		if (o.movies[name]) {
			o.movies[name].layer = l;
			o.indexes[i] = {name: name, icon: icon};
			o.total++;
		}
	})
	this.player.find('a.close').click(function(){ return o.close() })
	this.prevLink.click(function(){ return o.showPrev() })
	this.nextLink.click(function(){ return o.showNext() })

	$(window).resize(function(){
		o.move();
	})
}

MoviePreview.prototype = {

	show: function (name) {
		var item = this.movies[name];
		if (!item) {
			return;
		}
		if (!item.loaded) {
			var style = '/player/video_' + item.width + '.' + item.height + '.txt';
			var flashvars = {st: style, file: item.url};
			new swfobject.embedSWF('/player/uppod.swf', name + '_holder', item.width, item.height, '9.0.0', false, flashvars, this.params);
			item.loaded = true;
		}
		if (this.selected) {
			this.movies[this.selected].layer.hide();
		}
		item.layer.show();
		this.player.removeClass();
		this.player.addClass('w' + item.width);
		this.selected = name;
		this.selectedIdx = item.index;
		this.opened = true;
		
		FadeUtils.show();
		this.player.show();
		this.move();
		this.updateNav();
	},
	
	showNext: function () {
		this.showByIndex(this.nextIndex);
		return false;
	},
	
	showPrev: function () {
		this.showByIndex(this.prevIndex)
		return false;
	},
	
	showByIndex: function (index) {
		var item = this.indexes[index];
		if (item) {
			this.show(item.name);
		}
	},

	close: function () {
		FadeUtils.hide();
		this.player.hide();
		this.opened = false;
		return false;
	},

	updateNav: function () {
		if (this.selectedIdx < 0) {
			return;
		}
		this.prevIndex = (this.selectedIdx == 0 ? this.total - 1 : this.selectedIdx - 1);
		this.nextIndex = (this.selectedIdx == this.total - 1 ? 0 : this.selectedIdx + 1);

		var prevItem = this.indexes[this.prevIndex];
		var nextItem = this.indexes[this.nextIndex];
		if (prevItem) {
			this.prevLink.find('span').empty();
			this.prevLink.attr('href', '#' + prevItem.name);
			prevItem.icon.clone().prependTo(this.prevLink.find('span'));
		}
		if (nextItem) {
			this.nextLink.find('span').empty();
			this.nextLink.attr('href', '#' + nextItem.name);
			nextItem.icon.clone().prependTo(this.nextLink.find('span'));
		}
	},
	
	move: function () {
		if (this.opened) {
			var x = ($(window).width() - this.player.width())/2;
			var y = ($(window).height() - this.player.height())/2 + getPageScroll()[1];
			this.player.css('left', x);
			this.player.css('top', y);
		}
	}
}


// -----------------------------------------------------------------------------------


FadeUtils = {

	showed: false,

	init: function () {
		var o = this;
		this.oFade = $('#fade');
		this.oFade.css('opacity', 0.7);
		$(window).resize(function(){
			o.resize();
		})
	},

	show: function () {
		this.showed = true;
		this.oFade.show();
		this.resize();
	},
	
	resize: function () {
		this.oFade.height(getPageSize()[1]);
	},
	
	hide: function () {
		this.showed = false;
		this.oFade.hide();
	},
	
	attachEvent: function (func) {
		this.oFade.click(function(){ func() });
	}
}


// -----------------------------------------------------------------------------------


//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.com
//
function getPageScroll(){

	var xScroll, yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;	
	}

	arrayPageScroll = new Array(xScroll,yScroll) 
	return arrayPageScroll;
}



// -----------------------------------------------------------------------------------

//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.com
// Edit for Firefox by pHaez
//
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}

	arrayPageSize = [pageWidth,pageHeight,windowWidth,windowHeight];
	return arrayPageSize;
}



// -----------------------------------------------------------------------------------

function formatNumber(value) {
	value += '';
	var x = value.split('.');
	var x1 = x[0];
	var x2 = x.length > 1 ? ',' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + '&nbsp;' + '$2');
	}
	return x1 + (x2 != ',00' ? x2 : '');
}
