jQuery(document).ready(function(){
	var oMidnightReg = new MidnightReg();
	oMidnightReg.init();
	
	var oEditoor = new Editor();
	oEditoor.init();
	
	var oVotes = new Votes();
	oVotes.init();
	

	if ( jQuery('#flash').length > 0){
		var params = {
			bgcolor: "#000000"
		}
		swfobject.embedSWF("/flash/midnight/sphere561.swf", "flash", "960", "580", "9.0.0", "/flash/expressInstall.swf","",params)
	}
	if ( jQuery('#tour').length > 0){
		var params = {
			bgcolor: "#000000",
			url: "/flash/midnight/bot.flv"
		}
		swfobject.embedSWF("/flash/midnight/video3.swf", "tour3", "940", "539", "9.0.0", "/flash/expressInstall.swf","",params)
		swfobject.embedSWF("/flash/midnight/video4.swf", "tour4", "940", "539", "9.0.0", "/flash/expressInstall.swf","",params)
		swfobject.embedSWF("/flash/midnight/video5.swf", "tour5", "940", "539", "9.0.0", "/flash/expressInstall.swf","",params)
		swfobject.embedSWF("/flash/midnight/video6.swf", "tour6", "940", "539", "9.0.0", "/flash/expressInstall.swf","",params)
		swfobject.embedSWF("/flash/midnight/final.swf", "final", "940", "539", "9.0.0", "/flash/expressInstall.swf","",params)
	}
	if (jQuery('.about').length > 0){
		var params = {
			flashvars: 'picture=/images/midnight/preview.jpg&video=/videos/midnight/tv.flv'
		}
		swfobject.embedSWF("/flash/player.swf", "video", "603", "339", "9.0.0", "/flash/expressInstall.swf","",params)
		var params2 = {
			flashvars: 'picture=/images/midnight/preview2.jpg&video=/videos/midnight/tv2.flv'
		}
		swfobject.embedSWF("/flash/player.swf", "video2", "452", "339", "9.0.0", "/flash/expressInstall.swf","",params2)
	}
	if ( jQuery('#mp3').length > 0){
		var vars = {
			flashvars: 'title=Manicure – About girl&mp3=/videos/midnight/manicure.mp3'
		}
		swfobject.embedSWF("/flash/midnight/mp3player.swf", "mp3", "300", "40", "9.0.0", "/flash/expressInstall.swf","", vars)
	}
	
	
	var regexS = "[\\?&]moveto=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results != null )
    oMidnightReg.moveToBottle(results[1]);
});
function MidnightReg(){
	
}
MidnightReg.prototype = {
	init:function(){
		this.ItemWidth = 116 + 30
		this.stop = false
		this.itemNum = jQuery('.participants li').length;
		this.participantsWidth = this.itemNum * this.ItemWidth + 50
		jQuery('.participants').css('width', this.participantsWidth + 'px')
		// jQuery('.participants img').remove()
		this.attachEvents()
	},
	attachEvents:function(){
		_my = this
		
		jQuery('.go_battle').click(function(){
			window.location = '/midnight/battle/'
		})
		
		jQuery('#update').click(function(e){
			if (confirm('Вы уверенны что хотите загрузить нового героя (ваша заявка будет рассмотренна заново)?')) {
				return true
			} else {
				e.preventDefault()
			}
		})
		jQuery('#go').click(function(e){
			e.preventDefault()
			_my.showLogin()
		})
		jQuery(".heroes li[class!='voted']").hover(
      function () {
        $('span', this).css('display', 'block')
      }, 
      function () {
        	$('span', this).css('display', 'none')
      }
    );
		
		jQuery('.signup').click(function(e){
			e.preventDefault()
			oFileds = jQuery(".votes form input[name='votes[]']")
			oLogin = jQuery('.login')
			for ( i = 0; i < oFileds.length; i++){
				oLogin.append('<input type="hidden" value="' + jQuery(oFileds.eq(i)).attr('value') + '" name="votes[]"/>')
			}
			_my.showLogin()
		});
		jQuery('#close').click(function(e){
			e.preventDefault()
			_my.closeLogin()
		});
		
		this.tvPreview = jQuery('.tv-prevew');
		this.tvPreview2 = jQuery('.tv-prevew2');
		
		var _my = this;
		
		this.tvPreview.click(function(){
			jQuery( 'img', this ).fadeOut('fast');
			jQuery( 'body' ).css('background-image', 'none');
			jQuery( '.video' ).fadeIn('slow');
			jQuery( '.embed' ).fadeIn('slow');
			_my.tvPreview2.find('img').fadeIn('fast');
			jQuery( '.video2' ).css({display:'none'});
			_my.tvPreview2.find('.embed2').fadeOut('fast');
		});
		this.tvPreview2.click(function(){
			jQuery( 'img', this ).fadeOut('fast');
			jQuery( 'body' ).css('background-image', 'none');
			jQuery( '.video2' ).fadeIn('slow');
			jQuery( '.embed2' ).fadeIn('slow');
			_my.tvPreview.find('img').fadeIn('fast');
			jQuery( '.video' ).css({display:'none'});
			_my.tvPreview.css({paddingTop:0}).find('.embed').fadeOut('fast');
		});
		
		_my.participantsWidth  = _my.participantsWidth - this.ItemWidth*7

		jQuery('.next img').click(function(){
			_my.stop = false
			_my.moveLeft()
		});
		jQuery('.prev img').click(function(){
			_my.stop = false
			_my.moveRight()
		});
	},
	moveLeft:function(bottleWitdhCoeff){
		if( !this.stop ){
			curLeft = (jQuery('.participants').css('left') == 'auto') ? 0 : parseInt(jQuery('.participants').css('left'))
			if ( curLeft*(-1)  - 40< _my.participantsWidth ){
				newLeft = curLeft - _my.ItemWidth * (bottleWitdhCoeff ? bottleWitdhCoeff : 4);
				newLeft = (newLeft)*(-1) > _my.participantsWidth ? _my.participantsWidth*(-1) : newLeft
				jQuery('.participants').animate({left: newLeft + 'px'}, 1000)
						
				jQuery('.prev').fadeIn('fast')
				
				if ( newLeft*(-1) + 40> _my.participantsWidth ){
					jQuery('.next').fadeOut('slow')
				}
			} else {
				
			}
		}
	},
	moveRight:function(bottleWitdhCoeff){
		if( !this.stop ){
			curLeft = (jQuery('.participants').css('left') == 'auto') ? 0 : parseInt(jQuery('.participants').css('left'))
			if ( curLeft < 0 ){
				newLeft = curLeft + _my.ItemWidth * (bottleWitdhCoeff ? bottleWitdhCoeff : 4)
				newLeft = (newLeft + 40) > 0 ? 0 : newLeft
				jQuery('.participants').animate({left: newLeft + 'px'}, 1000)
				jQuery('.next').fadeIn('fast')
				if ( (newLeft + 40) > 0 ){
					jQuery('.prev').fadeOut('slow')
				}
			} else {
				
			}
		}
	},
	moveToBottle:function(id){
	  var pos = -1;
	  
	  jQuery.each(jQuery(".participants li"), function(i, e) {
	    if ( ('bbottle' + id) ==  jQuery(e).attr("id") ) {
	      pos = i;
	    }
	  });
	  
	  if (pos - 6 > 0) {
			_my.stop = false;
			_my.moveLeft(pos - 6 + 3);
	  }
	},
	showArrow: function(){
		jQuery('.next img').css('display', 'none')
	},
	showLogin: function(){
		jQuery('#not-registred').css( 'display', 'block')
	},
	closeLogin: function(){
		jQuery('#not-registred').css( 'display', 'none')
	}
}

