$(document).ready(function(){
	markup_init();
	qblocks_init();
	fonts_init();
	fancybox_init();
	fillform_init();
});

/*************************************************************************/

function markup_init()
{
	$('.qblock_wrapper').each(function(){
		$(this).addClass('n'+($(this).index('.qblock_wrapper')+1));
	});
	
	$('.content li').each(function(){
		$(this).html('<div class="li_inner">'+$(this).html()+'</div>');
	});
	
	$('.content > li.quote > .li_inner').each(function(){
		var $author=$(this).find('.author');
		if($author.length)
		{
			var author=$author.text();
			$author.remove();
			$(this).html('<div class="quote"><h1>&ldquo;</h1>'+$(this).html()+'</div><div class="author">'+author+'</div>');
		}
	});
	
	var $butt=$('.center_button > a:first');
	if($butt.length)
	{
		$butt.html('<span class="hov"></span><span class="inner">'+$butt.html()+'</span>');
		if($.browser.msie)
		{
			$butt.mouseenter(function(){
				$(this).children('.hov').show();
			}).mouseleave(function(){
				$(this).children('.hov').hide();
			});
		}
		else
		{
			$butt.mouseenter(function(){
				$(this).children('.hov').stop().fadeTo(300,1);
			}).mouseleave(function(){
				$(this).children('.hov').stop().fadeTo(200,0);
			});
		}
	}
	
	$('.countdown').each(function(){
		var txt=$(this).text().replace( /^\s+/g, '').replace( /\s+$/g, '');
		var tmp=txt.split(' ');
		if(tmp.length == 2)
		{
			var tmp_d=tmp[0].split('-');
			var tmp_h=tmp[1].split(':');
			if(tmp_d.length == 3 && tmp_h.length == 3)
			{
				$(this).countdown({
					until: new Date(tmp_d[0], tmp_d[1]-1, tmp_d[2], tmp_h[0], tmp_h[1], tmp_h[2]),
					labels: ['years', 'months', 'weeks', 'days', 'hours', 'min', 'sec'],
					labels1: ['year', 'month', 'week', 'day', 'hour', 'min', 'sec']
				});
			}
			else
				$(this).remove();
		}
		else
			$(this).remove();
	});
	
	$('.socials > a').fadeTo(0,0.5).mouseenter(function(){
		$(this).stop().fadeTo(400,1);
	}).mouseleave(function(){
		$(this).stop().fadeTo(300,0.5);
	});
	
	/*
	// show bg-color only afret bg-image is loaded
	var body_bg=$('body').css('background-image');
	if(body_bg != 'none')
	{
		body_bg=body_bg.replace(/url\("(.*)"\)/,'$1');
		var body_bg_color=$('body').css('background-color');
		$('body').css('background-color','#ffffff');
		$("<img />").attr("src", body_bg).load(function(){
			alert(body_bg_color);
			$('body').css('background-color',body_bg_color);
		});
	}
	*/
}

/*************************************************************************/

function qblocks_init()
{
	$('.qblock .content').each(function(){
		if($(this).children().length > 1)
		{
			var $qb=$(this).parent();
			var ind=$qb.parent().index();
			var txt='Next';
			var $nl=$qb.find('.next_link_anchor');
			if($nl.length)
			{
				txt=$nl.text();
				$nl.remove();
			}
			$qb.append('<div class="control"><span class="next" id="qblock'+ind+'_next">'+txt+'</span></div>');
			$(this).cycle({
				timeout: 0,
				fx: 'scrollHorz',
				next:   '#qblock'+ind+'_next',
				easing: 'easeOutExpo'
			});
		}
	});
	
	$('.control .next, .static_block a').mouseenter(function(){
		$(this).stop().animate({paddingLeft:'4px', marginRight: '-4px'},500,'easeOutExpo');
	});
	
	$('.control .next, .static_block a').mouseleave(function(){
		$(this).stop().css('top','0').css('left','0').animate({paddingLeft:'0', marginRight: '0'},300,'easeOutExpo');
	});
	
	$('.control .next, .static_block a').mousedown(function(){
		$(this).stop().css('top','1px').css('left','1px');
	});
	
	$('.control .next, .static_block a').mouseup(function(){
		$(this).stop().css('top','0px').css('left','0px');
	});
}

/*************************************************************************/

function fonts_init()
{
	Cufon.replace('.cufon, h1, h2, h3, h4');
}

/*************************************************************************/

function fancybox_init()
{
	$("a[rel=fancybox]").fancybox({
		titlePosition: 'inside' 
	});
}

/*************************************************************************/

// "Fill the form" block
function fillform_init()
{
  var options = {
		data: { site: 1 },
		success: fillform_success,
		dataType: 'json',
		beforeSubmit: fillform_before
	}; 	
	
	$("#filltheform_form").validate({
		submitHandler: function(form) {
			$(form).ajaxSubmit(options);
		},
		errorPlacement: function(error, element) {
			error.addClass('errorTip');
			//error.insertAfter(element);
		},
		wrapper: 'div'
	});
	
	$("#filltheform_form").find("input[type=text], textarea").focus(function(){
		if($(this).val() == this.defaultValue)
			$(this).attr('value','');
	}).blur(function(){
		if($(this).val() == '')
			$(this).attr('value',this.defaultValue);
	}).each(function(){
		var $icon=$(this).next();
		if($icon.hasClass('input_icon'))
		{
			var pos=$(this).position();
			pos.left+=$(this).outerWidth()-25;
			pos.top+=6;
			var hei=$(this).outerHeight()-2;
			$icon.css('height',hei+'px');
			$icon.css('left',pos.left+'px');
			$icon.css('top',pos.top+'px');
			$icon.css('display','block');
		}
	});
}

function fillform_before()
{
	var $obj=$('#filltheform_form');
	$obj.fadeTo(300,0.5);
	$obj.before('<div id="filltheform_form_blocker" style="position:absolute;width:'+$obj.outerWidth()+'px;height:'+$obj.outerHeight()+'px;z-index:9999;background:url(img/ajax-loading.gif) no-repeat center center"></div>');
}

function fillform_success(obj)
{
	$('#filltheform_form_blocker').remove();
	if(obj.error == 0)
	{
		$('#filltheform_form').fadeOut(300,function(){
			$('#filltheform_form').remove();
			$('#filltheform_form_success').fadeIn(200);
		});
	}
	else
	{
		$('#filltheform_form').fadeOut(300,function(){
			$('#filltheform_form').remove();
			$('#filltheform_form_error').fadeIn(200);
		});		
	}
}

