/******************************************************************************
 *
 * Argentum Site General Script
 * http://argentumsite.com/
 *
 * Version 2.0.0
 * Date: Sat 16-04-2011 14:00
 *
 * Author Oleg Skalov
 * Copyright 2010-2011, Argentum Site 
 * http://argentumsite.com/ 
 *
 ******************************************************************************/

var g_os;
var g_focus_flag 	= false;
var g_get 			= new String(window.location);
var g_position 		= g_get.indexOf('#');
var g_hash_page 	= (document.location.hash != '' ? document.location.hash : '');
var g_duration_show = 200;
var g_duration_hide = 600;

//////////////////////////////////
function getRandomInt(min, max)
{
	return Math.floor(Math.random() * (max - min + 1)) + min;
}

function shakePosition()
{
	var element			= $(this);
	var clone			= element.clone();
	var position 		= element.position();
	var coefficient 	= getRandomInt(2, 8);
	var font_size		= parseInt(element.css('font-size'));
	var size_offset		= 2;
	var cnt_iteration	= 4;	//count iteration of cycle
	var delay			= 20; 	//ms
	
	element.css('visibility', 'hidden');
	clone.appendTo(element.parent()).bind('click', linkClick).css({
		'position': 'absolute',	
		'top':		position.top,
		'left':		position.left
	});
	
	for (var i = 0; i < cnt_iteration; i++)
	{
		var new_top 		= Math.floor(getRandomInt(position.top - coefficient, position.top + coefficient));
		var new_left		= Math.floor(getRandomInt(position.left - coefficient, position.left + coefficient));
		var new_font_size	= getRandomInt(font_size - size_offset, font_size + size_offset);
		
		clone.animate(
			{
				'top':			new_top,
				'left':   		new_left,
				'font-size': 	new_font_size
			}, 
			delay
		);
	}
	
	clone.animate(
		{
			'top':			Math.floor(position.top),
			'left':   		Math.floor(position.left),
			'font_size': 	font_size
		}, 
		delay,
		function()
		{
			clone.remove();
			element.css('visibility', 'visible');
		}
	);	
}

//////////////////////////////////
function currentTabShow(tab_id)
{
	var millisecond = 0;
		
	if (tab_id != null)
	{
		millisecond = 400;
		$('.content-block').fadeOut(millisecond);
	}
	
	setTimeout (
		function()
		{
			if (tab_id == null)
			{
				$('.main-menu li a').each(
					function()
					{
						if ($(this).attr('href') == g_hash_page)
						{
							tab_id = $(this).attr('rel');
						}
					}
				);
				
				if (tab_id == null)
				{
					tab_id = $('.main-menu li:first a').attr('rel');
				}
			}
			
			//console.log('tab_id = ' + tab_id);
			
			$('.main-menu li').each(
				function()
				{
					if ($('a', this).attr('rel') == tab_id)
					{
						$(this).addClass('selected');
						$('a', this).addClass('selected');
						$('a', this).unbind('mouseenter');
						
						g_hash_page = $('a', this).attr('href');			
						tab_id 		= $('a', this).attr('rel');
					}
					else
					{
						$(this).removeClass('selected');
						$('a', this).removeClass('selected');
						$('a', this).bind('mouseenter', shakePosition);
					}
				}
			);
			
			$('.tab-block').each(
				function()
				{
					if ($(this).attr('id') == tab_id)
					{
						$(this).show(
							0,
							function() 
							{
								var count_li = $('ul.works-list li', this).length;
								var width_li = $('ul.works-list li', this).width();
								
								$('ul.works-list', this).css('width', count_li * width_li);
								
								g_os = $(this).overscroll({
									showThumbs:		false,
									cursor: 		'auto',
									wheelDirection: 'horizontal'
								});
								
								if ($('ul.works-list', this).width() > $(document).width())
								{
									$('.works-list .work-block', this).css('cursor', 'e-resize');
								}
								
								$('.works-list .work-block').hover(
									function()
									{
										$(".work-title", this).stop().animate({top: '10px'},{queue: false, duration: g_duration_show});
										$(".work-description", this).stop().animate({top: '300px'},{queue: false, duration: g_duration_show});
									}, 
									function() 
									{
										$(".work-title", this).stop().animate({top: '70px'},{queue: false, duration: g_duration_hide});
										$(".work-description", this).stop().animate({top: '220px'},{queue: false, duration: g_duration_hide});
									}
								);
																
								$('.img-block a', this).bind(
									'click',
									function()
									{
										return !g_os.data("dragging");
									}
								);
							}
						);
					}
					else
					{
						$(this).hide(0);
					}
				}
			);
		}, 
		millisecond
	);
	
	if (tab_id != null)
	{
		$('.content-block').fadeIn(400);
	}
}

