;(function($){
	
$(function(){
	clearFormFields({
		clearInputs: true,
		clearTextareas: true,
		passwordFieldText: true,
		addClassFocus: "focus",
		filterClass: "default"
	});
	initGallerys();
	initVideos();
	initPopups();
	initNewsTabs();
	initImage();
});

function initImage (){
	var images = $('.details-block .img-area img, .news-item img');
	var news = $('.news-item');
	var value = '?Action';
	var newValue;
	var _a = 0;
	images.each( function (){
		var image = $(this);
		var imageSrc = image.attr('src');
		var index = imageSrc.indexOf(value);
		newValue = imageSrc.substring(index+value.length);
		if (index == 0) {
			if (!news.length) 
				image.hide();
			else {
				image.attr('src', '/images/logo.png?Action'+newValue).css('border', 'none');
			}
		} else {
			_a += 1;
		}
	});
}
function initNewsTabs(){
	var blocks = $('div.news-item');
	var pagingHolder = $('div.news-paging');
	
	var monthNames = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
	var monthNamesShort = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
	var yearArray = [];
	var monthArray = [];
	
	if (pagingHolder.length && blocks.length) {
		blocks.each(function(){
			var block = $(this);
			var dateString = $('span.date strong',block).text().substr(3).split('-');
			var year = dateString[1];
			var month = dateString[0];
			
			parse(yearArray, year);
			parse(monthArray, month);
			
			function parse(resultArray, param){
				var addFlag = false;
				for (var i = 0; i < resultArray.length; i++) {
					if (resultArray[i].string == param) {
						 addData();
						 break;
					}
				}
				
				if (!resultArray.length || i == resultArray.length) createObject();
				
				function createObject(){
					var obj = new Object();
					obj.string = param;
					obj.data = $('');
					obj.data  = obj.data.add(block);
					resultArray.push(obj);
				}
				
				function addData(){
					resultArray[i].data = resultArray[i].data.add(block);
				}
			}
		});
		
		function insertTabs(resultArray){
			var ul = $('<ul class="months">');
			$(resultArray).each(function(i,obj){
				var string = obj.string;
				if (resultArray == monthArray) {
					var index = $.inArray(string,monthNamesShort)
					if (index != -1) string = monthNames[index];
					ul.addClass('months-list');
				}
				$('<li><a href="#">'+string+'</a></li>').data({
					'index':i,
					'string':obj.string
				}).appendTo(ul);
			});
			pagingHolder.append(ul);
		}
		
		function changeTab(){
			var lists = $('ul.months',pagingHolder);
			lists.each(function(){
				var list = $(this);
				var elems = $('>li',list);
				elems.click(function(){
					var elem = $(this);
					elems.removeClass('active');
					elem.addClass('active');
					blocks.show();
					
					lists.find('>li').filter('.active').each(function(i){
						var _this = $(this);
						
						if (_this.parent().hasClass('months-list')) {
							for (var i = 0; i < monthArray.length; i++) {
								if (monthArray[i].string != _this.data('string')) monthArray[i].data.hide();
							}
						} else if (yearArray.length > 1) {
							for (var i = 0; i < yearArray.length; i++) {
								if (yearArray[i].string != _this.data('string')) yearArray[i].data.hide();
							}	
						}
					});
					return false;
				});
				elems.eq(0).trigger('click');
			});
		}
		
		if (yearArray.length > 1) insertTabs(yearArray);
		insertTabs(monthArray);
		changeTab();
	}
}

function initGallerys(){
	$('div.carousel').scrollGallery();
}

function initPopups(){
	$('a.with-popup').modalPopup();
}

function initVideos(){
	var boxes = $('div.video-box');
	var frame = $('#content div.video');
	var description = $('#content div.description');
	var titleHolder = $('strong',description);
	var productionHolder = $('li.production',description);
	var directorHolder = $('li.director',description);
	var distributorHolder = $('li.distributor',description);
	var active = -1;
	
	boxes.each(function(i){
		var holder = $(this);
		var link = $('span.img-holder > a',holder);
		var hoverBlock = $('div.hover',holder);
		var title = $('p',holder).text();
		var production = $('span.production',holder).html();
		var director = $('span.director',holder).html();
		var distributor = $('span.distributor',holder).html();
		var videoId = link.attr('href');
		var index = videoId.indexOf('watch?v=');
		if (index == -1) return;
		videoId = videoId.substr(index+8);
		if (!videoId) return;
		index = videoId.indexOf('&');
		if (index != -1) videoId = videoId.slice(0,index);
		
		$.ajax({
			url:'https://gdata.youtube.com/feeds/api/videos/'+videoId+'?v=2&alt=json-in-script',
			dataType:'jsonp',
			crossDomain:true,
			success:function(json){
				var mediaGroup = json.entry.media$group;
				var imageUrl = mediaGroup.media$thumbnail[0].url;
				link.html('<img src="'+imageUrl+'" width="152" height="102" />').show();
				
				hoverBlock.bind('click',function(){
					play(true);
					return false;
				});
			},
			error:function(){
				alert('ajax error');
			}
		});
		
		function play(autoplay){
			if (i != active) {
				if (autoplay) frame.html('<iframe width="586" height="341" src="http://www.youtube.com/embed/'+videoId+'?autoplay=1&wmode=transparent" frameborder="0" allowfullscreen></iframe>');
				else frame.html('<iframe width="586" height="341" src="http://www.youtube.com/embed/'+videoId+'?&wmode=transparent" frameborder="0" allowfullscreen></iframe>');
				productionHolder.html(production);
				directorHolder.html(director);
				distributorHolder.html(distributor);
				titleHolder.text(title);
				active = i;
			}
		}
		
		if (i == 0) play();
	});
}

//lightbox plugin
$.fn.modalPopup = function(options){return new modalPopup($(this).eq(0),options)}
function modalPopup(link, options) {this.init(link,options)}
modalPopup.prototype = {
	init:function(link,options){
		var el = this;
		//options
		el.options = $.extend({
			fadeSpeed:400,
			closer:'a.close',
			scroll:false,
			wrapper:'body',
			IE:true,
			zIndex:100
		},options);	
		//popup & default css styles
		if ($.browser.msie && el.options.IE) el.popup = $(link.attr('href')).css({visibility:'hidden'})
		else el.popup = $(link.attr('href')).css({opacity:0,visibility:'hidden'});
		if (el.options.zIndex) el.popup.css({zIndex : el.options.zIndex});
		el.closer = $(el.popup.find(el.options.closer));
		el.popup.visible = false;
		modalPopup.prototype.activePopup = false;
		if (!modalPopup.prototype.firstRun) {
			modalPopup.prototype.firstRun = 'done';
			if ($.browser.msie && $.browser.version < 7) modalPopup.prototype.selects = $('select');
			//create fader
			if (!$('#fader').length) $('body').append('<div id="fader"></div>');
			modalPopup.prototype.fader = $('#fader');
			modalPopup.prototype.fader.css({position:'absolute',top:0,left:0,background:'#000',opacity:0,display:'none'});	
			if (el.options.zIndex) modalPopup.prototype.fader.css({zIndex : el.options.zIndex-1});
			modalPopup.prototype.wrapper = $(el.options.wrapper);
			//fader click event
			modalPopup.prototype.fader.click(function(){
				if (modalPopup.prototype.activePopup == false) el.hideFader()
				else modalPopup.prototype.activePopup.hidePopup(function(){el.hideFader()});
				return false;
			});
			//esc event
			$(document).keydown(function (e) {
				if (e.keyCode == 27) {
					if (modalPopup.prototype.activePopup == false) el.hideFader()
					else modalPopup.prototype.activePopup.hidePopup(function(){el.hideFader()});
					return false;
				}
			});
		}
		
		el.wrapper = modalPopup.prototype.wrapper;
		
		if ($.browser.msie && $.browser.version < 7) {
			el.popupSelects = $('select',el.popup);
			modalPopup.prototype.selects = modalPopup.prototype.selects.not(el.popupSelects);
		}
		
		//open event
		link.click(function(){
			el.show();
			return false;
		});
		//close event
		el.closer.click(function(){
			el.close();
			return false;
		});
		//resize event
		$(window).resize(function(){
			if (el.popup.visible) el.positioning(false);
		});
		if (el.options.scroll) {
			$(window).scroll(function(){
				el.positioning(true);
			});
		}
	},
	
	close:function(){
		var el = this;
		el.hidePopup(function(){el.hideFader()});
	},
	
	show:function(){
		var el = this;
		if (modalPopup.prototype.activePopup == el) {return false;}
		if (modalPopup.prototype.activePopup) {
			modalPopup.prototype.activePopup.hidePopup(function(){
				el.showPopup()
				el.positioning(true);
			});
		} else {
			el.showFader(function(){el.showPopup()});
			el.positioning(true);
		} 
	},
	
	showPopup:function(){
		var el = this;
		el.popup.visible = true;
		modalPopup.prototype.activePopup = el;
		if ($.browser.msie && el.options.IE) el.popup.css({visibility:'visible'})
		else el.popup.stop().css({'visibility':'visible'}).animate({opacity:1},el.options.fadeSpeed)
	},
	hidePopup:function(callback){
		var el = this;
		if ($.browser.msie && el.options.IE) {
			el.popup.css({left:'-9999px',top:'-9999px',visibility:'hidden'});
			el.popup.visible = false;
			modalPopup.prototype.activePopup = false;
			if ($.isFunction(callback)) callback();
		} else {
			el.popup.stop().animate({opacity:0},el.options.fadeSpeed,function(){
				el.popup.css({left:'-9999px',top:'-9999px',visibility:'hidden'});
				el.popup.visible = false;
				modalPopup.prototype.activePopup = false;
				if ($.isFunction(callback)) callback();
			});
		}
	},
	showFader:function(callback){
		var el = this;
		el.fader.stop().css({display:'block'}).animate({opacity:0.5},el.options.fadeSpeed,function(){
			if ($.isFunction(callback)) callback();
		});
		if ($.browser.msie && $.browser.version < 7) modalPopup.prototype.selects.css({'visibility': 'hidden'});
	},
	hideFader:function(){
		var el = this;
		el.fader.stop().animate({opacity:0},el.options.fadeSpeed,function(){
			el.fader.css({display:'none'});
			if ($.browser.msie && $.browser.version < 7) modalPopup.prototype.selects.css({'visibility': 'visible'});
		});
	},
	positioning:function(openFlag){
		var el = this;
		//x offset
		var windowW = $(window).width();
		var popupW = el.popup.outerWidth();
		var wrapperW = el.wrapper.outerWidth();
		
		if (windowW < wrapperW) {
			el.popup.css({left:wrapperW/2-popupW/2})
			el.fader.css({width:wrapperW});
		} else {
			 el.popup.css({left:windowW/2-popupW/2});
			 el.fader.css({width:windowW})
		}
		//y offset
		var docH;
		var windowH = $(window).height();
		var wrapperH = el.wrapper.outerHeight();
		if (windowH < wrapperH) docH = wrapperH
		else docH = windowH;
		
		var popupH = el.popup.outerHeight();
		if (openFlag) {
			var popupH = el.popup.outerHeight();
			if (popupH < windowH) el.popup.css({top:windowH/2-popupH/2+$(window).scrollTop()});
			else if ($(window).scrollTop()+popupH > docH){
				el.popup.css({top:docH-popupH});
			} else {
				el.popup.css({top:$(window).scrollTop()});
			}
		}
		el.fader.css({height:docH});
	}
}

//create jQuery plugin
$.fn.scrollGallery = function(options){return new scrollGallery(this,options)}

//constructor
function scrollGallery(obj,options){this.init(obj,options);}

//prototype
scrollGallery.prototype = {
	init:function(obj,options){
		this.options = $.extend({
			holder:'div.carousel-slides', 
			slider:'div.carousel-area',
			slides:'div.video-box',
			prevBtn:'a.prev',
			nextBtn:'a.next',
			pagingHolder:'div.switcher div.s1',
			pagingBtn:'li',
			createPaging:true,
			circleSlide:true,
			slideSpeed:500,
			startSlide:false,
			step:false
		},options);
		
		// elems
		this.mainHolder = $(obj);
		this.holder = $(this.options.holder, this.mainHolder);
		this.slider = $(this.options.slider, this.holder)
		this.slides = $(this.options.slides ,this.slider);
		this.prevBtn = $(this.options.prevBtn, this.mainHolder);
		this.nextBtn = $(this.options.nextBtn, this.mainHolder);
		
		//variables
		this.slideW = this.slides.eq(0).outerWidth(true);
		this.visibleStep = Math.round(this.holder.width()/this.slideW);
		this.options.step ? this.step = this.options.step : this.step = this.visibleStep;
		this.options.startSlide ? this.current = Math.floor(this.options.startSlide/this.step) : this.current = 0;
		this.max = this.slides.length - this.visibleStep;
		this.stepCount = Math.ceil((this.slides.length-this.visibleStep)/this.step)+1;
		
		//paging
		this.pagingHolder = $(this.options.pagingHolder, this.mainHolder);
		this.pagingLength = Math.ceil(this.slides.length / this.step);
		this.current = -1;
		
		if (this.pagingHolder.length) {
			this.pagingBtn = new Array;
			if (this.options.createPaging) {
				this.pagingHolder.each(function(i,obj){
					var $this = $(obj);
					$this.empty();
					var list = $('<ul>');
					for (var i = 0; i < this.pagingLength; i++) $('<li><a href="#">' + (i + 1) + '</a></li>').appendTo(list);
					$this.append(list);
				}.bind(this));
			}
			
			this.pagingHolder.each(function(i,obj){
				this.pagingBtn[i] = $(this.options.pagingBtn, $(obj)); 
				var stepCount = 0;
				this.pagingBtn[i].each(function(j,obj){
					var index = stepCount;
					$(obj).click(function(){
						if (index != this.current) {
							this.showSlide(index);
						}
						return false;
					}.bind(this));
					stepCount += this.step;
				}.bind(this));
			}.bind(this));
		}
		
		//next & prev
		this.nextBtn.click(function(){
			this.nextSlide();
			return false;
		}.bind(this));
		this.prevBtn.click(function(){
			this.prevSlide();
			return false;
		}.bind(this));
		
		this.showSlide(this.options.startSlide,true);
	},
	
	nextSlide:function(){
		if (this.options.circleSlide && this.current == this.stepCount - 1) {
			this.previous = this.current;
			this.current = 0;
			this.move();
		}
		else if (this.current < this.stepCount - 1) {
			this.previous = this.current++
			this.move();
		}
	},
	
	prevSlide:function(){
		if (this.options.circleSlide && this.current == 0) {
			this.previous = this.current;
			this.current = this.stepCount - 1;
			this.move();
		}
		else if (this.current > 0) {
			this.previous = this.current--;
			this.move();
		}
	},
	
	move:function(instant){
		var offset;
		if (this.step * this.current < this.max) offset = (this.step*this.current)*this.slideW;
		else offset = this.max * this.slideW
		if (instant) this.slider.css({marginLeft:-offset});
		else this.slider.animate({marginLeft:-offset},this.options.slideSpeed);
		this.refreshStatus();
	},
	
	showSlide:function(nextInd,instant){
		var nextCount = Math.floor(nextInd/this.step);
		if (nextCount != this.current) {
			this.previous = this.current;
			this.current = nextCount;
			if (instant) this.move(instant);
			else this.move();
		}
	},
	
	refreshStatus:function(){
		$(this.pagingBtn).each(function(i,array){
			array.eq(this.previous).removeClass('active');
			array.eq(this.current).addClass('active');
		}.bind(this));
	}
}

// bind scope function
Function.prototype.bind = function(scope) {
	var _function = this;
	return function() {
		return _function.apply(scope, arguments);
	}
}

})(jQuery);