function Editor(){
	
}

Editor.prototype = {
	init:function(){
		this.attachEvents()
	},
	attachEvents:function(){
		__my = this
		
		jQuery('.open').click(function(){
			jQuery( 'table', this.parentNode).css('display', 'block')
		})
	}
}


function Votes(){
	
}
Votes.prototype = {
	init:function(){
		this.oVote = jQuery('.vote')
		this.oSmallBottles = jQuery(".small li[class!='voted']")
		this.oForm = jQuery('.votes form')
		this.attachEvents()
	},
	attachEvents:function(){
		__my = this
		jQuery('.choose').click(function(){
			jQuery(this.parentNode).append('<div id="voted_' + this.id + '" class="voted"/>')
			iId = this.id.replace(/bottle/, '')
			sVoteField = '<input type="hidden" value="' + iId + '" name="votes[]"/>'
			sMiniBottleId = '.' + this.id
			sSrc = '/images/midnight_user/bottle_source/' + jQuery('img', this.parentNode).attr('id') + '/small-bottle.jpg'
			if( jQuery(sMiniBottleId).length ){
			} else {
				empty = jQuery('.votes .empty:first');
				empty.removeClass('empty')
				empty.addClass(this.id)
				jQuery('img', empty).attr('src',sSrc)
			}

			jQuery(sMiniBottleId).append(sVoteField)
			jQuery(this).remove()
		})
		this.oVote.click(function(){
			iId = this.id.replace(/bottle/, '')
			jQuery(this.parentNode).append('<div id="voted_' + this.id + '" class="voted"/>')
			sVoteField = '<input type="hidden" value="' + iId + '" name="votes[]"/>'
			sMiniBottleId = '.' + this.id
			sSrc = '/images/midnight_user/bottle_source/' + jQuery('img', this).attr('id') + '/small-bottle.jpg'
			if( jQuery(sMiniBottleId).length ){
			} else {
				empty = jQuery('.votes .empty:first');
				empty.removeClass('empty')
				empty.addClass(this.id)
				jQuery('img', empty).attr('src',sSrc)
			}

			jQuery(sMiniBottleId).append(sVoteField)
			
			jQuery('span', this.parentNode).remove()
		})
		
		this.oSmallBottles.click(function(){
			jQuery( 'input', this).remove()
			sCurClass = jQuery(this).attr('class')
			oVoted =jQuery( '.participants #voted_' + sCurClass )
			jQuery('#b' + sCurClass).append('<span id="'+ sCurClass+'" class="choose" style="display: none;"/>')
			jQuery(this).removeClass(sCurClass)
			jQuery(this).addClass('empty')
			jQuery('img', this).attr('src', '/images/midnight/empty-small-bottle.gif')
			
			oVoted.remove()
		})
	}
}