/**
 * jQuery.ScrollTo - Easy element scrolling using jQuery.
 * Copyright (c) 2007-2009 Ariel Flesler - aflesler(at)gmail(dot)com | http://flesler.blogspot.com
 * Dual licensed under MIT and GPL.
 * Date: 5/25/2009
 * @author Ariel Flesler
 * @version 1.4.2
 *
 * http://flesler.blogspot.com/2007/10/jqueryscrollto.html
 */
;(function(d){var k=d.scrollTo=function(a,i,e){d(window).scrollTo(a,i,e)};k.defaults={axis:'xy',duration:parseFloat(d.fn.jquery)>=1.3?0:1};k.window=function(a){return d(window)._scrollable()};d.fn._scrollable=function(){return this.map(function(){var a=this,i=!a.nodeName||d.inArray(a.nodeName.toLowerCase(),['iframe','#document','html','body'])!=-1;if(!i)return a;var e=(a.contentWindow||a).document||a.ownerDocument||a;return d.browser.safari||e.compatMode=='BackCompat'?e.body:e.documentElement})};d.fn.scrollTo=function(n,j,b){if(typeof j=='object'){b=j;j=0}if(typeof b=='function')b={onAfter:b};if(n=='max')n=9e9;b=d.extend({},k.defaults,b);j=j||b.speed||b.duration;b.queue=b.queue&&b.axis.length>1;if(b.queue)j/=2;b.offset=p(b.offset);b.over=p(b.over);return this._scrollable().each(function(){var q=this,r=d(q),f=n,s,g={},u=r.is('html,body');switch(typeof f){case'number':case'string':if(/^([+-]=)?\d+(\.\d+)?(px|%)?$/.test(f)){f=p(f);break}f=d(f,this);case'object':if(f.is||f.style)s=(f=d(f)).offset()}d.each(b.axis.split(''),function(a,i){var e=i=='x'?'Left':'Top',h=e.toLowerCase(),c='scroll'+e,l=q[c],m=k.max(q,i);if(s){g[c]=s[h]+(u?0:l-r.offset()[h]);if(b.margin){g[c]-=parseInt(f.css('margin'+e))||0;g[c]-=parseInt(f.css('border'+e+'Width'))||0}g[c]+=b.offset[h]||0;if(b.over[h])g[c]+=f[i=='x'?'width':'height']()*b.over[h]}else{var o=f[h];g[c]=o.slice&&o.slice(-1)=='%'?parseFloat(o)/100*m:o}if(/^\d+$/.test(g[c]))g[c]=g[c]<=0?0:Math.min(g[c],m);if(!a&&b.queue){if(l!=g[c])t(b.onAfterFirst);delete g[c]}});t(b.onAfter);function t(a){r.animate(g,j,b.easing,a&&function(){a.call(this,n,b)})}}).end()};k.max=function(a,i){var e=i=='x'?'Width':'Height',h='scroll'+e;if(!d(a).is('html,body'))return a[h]-d(a)[e.toLowerCase()]();var c='client'+e,l=a.ownerDocument.documentElement,m=a.ownerDocument.body;return Math.max(l[h],m[h])-Math.min(l[c],m[c])};function p(a){return typeof a=='object'?a:{top:a,left:a}}})(jQuery);

/**
 * jQuery.placeholder - Placeholder plugin for input fields
 * Written by Blair Mitchelmore (blair DOT mitchelmore AT gmail DOT com)
 * Licensed under the WTFPL (http://sam.zoy.org/wtfpl/).
 * Date: 2008/10/14
 *
 * @author Blair Mitchelmore
 * @version 1.0.1
 *
 **/
new function($) {
    $.fn.placeholder = function(settings) {
        settings = settings || {};
        var key = settings.dataKey || "placeholderValue";
        var attr = settings.attr || "placeholder";
        var className = settings.className || "placeholder";
        var values = settings.values || [];
        var block = settings.blockSubmit || false;
        var blank = settings.blankSubmit || false;
        var submit = settings.onSubmit || false;
        var value = settings.value || "";
        var position = settings.cursor_position || 0;

        
        return this.filter(":input").each(function(index) { 
            $.data(this, key, values[index] || $(this).attr(attr)); 
        }).each(function() {
            if ($.trim($(this).val()) === "")
                $(this).addClass(className).val($.data(this, key));
        }).focus(function() {
            if ($.trim($(this).val()) === $.data(this, key)) 
                $(this).removeClass(className).val(value)
                if ($.fn.setCursorPosition) {
                  $(this).setCursorPosition(position);
                }
        }).blur(function() {
            if ($.trim($(this).val()) === value)
                $(this).addClass(className).val($.data(this, key));
        }).each(function(index, elem) {
            if (block)
                new function(e) {
                    $(e.form).submit(function() {
                        return $.trim($(e).val()) != $.data(e, key)
                    });
                }(elem);
            else if (blank)
                new function(e) {
                    $(e.form).submit(function() {
                        if ($.trim($(e).val()) == $.data(e, key)) 
                            $(e).removeClass(className).val("");
                        return true;
                    });
                }(elem);
            else if (submit)
                new function(e) { $(e.form).submit(submit); }(elem);
        });
    };
}(jQuery);

