// JavaScript Document

String.prototype.trim = function(){
	return this.replace(/^\s+|\s+$/, '');
};

function isDefined(variable){
    return (typeof(window[variable])=='undefined')?false:true;
}

jQuery.preloadImages = function(){
	for(var i = 0; i<arguments.length; i++){
		jQuery("<img>").attr("src", arguments[i]);
	}
}

function Menu(){

	this.block = false;
	this.item = null;
	this.obj = null;

	this.hide = function(force){
		var val = $(this.obj).attr('value');
		if (!this.block || force){
			$('div#menu-bg-'+val).hide();
			$(this.obj).removeClass('hover');
		}
	}

	this.show = function(obj){
		var val = $(this.obj).attr('value');
		$('div#menu-bg-'+val).show();
		$(this.obj).addClass('hover');
	}

	this.load = function(){
		if ($.browser.msie && $.browser.version=='6.0'){
			$('div#menu li ul li').hover(
				function(){
					$(this).addClass('hover');
				},
				function(){
					$(this).removeClass('hover');
				}
			);
		}
		$('div#menu li').hover(
			function(){
				if ($(this).parent().parent().get(0).tagName=='LI') return false;
				var val = $(this).attr('value');
				if (!val||val==-1){
					$(this).find('a img').each(function(){
						var s = $(this).attr('src');
						$(this).attr('src',s.substring(0,s.length-4)+'-.gif');
					});
				}
				Menu.block = true;
				if ($(Menu.obj).attr('value')!=val){
					Menu.hide(true);
				}
				if (val){
					Menu.obj = this;
					Menu.show();
				}
			},
			function(){
				if ($(this).parent().parent().get(0).tagName=='LI') return false;
				var val = $(this).attr('value');
				if (!val||val==-1){
					$(this).find('a img').each(function(){
						var s = $(this).attr('src');
						$(this).attr('src',s.substring(0,s.length-5)+'.gif')	
					});
				}
				Menu.block = false;
				$.timer(500,function(){
					Menu.hide();
				});
			}
		);
		$('div#menu li').click(function(){
			$(this).children('a:first').each(function(){
				//location.href = $(this).attr('href');
			});
		});
		$('div#menu ul li ul').hover(
			function(){
				Menu.block = true;
			},
			function(){
				//Menu.block = false;
				$.timer(500,function(){
					Menu.hide();
				});
			}
		);
	}

}