function clearFormFields(o)
{
	if (o.clearInputs == null) o.clearInputs = true;
	if (o.clearTextareas == null) o.clearTextareas = true;
	if (o.passwordFieldText == null) o.passwordFieldText = false;
	if (o.addClassFocus == null) o.addClassFocus = false;
	if (!o.filter) o.filter = "default";
	if(o.clearInputs) {
		var inputs = document.getElementsByTagName("input");
		for (var i = 0; i < inputs.length; i++ ) {
			if((inputs[i].type == "text" || inputs[i].type == "password") && inputs[i].className.indexOf(o.filterClass)) {
				inputs[i].valueHtml = inputs[i].value;
				inputs[i].onfocus = function ()	{
					if(this.valueHtml == this.value) this.value = "";
					if(this.fake) {
						inputsSwap(this, this.previousSibling);
						this.previousSibling.focus();
					}
					if(o.addClassFocus && !this.fake) {
						this.className += " " + o.addClassFocus;
						this.parentNode.className += " parent-" + o.addClassFocus;
					}
				}
				inputs[i].onblur = function () {
					if(this.value == "") {
						this.value = this.valueHtml;
						if(o.passwordFieldText && this.type == "password") inputsSwap(this, this.nextSibling);
					}
					if(o.addClassFocus) {
						this.className = this.className.replace(o.addClassFocus, "");
						this.parentNode.className = this.parentNode.className.replace("parent-"+o.addClassFocus, "");
					}
				}
				if(o.passwordFieldText && inputs[i].type == "password") {
					var fakeInput = document.createElement("input");
					fakeInput.type = "text";
					fakeInput.value = inputs[i].value;
					fakeInput.className = inputs[i].className;
					fakeInput.fake = true;
					inputs[i].parentNode.insertBefore(fakeInput, inputs[i].nextSibling);
					inputsSwap(inputs[i], null);
				}
			}
		}
	}
	if(o.clearTextareas) {
		var textareas = document.getElementsByTagName("textarea");
		for(var i=0; i<textareas.length; i++) {
			if(textareas[i].className.indexOf(o.filterClass)) {
				textareas[i].valueHtml = textareas[i].value;
				textareas[i].onfocus = function() {
					if(this.value == this.valueHtml) this.value = "";
					if(o.addClassFocus) {
						this.className += " " + o.addClassFocus;
						this.parentNode.className += " parent-" + o.addClassFocus;
					}
				}
				textareas[i].onblur = function() {
					if(this.value == "") this.value = this.valueHtml;
					if(o.addClassFocus) {
						this.className = this.className.replace(o.addClassFocus, "");
						this.parentNode.className = this.parentNode.className.replace("parent-"+o.addClassFocus, "");
					}
				}
			}
		}
	}
	function inputsSwap(el, el2) {
		if(el) el.style.display = "none";
		if(el2) el2.style.display = "inline";
	}
}
	