$.fn.equalHeight = function ()
{
	var tallest = 0;
	this.each(function(){
		var thisHeight = $(this).height();
		if(thisHeight > tallest) {
			tallest = thisHeight;
		}
	});
	$(this).height(tallest);
	return true;
}


$(function(){
	$('[placeholder]').placeholder();
	
	$('#slider').anythingSlider({
		delay: 5000,
		animationTime : 1200
	});
	
	$('#counters').fadeTo(0,0.1).hover(
		function(){	$(this).fadeTo(900,1);},
		function(){	$(this).fadeTo(0,0.1);}
	);
	$('#wrapper').append('<div id="bgl"></div>');

	$(window).load(function(){$('#advantages .col3_item').equalHeight();});
	
	$('a#recall').fancybox({
		'width'				: 250,
		'height'			: 250,
        'autoScale'     	: false,
        'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'type'				: 'iframe'
	});
	
	if($('.works_all_container').size()>0)
	{
		$(".works_all_container").quickPager({pageSize:100});
		$(".works_all_container").data("page_url",$(".works_all_container").attr('url_page'));
		
		
		$.scrollTo($('.works_all_container'), 1000, {onAfter: function(){
			$($("#thumb_"+hash).size()>0)
				$("#thumb_"+hash).click();
		}});
		var hash = window.location.hash.replace("#","");
		
		$('.works_all_container a.thumb').fancybox({
			'showCloseButton'	: true,
			'titlePosition' 	: 'inside',
//			'type'				: 'ajax',
			'onStart'			: function(selectedArray, selectedIndex, selectedOpts){
				
				var site_url = $('.works_all_container').data('page_url');
				var current_thumb = $('.works_all_container a.thumb').eq(selectedIndex);
				
				var vtitle = $(current_thumb).attr('title');
				var vurl = site_url+'#'+$(current_thumb).attr('id').replace('thumb_','');
				var vimage = $(current_thumb).find('img').attr('src');
				var vid = $(current_thumb).attr('id').replace('thumb_','');
				
				document.title = vtitle;
				window.location.hash = '#'+vid;
				
				
			},
			'onComplete'		: function(currentArray, currentIndex, currentOpts) {
				$('#fancybox-wrap').width('800px');
				var calc = (800-$('#fancybox-inner').width())/2;
				if (calc<10) calc = 10;
				$('#fancybox-inner').css({'left': calc  });

				$.fancybox.resize();
				$.fancybox.center();
				gapi.plusone.go();
			},
			'onClosed'		: function(currentArray, currentIndex, currentOpts) {
				//window.location.hash = '';	
			},
			'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
				var html = '';
				var site_url = $('.works_all_container').data('page_url');
				var current_thumb = $('.works_all_container a.thumb').eq(currentIndex);
				
				var vtitle = 'Я хочу себе такой моддинг - '+title;
				var vurl = site_url+'#'+$(current_thumb).attr('id').replace('thumb_','');
				var vimage = $(current_thumb).find('img').attr('src');
				var vid = $(current_thumb).attr('id').replace('thumb_','');
				
				html = html + '<div class="fancybox-content-likes">';
				html = html + '<div class="title">' + title + '</div>';
				html = html + '<div class="in_likes">';
				
				// Vkontakte
				html = html + '<div class="in_likes_item"><div id="vk_like_'+currentIndex+'"></div></div>';
				$('vk_like_'+currentIndex).ready(function(){
					VK.Widgets.Like("vk_like_"+currentIndex, 
					{ 
						pageTitle: vtitle, 
						pageUrl: vurl,
						pageImage: vimage,
						type: 'button'
					},
					 vid);
					 
				});
				
				// facebook
				html = html + '<div class="in_likes_item l2"><div id="fb_like_'+currentIndex+'"><iframe src="//www.facebook.com/plugins/like.php?href='+vurl.replace("#", "%23")+'&amp;send=false&amp;layout=button_count&amp;width=150&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font&amp;height=30" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:150px; height:30px;" allowTransparency="true"></iframe></div></div>';
				
				// google plus
				html = html + '<div class="in_likes_item l3"><div id="gp_like_'+currentIndex+'"><g:plusone annotation="none" width="240" href="'+vurl+'"></g:plusone></div></div>';
				
								
				
				
				
				
				html = html + '</div>';
				
				html = html + '</div>';
				return html;
			}
		});
		
		
	}
	
	/* <!-- Put this div tag to the place, where the Like block will be -->
	<div id="vk_like"></div>
	<script type="text/javascript">
	VK.Widgets.Like("vk_like", {type: "button"});
	</script>
	*/
	
	
	
});
