if ($.browser.msie) {
	if (parseInt($.browser.version, 10) >= 6) {var thisIsIE = true;}
}

$(document).ready(function(){

	//if (thisIsIE) {
		var topM = $(window).height()/10;
		if (topM < 20) topM = 20;
		$('.light .main-menu, .dark .main-menu, .light #logo, .dark #logo, #close-portfolio').css({position:'fixed',left:'auto',top:topM});
		$('.light #page, .dark #page').css({paddingTop:topM+80});
	//}

	$('.video-player').each(function(){
		var img = $(this).find('img').attr('src');
		var flv = $(this).attr('href');
	});

	$('.portfolio-filter').not('.all-filter').each(function(){
		var search = $(this).attr('href').replace('#', '.');
		var count = $('.portfolio-items ' + search).length;
		$(this).append('<sup> ' + count + '</sup>');
	});
	
	$('.all-filter').each(function(){
		var count = $('.portfolio-items li').length;
		$(this).append('<sup> ' + count + '</sup>');
	});

	var currentFilter = '.services-all';
	var currentPage = 0;
	var itemsPerPage = 12;
	
	function paginate(){
		var l = false, r = false;
		$(currentFilter).each(function(i){
			$(this).show();
			if(i < currentPage * itemsPerPage){
				$(this).hide();
				l = true;
			}
			if(i >= (currentPage + 1) * itemsPerPage){
				$(this).hide();
				r = true;
			}
		});
		if(l){
			$('#left-arrow').show();
		} else {
			$('#left-arrow').hide();
		}
		if(r){
			$('#right-arrow').show();
		} else {
			$('#right-arrow').hide();
		}
	}
	
	$('.portfolio-groups').show();
	$('#services').show();
	$('#left-arrow, #right-arrow').css({display: 'block'}).hide();

	$('.article-components').show();
	$('#sites').show();

	paginate();
	
	$('#list-type').click(function(e){
		$(this).toggleClass('list-type-text');
		$('.portfolio-items').toggleClass('list-text');
		if(itemsPerPage == 12){
			itemsPerPage = 24;
		} else {
			itemsPerPage = 12;
		}
		currentPage = 0;
		paginate();
		e.preventDefault();
	});

	$('.portfolio-filter-group').click(function(e){
		$('.portfolio-group').hide();
		$($(this).attr('href')).show();
		$('.portfolio-filter-group').removeClass('current-filter-group');
		$(this).addClass('current-filter-group');
		e.preventDefault();
	});
	
	$('.portfolio-filter').click(function(e){
		var f = $(this).attr('href').replace('#', '.');
		currentFilter = f;
		currentPage = 0;
		$('.portfolio-items li').hide();
		$(f).show();
		$('.portfolio-filter').removeClass('current-filter');
		$(this).addClass('current-filter');
		
		paginate();
		e.preventDefault();
	});

	$('.portfolio-filter').click(function(e){
		var f = $(this).attr('href').replace('#', '.');
		currentFilter = f;
		currentPage = 0;
		$('.portfolio-items li').hide();
		$(f).show();
		$('.portfolio-filter').removeClass('current-filter');
		$(this).addClass('current-filter');
		
		paginate();
		e.preventDefault();
	});
	
	$('.article-filter-component').click(function(e){
		$('.article-component').hide();
		$($(this).attr('href')).show();
		$('.article-filter-component').removeClass('current-filter-group');
		$(this).addClass('current-filter-group');
		e.preventDefault();
	});
	
	$('.portfolio-items img').hover(function(){
		$(this).animate({opacity: 0.1}, 400);
	}, function(){
		$(this).animate({opacity: 1}, 400);
	});
	
	$('#left-arrow').click(function(e){
		currentPage--;
		paginate();
		e.preventDefault();
	});
	$('#right-arrow').click(function(e){
		currentPage++;
		paginate();
		e.preventDefault();
	});
	$('.submenued').each(function(){
		submenued($(this));	
	});


	var VK = $('<a/>',{
		href : 'http://vkontakte.ru/share.php?url=' + document.location,
		'class' : 'vk',
		html : '<ins/>'
	}).appendTo('#footer .soc');

	//$('<script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>').prependTo('#footer .soc');
	var TW = $('<a/>',{
		href : 'http://twitter.com/share?original_referer=' + document.location + '&source=tweetbutton&text=' + $('head title').text() + '&url=' + document.location,
		'class' : 'tw',
		html : '<ins/>'
	}).appendTo('#footer .soc');

	var FB = $('<a/>',{
		href : "javascript:var d=document,f='http://www.facebook.com/share',l=d.location,e=encodeURIComponent,p='.php?src=bm&v=4&u='+e(l.href)+'&t='+e(d.title);1;try{if (!/^(.*\.)?facebook\.[^.]*$/.test(l.host))throw(0);share_internal_bookmarklet(p)}catch(z) {a=function() {if (!window.open(f+'r'+p,'sharer','toolbar=0,status=0,resizable=1,width=626,height=436'))l.href=f+p};if (/Firefox/.test(navigator.userAgent))setTimeout(a,0);else{a()}}void(0)",
		'class' : 'fb',
		html : '<ins/>'
	}).appendTo('#footer .soc');

	$('#footer .soc a ins').hover(function(){
		$(this).animate({opacity:1},500);
	}, function(){
		$(this).animate({opacity:0},500);
	});

	$('.response li').hover(function(){ $(this).css({zIndex:500}); }, function(){ $(this).css({zIndex:'auto'}); });
	$('.response a.respImg').click(function(e){
		e.preventDefault();
		$('#newRespose').remove();
		var elPos = $(this).offset();
		var resp = $(this).next('.descriptResponse');
		var nLeft = ( resp.hasClass('respLeft') )  ?  elPos.left-360 : elPos.left-20;
		var nTop = elPos.top-20;
		var newResponse = resp.clone().attr({'id': 'newRespose'}).css({top:nTop+'px', left:nLeft+'px', zIndex:500});
		$('#page-wrapper').append(newResponse);
		$('#newRespose').fadeIn();
	});
	$('.response').live('mouseover',function(){ $('#newRespose').delay(300).fadeOut('normal', function(){ $(this).remove(); }); });

	$('a.descrClose').live('click',function(e){
		e.preventDefault();
		$('#newRespose').fadeOut('normal', function(){ $('#newRespose').remove(); });
	});


	$('#prev-portfolio-item, #next-portfolio-item').css({position:'fixed',top:$(window).height()/2});

	$('#prev-portfolio-item, #next-portfolio-item').hover(function(){
		$('img',this).show(0);
	},function(){
		$('img',this).hide(0);
	});
	$('#prev-portfolio-item, #next-portfolio-item').mousemove(function(e){
		var xL = e.pageX - this.offsetLeft;
		var xR = 60 - (e.pageX - this.offsetLeft);
		var y = e.pageY - this.offsetTop -$(window).scrollTop() + 15;

		if ($(this).attr('id') == 'prev-portfolio-item') $('img',this).css({top:y,left:xL});
		if ($(this).attr('id') == 'next-portfolio-item') $('img',this).css({top:y,right:xR});

	});

});

function submenued(context) {
	var menuArr = Array();
	var i,m = 0;
	$('h3',context).each(function(m){
		menuArr[m] = $(this).text();
		$(this).attr({'index':m});
		i = m;
	});
	if (menuArr != '') {
		$("<ul/>",{
			'class':'subMenu',
			'style':'float:left;width:200px;margin:0 40px 0 0;'
		}).prependTo(context);
		for (j = 0; j <= i; j++) {
			$("<a/>",{
				href:'#' + j,
				text:menuArr[j]
			}).appendTo('.subMenu', context).wrap('<li/>');
		}
	}
}
