Promo = {
		activeTab: 1,
		timerID: 0,
		switcher: true,
		autoAnimation: true,
		init: function() {
			var self = this;
			$('a[id^="tab-"]:not(.active)').live('click', Promo.select);
			$('a[id^="tab-"]:not(.active)').live('click', Promo.select);
			$('.tour img').bind('click', function() {
				self.autoAnimation = false;
			});
			$('a[id^="tab-"]').live('click', function(event) {event.preventDefault();});
			this.bindSwitcher();
			$('.tab-text, #promo > .tabs > a').hover(function() {
				self.switcher = false;
				self.bindSwitcher();
			}, function() {
				self.switcher = true;
				self.bindSwitcher();
			});
		},
		select: function() {
			if (!Promo.switcher) Promo.autoAnimation = false;
			var active = Promo.activeTab = $(this).index() + 1;
			$('[id^="tab-"]').removeClass('active');
			$('[id^="tab-' + active + '"]').addClass('active');
			$('[id^="tab-text-"]').css('display', 'none');
			$('[id="tab-text-' + active + '"]').css('display', 'block');
			$('.screenshot.active').stop().removeClass('active').animate({width: 502, height: 311}, {duration: 1500, easing: 'easeOutExpo'});
			$('[id="screenshot-' + active + '"]').stop().addClass('active').animate({width: 700, height: 371}, {duration: 1500, easing: 'easeOutExpo'});
			$('#screenshots > table').stop().animate({'left': 89 - 562 * (active - 1)}, {duration: 1500, easing: 'easeOutExpo'});
		},
		bindSwitcher: function() {
			if (this.timerID) clearTimeout(this.timerID);
			if (!this.switcher || !this.autoAnimation) return;
			var self = this;
			this.timerID = setTimeout(function() {
				var active = self.activeTab + 1;
				if (active == 5) active = 1;
				$('[id="tab-' + active + '"]').trigger('click');
				self.bindSwitcher();
			}, 6500);
		}
}

