(function($) {

	$.fn.sorted = function(customOptions) {
		var options = {
			reversed: false,
			by: function(a) {
				return a.text();
			}
		};
		$.extend(options, customOptions);
	
		$data = $(this);
		arr = $data.get();
		arr.sort(function(a, b) {
			
			var valA = options.by($(a));
			var valB = options.by($(b));
			if (options.reversed) {
				return (valA < valB) ? 1 : (valA > valB) ? -1 : 0;				
			} else {		
				return (valA < valB) ? -1 : (valA > valB) ? 1 : 0;	
			}
		});
		return $(arr);
	};

})(jQuery);

$(document).ready(function()
{

	images_count = $('#hidd_imgs .image').size();
	
	var counter = 0;
	$('#hidd_imgs .image').each(function(index) {
		counter++;
		$(this).attr('rel', counter);
	});
	
	$('#all_images .curr_img').html($('#hidd_imgs .image').first().html());

	
	if (images_count)
	{
		
		cur_img = 1;
		
		for( i=1; i <= images_count; i++)
		{
			el = '<a class="numb" rel="'+i+'">'+i+'</a>';
			$('#the_pages_links').append(el);
		}
		
		
		$('#images_num a[rel=1]').addClass('active');
		
		$('#hidd').append($('#pics_menu'));
		
		function go_in_gall(w, m)
		{
			$('#images_num a.numb').removeClass('active');
			$('#images_num a[rel='+w+']').addClass('active');
			
			if (m==1)
			{
				$('#all_images .next_img2').html($('#hidd_imgs .image[rel='+w+']').first().html());
				s = 800;
				$('#all_images').stop().animate({
					"marginLeft": "-1900px"
					}, s, 'swing', function() {
					
					$('#all_images .curr_img').html($('#all_images .next_img2').html());
					$('#all_images').css('marginLeft', '-950px');
				});
			}
			else
			{
				$('#all_images .prev_img2').html($('#hidd_imgs .image[rel='+w+']').first().html());
				s = 800;
				$('#all_images').stop().animate({
					"marginLeft": "0px"
					}, s, 'swing', function() {
					
					$('#all_images .curr_img').html($('#all_images .prev_img2').html());
					$('#all_images').css('marginLeft', '-950px');
				});
			}
			
			
			
			
			if (w < 4)
				pages_m_s = 1;
			else
				pages_m_s = w - 3;
			
			pos_left = $('#images_num a[rel='+pages_m_s+']').position().left * -1
			
			$('#the_pages_links').stop().animate({
				"marginLeft": pos_left+"px"
			}, s, 'swing', function() {
				//
			});
			
			
			
			
		}
		
		$('#images_num a').click(function()
		{
			rel = parseInt($(this).attr("rel"));
			var m = 0;
			if (rel > cur_img)
				m = 1;
			cur_img = rel;
			go_in_gall(cur_img, m);
			return false;
		});
		
		$('.next_img').click(function()
		{
			if (cur_img < images_count)
			{
				cur_img++;
				go_in_gall(cur_img, 1);
			}
			else
			{
				url = $('#next_project_name a.nav').attr('href');
				window.location.href = url;
			}
			return false;
		});
		
		
		$('.prev_img').click(function()
		{
			cur_img--;
			if (cur_img > 0)
			{
				go_in_gall(cur_img, 2);
			}
			else
			{
				url = $('#prev_project_name a.nav').attr('href');
				window.location.href = url;
			}
			return false;
		});
	}
	
	
	list_count = $('#item_list .item').size();
	if (list_count)
	{
		var $list = $('#item_list');
		var $data = $list.clone();
		
		var $preferences = {
			duration: 600,
			easing: 'easeInOutQuad',
			attribute: 'id',
			adjustHeight: false
		};
		
		$('#order_list a').click(function()
		{
			var $filtered_data = $data.find('div.item');
			
			if ($('#item_list').hasClass('list_view'))
			{
				$('#item_list').removeClass('list_view');
				
				var $sorted_data = $filtered_data.sorted({
					by: function(v) {
						return parseInt($(v).find('a.item_index').attr('rel'));
					}
				});
			}
			else
			{				
				$('#item_list').addClass('list_view');
				
				var $sorted_data = $filtered_data.sorted({
					by: function(v) {
						return $(v).find('div.item_title a').text();
					}
				});
			}
			
			$list.quicksand( $($sorted_data, $preferences), function() {
			
			});
				
			return false;
		});
		
		$('#item_list .item').live('mouseenter', function() {
				$(this).find('.item_img').stop().animate({
					opacity: 0.45
					}, 500, function() {
					// Animation complete.
				});
		});

		$('#item_list .item').live('mouseleave', function() {
				$(this).find('.item_img').stop().animate({
					opacity: 1
					}, 500, function() {
					// Animation complete.
				});
		});
		
	}
	
	$('#logos_list .logo_item').live('mouseenter', function() {
			$(this).find('.the_f_logo').stop().animate({
					/*height: '0px'*/
					opacity: 0
				}, 500, function() {
				// Animation complete.
			});
	});
	
	$('#logos_list .logo_item').live('mouseleave', function() {
			$(this).find('.the_f_logo').stop().animate({
					/*height: '160px'*/
					opacity: 1
				}, 500, function() {
				// Animation complete.
			});
	});
	
	
	$('#contact_send').click(function()
	{
		var name = $('#con_form_name').val();
		var company = $('#con_form_company').val();
		var email = $('#con_form_email').val();
		var phone = $('#con_form_phone').val();
		var message = $('#con_form_message').val();
		
		var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		
		if (name == "" || email == "" || phone == "")
		{
			alert("You must enter Name, Email and Phone number");
		}else if (!filter.test(email)) {
			alert("Email is not valid");
		}
		else {
			$.post("/send_mail.php", { v_name: name, v_company: company, v_email: email, v_phone: phone, v_message: message },
			function(data) {
			     alert("message sent successfully");
			});
		}
		
		return false;
	});
	
	$('#item_list .item').each(function(index) {
		hr = $(this).find('.item_img a').attr('href');
		if (hr == "../3D/Valtech/")
			$(this).find('.item_img a').removeAttr('href')
	});

});
