/*config*/
var people_visible = 7;
var client_visible = 13;
var port_visible = 8;

var jspOptions = {
	showArrows: false,
	scrollbarWidth: 9,
	scrollbarMargin: 8,
	
	reinitialiseOnImageLoad: true
};
var fb_like_iframe = '<iframe src="http://www.facebook.com/plugins/like.php?href=MY_URL&amp;send=false&amp;layout=button_count&amp;width=150&amp;show_faces=true&amp;action=like&amp;colorscheme=light&amp;font&amp;height=21" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:150px; height:21px;" allowTransparency="true"></iframe>';
/*/config*/

/*window_resize - begin*/
var d = document;
var winIE = (navigator.userAgent.indexOf("Opera")==-1 && (d.getElementById &&  d.documentElement.behaviorUrns))  ? true : false;

function bodySize(){
	if(winIE && d.documentElement.clientWidth) {
		sObj = d.getElementsByTagName("body")[0].style;
		sObj.width = (d.documentElement.clientWidth<800) ? "800px" : "100%";
	}
}

function init(){
	if(winIE) { bodySize(); }
}
 
onload = init;

if(winIE) { onresize = bodySize; }
/*window_resize - end*/

/*people carousel*/
function init_people_carousel()
{
	$("div.people_carousel").css("overflow", "hidden");
	
	$("div.people_carousel ul li").hover(
		function() {
			$(this)
				.removeAttr("class")
				.addClass("hover")
				.parent()
					.removeAttr("class")
					.addClass("out")
			;
			var $img = $(this).find("img");
			$img.attr('src', $img.attr('color_img'));

		},
		function() {
			
			$(this)
				.removeAttr("class")
				.parent()
					.removeAttr("class")
			;
			var $img = $(this).find("img");
			$img.attr('src', $img.attr('gray_img'));
		}
	);

}

function no_print_page()
{
	$("div.hidden_to_print").fadeIn("fast");
	$('#top div.top_header').slideToggle(500, function(){
		
		$('body').toggleClass('print_version',100);
		
		if (typeof extNoPrintPage != "undefined" && $.isFunction(extNoPrintPage))
		{
			extNoPrintPage();
		}
	});
	if (jQuery.browser.msie)
	{
		$("#all *").each(function() { this.style.removeAttribute('filter'); } );
	}
	
	return false;
}

$.tools.flashembed.conf.onFail = function(){
	var $container = $(this.getContainer());
	
	var cur_version = this.getVersion();
	
	var html = '';
	
	if ($(document.documentElement).attr("lang") === "ru")
	{
		html += '<p class="req-version">Требуется флэш-плеер версии <strong>' + this.getConf()['version'].join(".") + '</strong> или новее.</p>' + 
			'<p class="cur-version">' + 
				(cur_version[0] == 0
					? 'У Вас флеш-плеер <strong>не установлен</strong>!'
					: 'Ваша версия <strong>' + cur_version.join(".") + '</strong>.'
				) +
			'</p>' +
			'<p class="adobe-link">' + 
				'Загрузить последнюю версию плеера можно ' + 
				'<a href="http://www.adobe.com/go/getflashplayer" title="Загрузить флэш-плеер">здесь</a>.' +
			'</p>';
	}
	else
	{
		html += '<p class="req-version">Flash version <strong>' + this.getConf()['version'].join(".") + '</strong> or greater is required.</p>' + 
			'<p class="cur-version">' + 
				(cur_version[0] == 0
					? 'You have <strong>no flash plugin installed</strong>!'
					: 'Your version is <strong>' + cur_version.join(".") + '</strong>.'
				) +
			'</p>' +
			'<p class="adobe-link">' + 
				'Download latest version from ' + 
				'<a href="http://www.adobe.com/go/getflashplayer" title="Download Flash Player">here</a>.' +
			'</p>';
	}
	
	if ($container[0].tagName.toLowerCase() === "a")
	{
		$container[0].onclick = function() {
			location.href= 'http://www.adobe.com/go/getflashplayer';
		};
	}
	$container.html('<div class="flash-fail" title="">' + html + '</div>');
};

