(function($){
	$(document).ready(function(){
		if($('.ford-test .answers').length){
			Test = Test();
		}
		if($('.ford-test span.code-for-blog').length){
			Code = Code();
		}
		
		if($('ul.week-list').length){
			WeekList = new WeekList();
		}
		
		if($('.ford-experts .expert').length){
			Expert = new Experts();
		}
		
		if($('.final-winners').length){
			$('#show-win').click(function(){
				$('.final-winners .flash').css({display:'block'});
				$('.final-winners .flash .close').click(function(){
					$('.final-winners .flash').css({display:'none'});
				});
			});
		}
		
		$('.big-column .users .user').hover(function(){
			$(this).addClass('visible');
		}, function(){
			$(this).removeClass('visible');
		});

		$('.twitter-comment span').click(function(){
			$('.tw-popup').slideToggle('fast');
		});

		
		notice = new Notice();
		
		vote = new Vote();
		
		$('#paste-to-blog').click(function(){
			$('#collage_code').css({display:'block'});
			$('#collage_code span.close').unbind().click(function(){
				$('#collage_code').css({display:'none'});
			});
		});

    $('#print-coupon-button').click(function(){
      javascript:window.print();
      $('body').append('<img src="http://ad.adriver.ru/cgi-bin/rle.cgi?sid=130436&sz=print&bt=21&pz=0&rnd=1883342010" border=0 width=1 height=1>')
      return false;
    });

		if($('body.fiesta-about-car').length || $('body.fiesta-gallery').length){
			new FiestaPower();
		}
		
		
		
	});
	
	function WeekList(){
		this.init();
	}
	
	WeekList.prototype = {
		init:function(){
			var _my = this;
			this.weeks = $('ul.week-list li');
			this.weekBlocks = $('.week-winners div.week');
			this.weeks.click(function(){
				_my.toggleWeeks($(this));
			});
		},
		toggleWeeks:function(elem){
			if(!elem.is('.disable') && !elem.is('.selected')){
				this.weeks.removeClass('selected');
				this.weekBlocks.removeClass('active').parents('.week-winners').find('div.'+elem.attr('class')).addClass('active');
				
				elem.addClass('selected');
			}
		}
	}
	
	
	function FiestaPower(){
		this.init();
	}
	
	FiestaPower.prototype = {
		init:function(){
			var _my = this;
			$(window).resize(function(){
				_my.setSize();
			});
			this.leftArr = $('#about-left-arr');
			this.rightArr = $('#about-right-arr');
			this.staticBlock = $('.about-static');
			this.movedBlock = $('.about-moved');
			this.leftArr.mouseover(function(){
				_my.moveInterval = setInterval(function(){
					_my.moveContent(-1);
				}, 30);
			}).mouseout(function(){
				_my.arrowsLighting();
				clearInterval(_my.moveInterval);
			});
			this.rightArr.mouseover(function(){
				_my.moveInterval = setInterval(function(){
					_my.moveContent(1);
					}, 30);
			}).mouseout(function(){
				_my.arrowsLighting();
				clearInterval(_my.moveInterval);
			});
			this.setSize();
			this.arrowsLighting();
			if($('.about-moved .images .img').length){
				this.initImages();
			}
		},
		initImages:function(){
			var _my = this;
			this.popup = $('.about-static #popup');
			$('.about-moved .images .img').click(function(){
				_my.showPopup($(this));
			});
			this.popup.find('.close').click(function(){
				_my.popup.removeAttr('class').css({display:'none'}).find('.img-wrap').css({display:'none'});
			});
		},
		showPopup:function(img){
			var _my = this;
			this.popup.removeAttr('class').find('.img-wrap').css({display:'none'});
			this.popup.css({display:'block'}).find('#'+img.attr('class').replace('img ', '')).css({display:'block'});
			this.popup.addClass(img.attr('rel'));
		},
		setSize:function(){
			this.delta = - this.movedBlock.width() + $(window).width();
		},
		moveContent:function(dir){
			var _my = this;
			if(dir == 1){
				this.stopLighting();
				var left = parseInt(_my.movedBlock.css('left'));
				if(left > this.delta){
					_my.movedBlock.css({left:left-30});
				}
			} else {
				this.stopLighting();
				var left = parseInt(_my.movedBlock.css('left'));
				if(left < 0){
					_my.movedBlock.css({left:left+30});
				}
			}
		},
		arrowsLighting:function(){
			var _my = this;
			this.lightInterval = setInterval(function(){
				_my.leftArr.addClass('light');
				_my.rightArr.addClass('light');
				setTimeout(function(){
					_my.leftArr.removeClass('light');
					_my.rightArr.removeClass('light');
				}, 1000);
			}, 2000);
		},
		stopLighting:function(){
			clearInterval(this.lightInterval);
			this.leftArr.removeClass('light');
			this.rightArr.removeClass('light');
		}
	}
	
	
	
	
	
	
	function Experts(){
		this.init();
	}
	
	Experts.prototype = {
		init:function(){
			var dark = $('#fiesta-dark');
			var telek = $('#telek');
			var close = telek.find('span.close');
			$('.ford-experts .expert').click(function(){
				$('#flash').remove();
				dark.css({display:'block'});
				telek.css({display:'block'});
				var id = $(this).attr('id');
				telek.find('.flash').append('<div id="flash"></div>');
				var params = { wmode:'transparent' }
				var flashvars = { 'song':asset_base_path + '/framework/specials/fiesta/tracks/'+ id +'.mp3' }
				new swfobject.embedSWF(asset_base_path + "/framework/specials/fiesta/flash/telik.swf", "flash", "620", "500", "9.0.115", false, flashvars, params);
				telek.find('.'+id).fadeIn('fast');
			});
			close.click(function(){
				telek.find('.images img').css({display:'none'});
				telek.find('.text div').css({display:'none'});
				dark.css({display:'none'});
				telek.css({display:'none'});
			});
		}
	}
	
	function Test(){
		var rightArrow = $('.test-right-arr');
		var answers = $('.answers .answer');
		answers.click(function(){
			answers.removeClass('selected');
			$(this).addClass('selected');
			$('#answer').val(''+$('.answers .answer').index($(this)));
		});
    answers.bind("dblclick", function(e){
      answers.removeClass('selected');
			$(this).addClass('selected');
			$('#answer').val(''+$('.answers .answer').index($(this)));
      $('#ctest_form').submit();
    })
		rightArrow.click(function(){
			$('#ctest_form').submit();
		})
	}
	
	function Code(){
		$('span.code-for-blog').click(function(){
			$('#code').toggle();
		});
		$('#code span.close').click(function(){
			$('#code').toggle();
		});
	}
	
	function Notice(){}
	
	Notice.prototype = {
		show:function(){
			$('#notice').show('fast', function(){
				setTimeout(function(){
					$('#notice').hide('fast');
				}, 2500);
			});
		}
	}
	
	function Vote(){
		this.init();
	}
	
	Vote.prototype = {
		init:function(){
			$('#voting_form_for_anonymous_user').click(function(event){
				$('#vote_form').css({display:'block', top:event.pageY-300});
				$('#vote_form span.close').click(function(){
					$('#vote_form').css({display:'none'});
					$(this).unbind();
				});
			});
		}
	}
	
})(jQuery);