// usage: log('inside coolFunc', this, arguments);
// paulirish.com/2009/log-a-lightweight-wrapper-for-consolelog/
window.log = function(){
  log.history = log.history || [];   // store logs to an array for reference
  log.history.push(arguments);
  if(this.console) console.log( Array.prototype.slice.call(arguments) );
};
// end for logger

var height;
var prev_height;
var height_exp1;
var height_exp2;
var prev_height_exp1;
var prev_height_exp2;

function hidden_height (selector) {
	var tmp = $(selector).height();
    $(selector).css('height','auto');
    var height = $(selector).height();
    $(selector).css('height',tmp);
	return height;
}

function carousel_initCallback(carousel, state) {
//	$('#portfolioSlider li.jcarousel-item a').click(function(fun) {
//		fun.preventDefault();
		// $(this).addClass('active');
//		var idx = $(this).parent().attr('jcarouselindex');
		// log ($(this).attr('jcarouselindex'));
		// log (idx); document.location = $self.attr('href');
//		$(".wrapPortfolioSlider div.title").fadeOut("slow");
//		carousel.scroll(idx-1);
		//document.location = $(this).attr('href');
		//setTimeout(function(){window.location = $(this).attr('href');},5000);
		//return null;
//	});
}

function carousel_itemFirstInCallback(carousel, state) {
	$('#portfolioSlider li.jcarousel-item a').removeClass('active');

	$('#slide_title').html($(this + "[jcarouselindex=" + (carousel.first+1) + "] a").addClass('active').find('img').attr('alt'));
	
	// var current = $(this + "[jcarouselindex=" + (carousel.first+1) + "] a").attr('href');
	// if (document.location != current) {
	// 	document.location = current;
	// }
}