function init_page_link()
{
	$("a.video_link").each( function () {
		if ( ! this.href.match(/\.flv$/i)) return;
		var a = $(this);
		var pic_url = a.find('img:first').attr('src');
		var clip_url = this.href;
		a.parent().css({height: '182px'}).flashembed({
			src: media_path + "swf/videoplayer.swf",
			width: "320",
			height: "197",
			version: [9,0],
			bgcolor: "#FFF",
			wmode: "transparent",
			align: "t"
		},{
			'clip': clip_url,
			'pic': pic_url
		});
	});
}

function set_flash_header()
{
	$("#top div.top_header")
//	.css('background', 'none')
	.flashembed({
		src: media_path + "swf/headers/leader-1a.swf",
		width: "100%",
		height: 88,
		version: [8,0],
		bgcolor: "#FFF",
		wmode: "opaque",
		align: "t"
	});
}

$(document).ready(function() {
	$("#center .content_wrapper").css("overflow", "hidden"); //
	
	if ($.browser.msie && $.browser.version < '7')
	{
		$("img.alt_gif").each(function(){this.src = this.src.replace(/\.png$/i, '.gif');});
		$(document).pngFix({blankgif: blankImg});
	}
	
	/*menu animation */
	$("#top div.menu_top_wrapper li").hover(function() {
		$(this).stop().animate({backgroundPosition: "(0 0)"}, 600);
	},function() {
		$(this).stop().animate({backgroundPosition: "(0 -25px)"}, 400);
	});
	/*/menu animation */
	
	/*header*/
	// If direct call flashembed "addthis" social plugin not work in IE
	window.setTimeout(set_flash_header, $.browser.msie ? 1000 : 0);
	/* /header*/
	
	if(!$("div.people_list_wrapper").length && !$("div.port_coverflow").length && !$("#body_services").length) {
		$("#center div.content").jScrollPane(jspOptions);
	}
	else {
		$("#center div.content").css({
			"height": "auto",
			"overflow": "hidden"
		});
		
		$("#center div.info_left").jScrollPane(jspOptions);
	}
	
	$("#center div.index_right").jScrollPane(jspOptions);
	
	$("#center div.port_descr").jScrollPane(jspOptions);
	
	$("#center div.services_wrapper").jScrollPane(jspOptions);
	
	/*people carousel*/
	var people_carousel = $("div.people_carousel");
	people_carousel.find("img").each(function (){
		var $this = $(this);
		
		var color = $this.attr('src');
		var gray  = $this.attr('src2');

		if (typeof(gray) == "undefined")
		{
			gray = color;
		}
		
		$this.attr('gray_img', gray);
		$this.attr('color_img', color);
		
		$this.attr('src', gray);
	});
	init_people_carousel();
	if (people_carousel.size() && people_carousel.find('li').size() > people_visible)
	{
		people_carousel.jCarouselLite({
			auto: 1000,
			speed: 1500,
			visible: people_visible,
			beforeStart: init_people_carousel,
			
			stop_on_hover: true
		});
	}
	
	var client_carousel = $("div.clients_list_small");
	if (client_carousel.size() && client_carousel.find('li').size() > client_visible)
	{
		$("#clients_list_right").show();
		var clients_list_first = client_carousel.find('li:first').attr('id');
		var clients_list_last = client_carousel.find('li:eq('+(client_carousel.find('li').size()-client_visible)+')').attr('id');
		$("div.clients_list_small div").jCarouselLite({
			visible: client_visible,
			circular: false,
			mouseWheel: true,
			
			btnNext: "#clients_list_right",
			btnPrev: "#clients_list_left",
			
			afterEnd: function(jcl) {
				var first_item_id = jcl[0].id;
				if (first_item_id == clients_list_first)
				{
					$("#clients_list_left").hide();
					$("#clients_list_right").show();
				}
				else if ((first_item_id == clients_list_last))
				{
					$("#clients_list_right").hide();
					$("#clients_list_left").show();
				}
				else
				{
					$("#clients_list_left, #clients_list_right").show();
				}
			}
		});
		
		var client_list_timer;
		$("#clients_list_right, #clients_list_left").hover(function(){
			var a = $(this);
			client_list_timer = setInterval(function(){a.click();}, 1000);
			return false;
		}, function(){
			clearInterval(client_list_timer);
			return false;
		});
	}
	
	/*ajax news*/
	$("#center ul.news_list").before("<div id='fancy_tmp' class='hidden'><a href='#fancy_div' id='fancylink_news'></a></div>");

	$("a.fancybox_news").click(function() {
		
		var $text_box = $(this).parents("li:first").find("div.text_box");
		var $h5 = $text_box.find("h5");
		var news_title = $h5.text().replace($h5.find("span.date").text(), '');
		var news_description = $text_box.find("p:first").html();
		var news_url = window.location.protocol + "//" +
			window.location.host + $(this).attr("href");
		$text_box = $h5 = null;
		
		function set_addthis_params()
		{
			if (window.addthis_share)
			{
				addthis_share.url = news_url;
				addthis_share.title = addthis_share._my_title_tpl.replace(/__news_title__/g, news_title);
				addthis_share.description = news_description;
				
				var a = $('#atbutton').clone(false);
				$('#atbutton_fancy').remove();
				$('#fancybox-outer').append(a.attr('id', 'atbutton_fancy'));
				a = null;
				
				addthis.button('#atbutton_fancy');
			}
		}
		
		function add_fb_like()
		{
			var fb_like_code = fb_like_iframe.replace(/MY_URL/g, news_url);
			$('#fancy_div').append('<div id="fb_like">'+fb_like_code+'</div>');
		}
		
		$.fancybox.showActivity();
		$.ajax({
			url: $(this).attr("href"),
			dataType: "html",
			async: false,
			success: function (data) {

				$("#fancy_tmp").append('<div id="fancy_div"></div>');
				$("#fancy_div").html($(data).find("ul.news_single"));
				$("#fancy_div li h5 span.date").appendTo($("#fancy_div li h5"));
				
				var $work_link = $("#fancy_div li p.work_link a").clone();
				$("#fancy_div li p.work_link").remove();

				$("#fancylink_news").fancybox({
					hideOnContentClick: false,
					padding: 15,
					overlayOpacity: 0.8,
					centerOnScroll: false,
					width: 370,
					height: 400,
					autoDimensions: false,
					
					titlePosition: 'inside',
					titleFormat: function(title, currentArray, currentIndex, currentOpts) {
						return '<span id="fancybox-title-inside">' + (title || '&nbsp;' )+ '</span>';
					},
					
					onStart: function() {
						$("#fancy_div").css({visibility: "hidden"});
						$("#fancybox-wrap").css({visibility: "hidden"});
						$("#fancybox-close").attr("href", "#close").click(function(){return false;});
					},
					
					onCleanup: function() {
						$("#fancy_div").parent().fadeOut().remove();
					},
					
					onComplete: function() {
						
						init_page_link();
						
						$("#fancy_div").height($("#fancybox-inner").height()-10).jScrollPane(jspOptions);
						
						var fw = $("#fancybox-wrap");
						var fw_offset = fw.offset();
						fw.css("left", 100 + fw.offset().left);
						fw.css({visibility: "visible"});
						$("#fancy_div").hide().css({visibility: "visible"}).fadeIn("slow");
						
						$("#fancybox-title").addClass("with_work_link").prepend($work_link);
						
						set_addthis_params();
						add_fb_like();
					}
				});
				$.fancybox.hideActivity();
				$("#fancylink_news").click();
			}
		});
		return false;
	});
	/*/ajax news*/
	
	/*print_version*/
	$('#center a.print_page').click(function(){
		$("div.hidden_to_print").fadeOut("slow");
		$('#top div.top_header').slideToggle(500, function(){
			
			$('body').toggleClass('print_version');
			if (typeof extPrintPage != "undefined" && $.isFunction(extPrintPage))
			{
				extPrintPage();
			}
		});
		
		return false;
	});
	
	/*/print_version*/
	
	init_page_link();
});
