$(
	function(){

		jQuery.extend(jQuery.easing, {
			shoot: function (a, t, b, c, d) {
				return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
			},
			reveal: function (a, t, b, c, d) {
				return c*((t=t/d-1)*t*t + 1) + b;
			},
			slide_move: function(a, t, b, c, d, s){
				if(s == undefined) s = 1.70158;
				if((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b;
				return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;
			}
		});
		
		moveAdditionalLinks();
		$(window).resize(function(){
			moveAdditionalLinks();
		})

		$(".switcher[rel='dynamic']").each(function(){
			new DynamicTabs();
		})

		FadeUtils.init();

		var mv = new MoviePreview();
		var f = function () { mv.close(); }
		FadeUtils.attachEvent(f);
		
	}
);



function moveAdditionalLinks() {
	return;
	try {
		//offset = $('#navigation li.bureau').offset().left - 19;
		offset = $('#header_top_phrase').width() + 50;
		$('#additional_links').css('left', offset);
	} catch (e) {}
}


// -----------------------------------------------------------------------------------


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() });
	}
}


// -----------------------------------------------------------------------------------


function MoviePreview () {

	var o = this;
	this.player = $('.popup_layer');
	this.movies = {};
	this.params = {bgcolor: '#000000', allowFullScreen: 'true', allowScriptAccess: 'always'};
	this.selected = null;
	this.opened = false;

	$("a[rel='movie_link']").each(function(){
		var link = $(this);
		var name = link.attr('mname');
		var url = link.attr('file');
		//link.attr('href', '#' + name);
		link.click(function() {
			o.show(name, url);
			return false;
		})
	})

	this.player.find('ul.items li').each(function(){
		var l = $(this);
		var width = l.attr('width');
		var height = l.attr('height')
		o.movies[l.attr('name')] = {
			layer: l,
			width: (width ? width : 460),
			height: (height ? height : 380),
			loaded: false
		};
	})
	this.player.find('a.close').click(function(){
		o.close();
	})
	
	$(window).resize(function(){
		o.move();
	})
}

MoviePreview.prototype = {

	show: function (name, url) {
		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: url};
			new swfobject.embedSWF('/player/uppod.swf', name + '_movie_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.selected = name;
		this.opened = true;

		FadeUtils.show();
		this.player.show();
		this.move();
	},

	close: function () {
		FadeUtils.hide();
		this.player.hide();
		this.opened = false;
	},
	
	move: function () {
		if (this.opened) {
			var y = ($(window).height() - this.player.height())/2 + getPageScroll()[1];
			this.player.css('top', y);
		}
	}
}



// -----------------------------------------------------------------------------------



var Utils = {

	parseUrl: function (url) {
		
		var parseValue = (url ? url : document.location.href);
		var split = parseValue.toString().split('#');
		return {
			link: split[0],
			anchor: decodeURIComponent(split[1])
		}
		
	}
}


var Cookie = {

	set: function (sName, vValue, dExpires) {
		document.cookie = sName + "=" + escape(vValue) +
		(dExpires == null ? "" : ("; expires=" + dExpires.toGMTString())) + "; path=/";
	},

	get: function (sName) {
		return StringUtils.pairsStringGetValue(document.cookie, sName);
	}
}


var MessageAttachments = {
	
	attachmentsCount: 0,
	
	maxActtachments: 10,
	
	switchAttachs: function () {
		var A = $('#multipleAttachments');
		if (A.hasClass('hidden')) {
			A.removeClass('hidden');
			if (!this.attachmentsCount) {
				this.addAttach();
			}
		} else {
			A.addClass('hidden');
		}
	},

	addAttach: function () {
		// проверяем, указаны ли файлы в других полях
		var error = false;
		var form = document.forms['feedback_form'];
		for (var i = 1; i < form.attachment.length; i++) {
			if (!form.attachment[i].value) {
				this.checkInput(form.attachment[i]);
				error = true;
				break;
			}
		}
		if (!error && this.attachmentsCount < this.maxActtachments) {
			this.attachmentsCount++;
			var A = $('#files_template').clone().appendTo('#multipleAttachments ul');
			A.find('input').attr('name', 'attachment' + this.attachmentsCount)
			A.removeClass('hidden');
			if (this.attachmentsCount == this.maxActtachments) {
				A.find('span.add-file').remove();
			}
		}
	},

	removeAttach: function (A) {
		if (this.attachmentsCount == this.maxActtachments) {
			$('#multipleAttachments ul li:last').remove();
		} else {
			var B = $(A).parent();
			B.remove();
		}
		this.attachmentsCount--;
		if (!this.attachmentsCount) {
			this.switchAttachs();
		}
	},
	
	checkInput: function (input) {
		var A = $(input);
		var B = A.parent().children('.file_message');
		if (!input.value) {
			B.show();
		} else {
			B.hide();
		}
	}
}







// -----------------------------------------------------------------------------------

//
// 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;
}