$(document).ready(function() {
	Promo.init();
	$('#promo').ifixpng();
	$('.promo-inside-screenshot a').lightBox({fixedNavigation:true});
	
	$('#promo-banners img').hover(
		function(e) {
			$(this).attr('src', '/img/logos/' + ($(this).parent().index()+1) + '-color.png');
		}, 
		function(e) {
			$(this).attr('src', '/img/logos/' + ($(this).parent().index()+1) + '-bw.png');
		}
	);
	
	if (navigator.userAgent.indexOf('iPhone') != -1 || navigator.userAgent.indexOf('iPad') != -1) {
		$('img.promo-banner').each(function() {
			var src = $(this).attr('src').replace('bw', 'color');
			$(this).attr('src', src);
		})
	}

	$('#load-news-archive').click(function() {
		if (getCookie('morenews') == 'yes') {
			setCookie('morenews', '', new Date(new Date().getTime() + 60*60*24*7*1000).toString(), '/');
			$('#news-archive').hide();
		} else {
			setCookie('morenews', 'yes', new Date(new Date().getTime() + 60*60*24*7*1000).toString(), '/')
			$('#news-archive').show();
		};
	});
	
	if (getCookie('morenews') == 'yes') {
		$('#news-archive').show();
	} else {
		$('#news-archive').hide();
	}
	
	$('.document a').each(function() {
		var text = $(this).html();
		if (text) {
			$('<span>' + $(this).html() + '</span>').insertAfter($(this));
			$(this).remove();
		}
	});
	
	$.each({
		'video1': 'http://lexpro.ru/files/video/Interwie_01', 
		'video2': 'http://lexpro.ru/files/video/Interwie_02',
		'video3': 'http://lexpro.ru/files/video/Interwie_03',
		'tour1': 'http://lexpro.ru/files/video/AnalitObrabotka',
		'tour2': 'http://lexpro.ru/files/video/IntellektPoisk',
		'tour3': 'http://lexpro.ru/files/video/Rubrikator',
		'tour4': 'http://lexpro.ru/files/video/Slovar'
	}, function(id, url) {
		if (!$('#' + id).size()) return;
		var ios = (navigator.userAgent.indexOf('iPod') != -1 || navigator.userAgent.indexOf('iPhone') != -1 || navigator.userAgent.indexOf('iPad') != -1) ? true : false;
		var options = $.extend(true, {clip: {url: url + (ios ? '.mp4': '.flv')}}, optionsVideo);
		flowplayer(id, "/flowplayer/flowplayer-3.2.7.swf", options).ipad();
	});
	
	$('#blog-code-button').click(
		function() {
			var visible = false;
			return function(event) {
				event.preventDefault();
				if (visible) {
					$('#blog-code').hide();
				} else {
					$('#blog-code').show();
				}
				visible = !visible;
			}
		} ()
	);
	
	if ($('#buy').size()) {
		$('#buy-local .radio').click(function(event) {
			var self = $(this);
			var index = self.parent().index();
			$('#buy-local .radio').removeClass('checked');
			self.addClass('checked');
			$('#buy-local-button').attr('href', '/buy/order?version=local-day&period=' + index);
		});
		$('#buy-internet .radio').click(function(event) {
			var self = $(this);
			var index = self.parent().index();
			$('#buy-internet .radio').removeClass('checked');
			self.addClass('checked');
			$('#buy-internet-button').attr('href', '/buy/order?version=internet&period=' + index);
		});
	}
	
	/*$('a[url]').each(function() {
		var self = $(this);
		self.attr('href', self.attr('url')).removeAttr('url');
	});*/
	
	if (location.href.match('/articles/last') && location.hash) {
		$('#main-content').html('');
		$.ajax({
			url: '/articles/one/' + location.hash.substr(1),
			success: function(resp) {
				if (resp) $('#main-content').html(resp);
			}
		});
	};
	
	$('#summary-total').data('total', parseInt($('#summary-total').text()));
	$('#field-count').blur(function() {
		var that = $(this);
		var count = parseInt(that.val());
		if (!count) count = 1;
		that.val(count);
		var total = count * $('#summary-total').data('total');
		$('#summary-total').text(total);
		$('input[name="total"]').val(total);
	});
	(function() {
		var that = $('#field-count');
		var count = parseInt(that.val());
		if (!count) count = 1;
		that.val(count);
		var total = count * $('#summary-total').data('total');
		$('#summary-total').text(total);
		$('input[name="total"]').val(total);
	})();
	
	var reposition = function() {
		if ($(document).height() > $(window).height()) {
			$('#container, #bottom > .content').css({'position': 'relative', 'left': (getScrollBarWidth() / 2) + 'px'});
		} else {
			$('#container, #bottom > .content').css({'position': 'relative', 'left': '0px'});
		}
	};
	reposition();
	$(window).resize(reposition);
	
	if (ODKL) ODKL.init();
	
	if (getCookie('language') == 'en') $('#promo-banners > a:eq(4)').css('display', 'none');
	
	/* link target fix */
	$('.article-view .body a').each(function() {
		if ($(this).attr('href').substring(0,7) == 'http://') $(this).attr('target', '_blank');
	});
	
	/*function touchHandler(event)
	{
	    var touches = event.changedTouches,
	        first = touches[0],
	        type = "";
	
	         switch(event.type)
	    {
	        case "touchstart": type = "mousedown"; break;
	        case "touchmove":  type="mousemove"; break;        
	        case "touchend":   type="mouseup"; break;
	        default: return;
	    }
	
	    //	initMouseEvent(type, canBubble, cancelable, view, clickCount,
	    //	screenX, screenY, clientX, clientY, ctrlKey,
	    //	altKey, shiftKey, metaKey, button, relatedTarget);

	    var simulatedEvent = document.createEvent("MouseEvent");
	    simulatedEvent.initMouseEvent(type, true, true, window, 1,
	                              first.screenX, first.screenY,
	                              first.clientX, first.clientY, false,
	                              false, false, false, 0, null);
		first.target.dispatchEvent(simulatedEvent);
		if (!first.startTime) {
			first.startTime = new Date().getTime();
		};
		if (event.type == 'touchmove') first.moved = true;
		if (event.type == 'touchend' && (new Date().getTime() - first.startTime) < 500 && !first.moved) {
			var simulatedEvent = document.createEvent("MouseEvent");
			simulatedEvent.initMouseEvent('click', true, true, window, 1,
	                              first.screenX, first.screenY,
	                              first.clientX, first.clientY, false,
	                              false, false, false, 0, null);
			first.target.dispatchEvent(simulatedEvent);
			event.preventDefault();
		}
	}
	
	document.addEventListener("touchstart", touchHandler, true);
	document.addEventListener("touchmove", touchHandler, true);
	document.addEventListener("touchend", touchHandler, true);
	document.addEventListener("touchcancel", touchHandler, true);   */
});

var optionsVideo = {
    clip:  {
        autoBuffering: true,
        scaling: "fit"
    },
    plugins: {
    	controls: {
    		all: false,
			play: true,
			scrubber: true,
			fullscreen: true
    	}
    }
};

function setCookie (name, value, expires, path, domain, secure) {
      document.cookie = name + "=" + escape(value) +
        "; expires=" + new Date(new Date().getTime() + 60*60*24*7*1000).toUTCString() +
        ((path) ? "; path=/" : "; path=/") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

function getCookie(name) {
	var cookie = " " + document.cookie;
	var search = " " + name + "=";
	var setStr = null;
	var offset = 0;
	var end = 0;
	if (cookie.length > 0) {
		offset = cookie.indexOf(search);
		if (offset != -1) {
			offset += search.length;
			end = cookie.indexOf(";", offset)
			if (end == -1) {
				end = cookie.length;
			}
			setStr = unescape(cookie.substring(offset, end));
		}
	}
	return(setStr);
}

function getScrollBarWidth () {
	var inner = document.createElement('p');
	inner.style.width = "100%";
	inner.style.height = "200px";

	var outer = document.createElement('div');
	outer.style.position = "absolute";
	outer.style.top = "0px";
	outer.style.left = "0px";
	outer.style.visibility = "hidden";
	outer.style.width = "200px";
	outer.style.height = "150px";
	outer.style.overflow = "hidden";
	outer.appendChild (inner);

	document.body.appendChild (outer);
	var w1 = inner.offsetWidth;
	outer.style.overflow = 'scroll';
	var w2 = inner.offsetWidth;
	if (w1 == w2) w2 = outer.clientWidth;

	document.body.removeChild (outer);

	return (w1 - w2);
};