// iepp v2.1pre @jon_neal & @aFarkas github.com/aFarkas/iepp
// html5shiv @rem remysharp.com/html5-enabling-script
// Dual licensed under the MIT or GPL Version 2 licenses
/*@cc_on(function(a,b){function r(a){var b=-1;while(++b<f)a.createElement(e[b])}if(!window.attachEvent||!b.createStyleSheet||!function(){var a=document.createElement("div");return a.innerHTML="<elem></elem>",a.childNodes.length!==1}())return;a.iepp=a.iepp||{};var c=a.iepp,d=c.html5elements||"abbr|article|aside|audio|canvas|datalist|details|figcaption|figure|footer|header|hgroup|mark|meter|nav|output|progress|section|subline|summary|time|video",e=d.split("|"),f=e.length,g=new RegExp("(^|\\s)("+d+")","gi"),h=new RegExp("<(/*)("+d+")","gi"),i=/^\s*[\{\}]\s*$/,j=new RegExp("(^|[^\\n]*?\\s)("+d+")([^\\n]*)({[\\n\\w\\W]*?})","gi"),k=b.createDocumentFragment(),l=b.documentElement,m=b.getElementsByTagName("script")[0].parentNode,n=b.createElement("body"),o=b.createElement("style"),p=/print|all/,q;c.getCSS=function(a,b){try{if(a+""===undefined)return""}catch(d){return""}var e=-1,f=a.length,g,h=[];while(++e<f){g=a[e];if(g.disabled)continue;b=g.media||b,p.test(b)&&h.push(c.getCSS(g.imports,b),g.cssText),b="all"}return h.join("")},c.parseCSS=function(a){var b=[],c;while((c=j.exec(a))!=null)b.push(((i.exec(c[1])?"\n":c[1])+c[2]+c[3]).replace(g,"$1.iepp-$2")+c[4]);return b.join("\n")},c.writeHTML=function(){var a=-1;q=q||b.body;while(++a<f){var c=b.getElementsByTagName(e[a]),d=c.length,g=-1;while(++g<d)c[g].className.indexOf("iepp-")<0&&(c[g].className+=" iepp-"+e[a])}k.appendChild(q),l.appendChild(n),n.className=q.className,n.id=q.id,n.innerHTML=q.innerHTML.replace(h,"<$1font")},c._beforePrint=function(){if(c.disablePP)return;o.styleSheet.cssText=c.parseCSS(c.getCSS(b.styleSheets,"all")),c.writeHTML()},c.restoreHTML=function(){if(c.disablePP)return;n.swapNode(q)},c._afterPrint=function(){c.restoreHTML(),o.styleSheet.cssText=""},r(b),r(k);if(c.disablePP)return;m.insertBefore(o,m.firstChild),o.media="print",o.className="iepp-printshim",a.attachEvent("onbeforeprint",c._beforePrint),a.attachEvent("onafterprint",c._afterPrint)})(this,document)@*/