$(document).ready(function(){

  $("a#like").toggle(function(){
    $("#addthis_toolbox").show();
    $("a#like").text('Свернуть');
  }, function(){
    $("#addthis_toolbox").hide();
    $("a#like").text('Понравилось');
  });

  
    $('img.load_img').lazyload({threshold : 200});

	// expandable thing on main
	height = hidden_height("#expand");
	height_exp1 = hidden_height("#exp1");
	height_exp2 = hidden_height("#exp2");
	// log(height_exp2);
	// log(height_exp1);
	// log(height);
	$(window).resize(function(){height = hidden_height("#expand"); height_exp1 = hidden_height("#exp1"); height_exp2 = hidden_height("#exp2");});
	
	$("a.expand").toggle(function(){
		prev_height = $("#expand").height();
		$("#expand").animate({height: height},"normal");
		$("a.expand").addClass('ll');
	}, function(){
		$("#expand").animate({height: prev_height},"normal");
		$("a.expand").removeClass('ll');
	});

	$("a.exp1").toggle(function(){
		prev_height_exp1 = $("#exp1").height();
		$("#exp1").animate({height: height_exp1},"normal", function(){
			$("a.exp1").text('Свернуть');
		});
	}, function(){
		$("#exp1").animate({height: prev_height_exp1},"normal", function(){
			$("a.exp1").text('Подробнее');
		});
	});

	$("a.exp2").toggle(function(){
		prev_height_exp2 = $("#exp2").height();
		$("#exp2").animate({height: height_exp2},"normal", function(){
			$("a.exp2").text('Свернуть');
		});
	}, function(){
		$("#exp2").animate({height: prev_height_exp2},"normal", function(){
			$("a.exp2").text('Подробнее');
		});
	});

	
	$("#expand a").hover(function(){
		$(this).find(".overlay").slideToggle("fast");
	}, function(){
		$(this).find(".overlay").slideToggle("fast");
	});
	// end of "expandable thing on main"
	$("li.dd").hover(function(){
		$(this).find("ul.dd").slideToggle("fast");
	}, function(){
		$(this).find("ul.dd").hide();
	});

	$("ul.faces li").hover(function(){
		$(this).addClass("current");
		$(this).find("div.layout").slideToggle("fast");
	}, function(){
		$(this).removeClass("current");
		$(this).find("div.layout").hide();
	});

	// $("#send_letter").toggle(function(){
	// 	$(this).parent().toggleClass('active');
 //    $(this).toggle(function(){
 //      $("#send_letter").text('Еще не время');  
 //    }, function(){
 //      $("#send_letter").text('Написать письмо');
 //    });
    
	// });

  $("#send_letter").toggle(function(){
    $("#send_letter").parent().toggleClass('active');
    //log($(this).parent());
    $("#send_letter").text('Еще не время');  
  }, function(){
    $("#send_letter").parent().toggleClass('active');
    //log($(this).parent());
    $("#send_letter").text('Написать письмо');
  });

	$("#send_letter_close").click(function(){
		$("#send_letter").parent().toggleClass('active');
    $("#send_letter").trigger('click');
	});

    /*-------------Человек Intraweb------------*/
    $('div.intarmen div').hover(function(){     
        var active_part = $(this).prevAll('div').size();
        $('ul.team_info li').hide();
        $('ul.team_info li').eq(active_part).show();
    });
    /*-------------Конец Человек Intraweb------------*/

    // tabs
    $("ul.tabs li").click(function(){
    	var idx = $(this).index();
    	$("ul.tabs li").removeClass('active');
    	$(this).addClass("active");
    	$("div.tab").hide();
    	$("div.tab").eq(idx).show();
    });
    
    var images = [
          		{url: '/our_kitchen.html',src: 'images/menu-img-1.jpg',cl: 'first',txt: 'варимся<br />потихоньку'},
          		{url: '/competitors.html',src: 'images/menu-img-2.jpg',cl: 'second',txt: 'конкурентам<br />перца'},
          		{url: '/good_ideas.html',src: 'images/menu-img-3.jpg',cl: 'third',txt: 'здравые<br />идеи'},
          		{url: '/sites',src: 'images/menu-img-4.jpg',cl: 'forth',txt: 'наше<br />портфолио'},
          		{url: '/web_sapiens.html',src: 'images/menu-img-5.jpg',cl: 'fifth',txt: 'Человек<br />Intraweb'},
          		{url: '/customers.html',src: 'images/menu-img-6.jpg',cl: 'sixth',txt: 'к нам стоит<br />обращаться'}
          	],
    menu = $('#bottom-menu');
    
    //перемешиваем
	images.sort(function() {return 0.5 - Math.random()});
	
	$.each(images,function(){

		menu.append('<li><a href="'+this.url+'"><img src="'+this.src+'" class="'+this.cl+'" />'+this.txt+'</a></li>');

	});
})

      // // (function () {
      // //   if (/Microsoft/.test(navigator.appName)) { return }

      //   window.onload = function () {
      //     //var headers = document.querySelectorAll('#docs h2, #guide h1');
      //     var menu = document.getElementById('menu');
      //     var init = menu.offsetTop;
      //     var docked;

      //     // for (var i = 0; i < headers.length; i++) {
      //     //   headers[i].id = '-' + headers[i].innerHTML.toLowerCase().replace(/ /g, '-');
      //     // }

      //     window.onscroll = function () {
      //       if (!docked && (menu.offsetTop - scrollTop() < 0)) {
      //         menu.style.top = 0;
      //         menu.style.position = 'fixed';
      //         menu.style.height = 'auto';
      //         menu.className = 'wrapPortfolioSlider docked';
      //         docked = true;
      //       } else if (docked && scrollTop() <= init) {
      //         menu.style.position = 'absolute';
      //         menu.style.top = init + 'px';
      //         menu.style.height = '228px';
      //         menu.className = menu.className.replace('wrapPortfolioSlider docked', 'wrapPortfolioSlider');
      //         docked = false;
      //       }
      //     };


      //   //   (function () {
      //   //     var link     = document.getElementById('guide-link'),
      //   //         menu     = document.getElementById('menu'),
      //   //         dropdown = document.getElementById('dropdown');

      //   //     link.onmouseover = function () {
      //   //       link.className = 'dark-red';
      //   //       dropdown.style.display = 'block';
      //   //     };
      //   //     link.onmouseout = function (e) {
      //   //       if (e.relatedTarget === dropdown) { return }
      //   //       link.className = link.className.replace('dark-red', '');
      //   //       hide ();
      //   //     };
      //   //     dropdown.onmouseout = function (e) {
      //   //       var t = e.relatedTarget;

      //   //       if (e.target == link) { return }

      //   //       while (t !== document.body) {
      //   //         if (t == dropdown) { return }
      //   //         else               { t = t.parentNode }
      //   //       } 
      //   //       link.className = link.className.replace('dark-red', '');
      //   //       hide ();
      //   //     };

      //   //     function hide() { dropdown.style.display = 'none' }
      //   //   })();
      //   };

      //   function scrollTop() {
      //     return document.body.scrollTop || document.documentElement.scrollTop;
      //   };
      // // })();