////////////////////////////////////////
function linkClick()
{
	if (!$(this).hasClass('selected'))
	{
		currentTabShow($(this).attr('rel') != '' ? $(this).attr('rel') : $('a', this).attr('rel'));
	}
}

////////////////////////////////////////
$(document).ready(
	function()
	{
		$("a").focus(
			function()
			{
				$(this).blur();
			}
		);

		////////////////
		$('.language').hover(
			function()
			{
				$(this).animate({left: "0px"}, g_duration_show);
			},
			function()
			{
				$(this).animate({left: "-89px"}, g_duration_hide);
			}
		);

		////////////////
		$('.languages img, .languages a').tooltip({
			track: 			true,
			delay: 			200,
			showURL: 		false,
			showBody: 		" - ",
			fade: 			300,
			lifetime: 		10000
		});
		
		$('.work-block').tooltip({
			track: 			true,
			delay: 			200,
			showURL: 		false,
			showBody: 		" - ",
			fade: 			300,
			lifetime: 		1000
		});

		$('.footer img, .footer a, .alkatraz').tooltip({
			positionLeft: 	true,
			track: 			true,
			delay: 			200,
			showURL: 		false,
			showBody: 		" - ",
			left:			-10,
			top:			-35,
			fade: 			300,
			lifetime: 		10000
		});
		
		/////////////////		
		$('#aboutus-block').mouseleave(
			function() 
			{
				$(document).one(
					'mouseover',
					function(event) 
					{ 
						if (event.target.id != 'logo')
						{
							$('#aboutus-block').hide(g_duration_hide);
						}
						else
						{
							$('#aboutus-block').mouseenter();
						}
					} 
				);
			}
		);
		
		$('.logo').mouseenter(
			function()
			{
				$('#aboutus-block').mouseenter();
				$('#aboutus-block').show(g_duration_show);
			}
		);
		
		/////////////////
		$('#contacts-block').mouseleave(
			function() 
			{ 
				$(this).hide(g_duration_hide); 
				$('.contacts').show(0);
			}
		);
		
		$('.contacts').mouseenter(
			function()
			{
				$('#contacts-block').mouseenter();
				$('#contacts-block').show(g_duration_show);
				$(this).hide(0);
			}
		);

		/////////////////
		if ($.browser.mozilla)
		{
			var replace_str = $('.contacts').html();
			
			$('.contacts').html(replace_str.replace(" <a", "&nbsp;<a"));
		}

		/////////////////
		//$('.contacts a').bind('mouseenter', shakePosition);		
		$('.main-menu a').bind('click', linkClick);

		//////////////////
		mainMenuShow();
		setPosition(g_focus_flag);
		currentTabShow();
	}
);	


////////////////////////////////////////	
$(window).resize(
	function()
	{
		setPosition(g_focus_flag);
		g_focus_flag = true;
	}
);

////////////////////////////////////////	
$(window).focus(
	function()
	{
		if (g_focus_flag)
		{
			setPosition(g_focus_flag);
		}
		else
		{
			g_focus_flag = true;
		}
	}
);
	