function Website(){

	this.path = '/';

	this.current = 0;
	this.expanded = false;

	this.page = null;

	this.lightBoxOpt = {
			overlayBgColor: '#006dc9',
			overlayOpacity: 0.9,
			imageBlank: '/img/lightbox/blank.gif',
			imageLoading: '/img/lightbox/ico-loading.gif',
			imageBtnClose: '/img/lightbox/btn-close.gif',
			imageBtnPrev: '/img/lightbox/btn-prev.gif',
			imageBtnNext: '/img/lightbox/btn-next.gif',
			txtImage: '',
			txtOf: 'из'
		};

	this.isEnter = function(e){
		var keynum;
		if (window.event){ // IE
			keynum = e.keyCode;
		} else if (e.which){ // Netscape/Firefox/Opera
			keynum = e.which;
		}
		return keynum==13;
	}

	this.load = function(){
		this.expanded = true;
		//this.current = 0;
		if (this.page=='videos'){
			this.loadPage(this.page);
		} else if (this.page=='notice'){
			this.loadPage(this.page);
		} else if (this.page=='announce'){
//			this.loadAnnounce();
		}

		// People load
		$('div.people div.people-image div.image a').hover(function(){
			$('div#people-full-image').html('<img src="'+$(this).attr('rel')+'" alt="" />');
		},function(){

		});
		$('div.people div.people-image div.image a').click(function(){
			//return false;
		});

		// Gallery load
		this.galleryLoad();

		// Programs load
		if (this.path=='/') $('div.projects div.body div.image a').lightBox(this.lightBoxOpt);
	}

	this.showAnnounce = function(){
		var m = $('div.header div.image img');
		var n = $('div.header div.text h1');
		var t = $('div.header div.text p.text');
		var d = $('div.header div.text p.clock span');
		var i = $('div.header div.text p.clock b');
		$.post(Website.path+'watch/promo',{ajax:1},function(data){
			var a = eval(data);
			m.attr('alt',a.name);
			m.attr('src',a.image);
			m.show('fast');
			n.text(a.name);
			t.text(a.text);
			d.text(a.date);
			i.text(a.time);
//			$.timer(30000,function(){
//				Website.showAnnounce();
//			});
		})
	}

	this.loadAnnounce = function(){
		Website.showAnnounce();
	}

	this.loadPage = function(s){
		var url = '';
		if (s=='videos'){
			url = Website.path+'watch/videos/'+Website.current+'/';
		} else if (s=='notice'){
			url = Website.path+'notice/'+Website.current+'/';
		}
		//var url = Website.path+'watch/'+str+'/'+Website.current+'/';
		var i = $('div.'+s+' div.pages input[name="page"]');
		var l = $('div.'+s+' div.items');
		var p = $('div.'+s+' div.pages a[rel="prev"]');
		var n = $('div.'+s+' div.pages a[rel="next"]');
		var d = $('div.'+s+' div.pages');
		var c = $('div.'+s+' div.pages span.count');
		$('div.'+s+' div.pages input[name="page"]').keypress(function(e){
			if (!Website.isEnter(e)) return true;
			Website.gotoPage(url,l,p,n,$(this),d,c);
			return false;
		});
		$('div.'+s+' div.pages a[rel="prev"], div.'+s+' div.pages a[rel="next"]').click(function(){
			if ($(this).hasClass('prevd') && $(this).attr('rel')=='prev') return false;
			if ($(this).hasClass('nextd') && $(this).attr('rel')=='next') return false;
			var str = $(this).attr('href');
			var a = str.split('/');
			i.val(a[a.length-1]);
			Website.gotoPage(url,l,p,n,i,d,c);
			return false;
		});
		$('div.'+s+' div.cat ul li a').click(function(){
			$('div.videos div.cat ul li').each(function(){
				$(this).removeClass('s top');
			})
			var str = $(this).attr('href');
			var a = str.split('/');
			url = Website.path+'watch/'+s+'/'+a[a.length-1]+'/';
			i.val(1);
			$(this).parent().addClass( $(this).text()==$('div.'+s+' div.cat ul li:first a').text() ? 'top' : 's' );
			Website.gotoPage(url,l,p,n,i,d,c);
			return false;
		});
	}

	this.galleryLoad = function(){
		if (this.path=='/')	$('div.gallery div.box a').lightBox(this.lightBoxOpt);
	}

	this.gotoPage = function(url,l,p,n,i,d,c){
		l.html('');
		$.post(url+i.val(), {ajax:1}, function(data){
			var a = eval(data);
			l.html(a.items);
			p.removeClass('prev prevd');
			n.removeClass('next nextd');
			p.attr('href',url+a.prev);
			n.attr('href',url+a.next);
			if (a.isprev) p.addClass('prev'); else p.addClass('prevd');
			if (a.isnext) n.addClass('next'); else n.addClass('nextd');
			if (a.isnext || a.isprev) d.show(); else d.hide();
			c.text(a.count);
			i.val(a.page);
		});
	}

	this.playVideo = function(a,url){
		if (!url) url = $(a).attr('href');
		$.post(url,{ajax:1},function(data){
			var a = eval(data);
			$('div.player span.name').text(a.name);
			$('div.player span.size').text(a.size);
			$('div.player span.cat').text(a.cat);
			swfobject.createSWF(
				{data: '/player.swf', width: '384', height: '310'},
				{flashvars: 'file='+a.video+'&image='+a.image, wmode: 'transparent', allowfullscreen: 'true', allowscriptaccess: 'always'},
				'video-player');
			$(window).scrollTo({top:'150px'},800);
		});
		return false;
	}

	// From HERE

	this.markDays = function(id,a){
		for (i=0; i<a.length; i++){
			var obj = $('span#show-'+id+' b:first');
			for (j=1; j<a[i]; j++){
				obj = obj.next();
			}
			obj.addClass(j>5?'r':'b');
		}
	}

	this.showFirst = function(){
		var link = null;
		$('div.people div.people-image div.image a').each(function(){
			if (!link) link = $(this).attr('rel');
		});
		if (link) $('div#people-full-image').html('<img src="'+link+'" alt="" />');
	}

	this.nav = function(obj){
		var o = $('div.gallery div.box').hide();
		var p = $('div.gallery div.pages a[rel="prev"]');
		var n = $('div.gallery div.pages a[rel="next"]');
		var url = Website.path+'tv5/gallery/page/';
		p.removeClass('prev prevd');
		n.removeClass('next nextd');
		$.post($(obj).attr('href'),{ajax:1},function(data){
			var a = eval(data);
			o.html(a.list).fadeIn('slow');
			p.attr('href',url+a.prev);
			n.attr('href',url+a.next);
			if (a.isprev) p.addClass('prev'); else p.addClass('prevd');
			if (a.isnext) n.addClass('next'); else n.addClass('nextd');
			Website.galleryLoad();
//			$('div.gallery div.box a').click(function(){
//				return Website.popup($(this).attr('href'));
//			})
		});
		return false;
	}
	
	this.poll = function(){
		var p = null;
		var o = false;
		$('div#context div.poll').each(function(){
			p = $(this);
		});
		$('div#context div.poll-open').each(function(){
			p = $(this);
			o = true;
		});
		var b = $('div#poll');
		if ($.browser.msie){
			if (window.opener){
				window.close();
			} else {
				Website.popup(Website.path+'poll',{width: 300,height:300});
			}
		} else {
			if (o){
				p.removeClass('poll-open');
				p.addClass('poll');
			} else {
				p.removeClass('poll');
				p.addClass('poll-open');
			}
		}
	}
	
	this.vote = function(){
		var val = 0;
		$('div#poll input[name="answer"]').each(function(){
			if ($(this).attr('checked')){
				val = $(this).val();
			}
		});
		if (val){
			$('div#poll ul').hide('slow');
			$('div#poll input[type="button"]').hide();
			$.post(Website.path+'poll',{ajax:1, answer:val},function(data){
				$('div#poll div.body').html(data);
				Website.drawVotes();
				$('div#poll div.body').show('slow');
			});		
		} else {
			alert('Выберите вариант ответа');
		}
	}
	
	this.drawVotes = function(){
		$('div#poll ul li div.p').each(function(){
			$(this).css('background-position', $(this).parent().attr('value')+'px top');
		});
	}

	this.expand = function(obj){
		var o = $('#'+$(obj).attr('rel'));
		if (!this.expanded){
			o.show('slow');
		} else {
			o.hide('slow');
		}
		this.expanded = !this.expanded;
		return true;
	}

	this.popup = function(url,p){
		var w = p.width || 450;
		var h = p.height || 450;
		var l = (screen.width - w) / 2;
		var t = (screen.height - h) / 2;
		var s = 'scrollbars=0,resizable=1,status=0,toolbar=0,menubar=0,location=0,left='+l+',top='+t+',width='+w+',height='+h;
		var w = window.open(url,'newWindow'+Math.floor(Math.random()*100000),s);
		return false;
	}
	
	this.sendEmail = function(page){
		var s = '';
		var f = null;
		if (page=='app'){
			f = 'div.application form';
			s = 'div.application form input,div.application form textarea';
		} else if (page=='pro'){
			f = 'div.projects form';
			s = 'div.projects form input,div.projects form textarea';
		} else {
			f = 'div.contacts form';
			s = 'div.contacts form input,div.contacts form textarea,div.contacts form select';
		}
		var par = new Object();
		par['send'] = 'send';
		par['ajax'] = 1;
		par['page'] = page
		$(s).each(function(){
			par[$(this).attr('name')] = $(this).val();
		})
		$(f).hide();
		$.post(Website.path+'business/email'+$(f).attr('action'),par,function(data){
			$(f).html(data).show();
		});
	}
	
	this.reset = function(o){
		if ($(o).val()=='Найти на сайте') $(o).val('');
	}

}

var Menu = new Menu();
var Website = new Website();