
$(document).ready(function() {
	
//	$('#container').css('height', $(document).height() - $('#footer_container').height() - 20);
	
	init_feedbackform();
	init_send_form();
	
	init_bottom_gallery();
	
	catch_leftmenu_links();
	
	if ($('#googlemap').length) {
	
			if (GBrowserIsCompatible()) { 
				var map = new GMap2(document.getElementById("googlemap"));
				map.setCenter(new GLatLng(59.45265,24.717318), 15);
				map.addOverlay(new GMarker(map.getCenter()));
			}
		
	}
	
	if ($('#contactgooglemap').length) {
	
			if (GBrowserIsCompatible()) { 
				var map = new GMap2(document.getElementById("contactgooglemap"));
				map.setCenter(new GLatLng(59.440581,24.748399), 15);
				map.addOverlay(new GMarker(map.getCenter()));
			}
		
	}
	
	init_site_enter();
	
	init_modelist();
	
	
	init_gallery();
	
	if ($('#category_name_selector').length)
	{
		$('#category_name_selector').change(function()
		{
			window.location.href = base_url + '/' + current_lang + '/' + $(this).val();
		})
	}	
	
	
});


function init_cross_links()
{
	if ($('#cross_links').length)
	{
		$('#cross_links div.li').click(
				function()
				{
					window.location.href = $('a', this).attr('href');
				}
		);
		$('#cross_links div.li').hover(
				function()
				{
					$('img', this).addClass('active_model');
				},
				function()
				{
					$('img', this).removeClass('active_model');
				}
		);
	}
}
function init_modelist()
{
	
	$('#model_list img').css('opacity', 0.65);
	if ($('#model_list').length)
	{
		$('#model_list div.li').click(
				function()
				{
					window.location.href = $('a', this).attr('href');
				}
		);
		$('#model_list div.li').hover(
				function()
				{
					$('img', this).css('opacity', 1);
				},
				function()
				{
					$('img', this).css('opacity', 0.65);
				}
		);		
	}
}

function init_site_enter()
{
	if ($('#firstpage_picture').length) {
		var left = $('#firstpage_picture img').width()/2 - 	$('#firstpage_picture .enter').width()/2;
		var top = $('#firstpage_picture img').height()/2 - 	$('#firstpage_picture .enter').height()/2;
		$('#firstpage_picture .enter').css('left', left).css('top', top);
		$('#firstpage_picture').hover(
				function()
				{
					$('#firstpage_picture .enter').show();
				},
				function()
				{
					$('#firstpage_picture .enter').hide();
				});
	}
}

function catch_leftmenu_links()
{
	if ($('#leftmenu').length) {
		$('#leftmenu .li').click(function() 
			{
				window.location.href = $(this).children('a').attr('href');
			}
		)
	}
}

function getNode(id)
{
	if(document.all)
	{
	    var node = document.all[id];
	}
	else if(document.layers)
	{
	    var node = document.layers[id];
	}
	else
	{
	    var node = document.getElementById(id);
	}
	if(node)return node;
}

function move_content(obj_name, direction){
	var step = 0;
	
	obj = getNode(obj_name);
	if (direction == "right"){
		step = 5;
	}else{
		step = -5;
	}
	obj.scrollLeft = obj.scrollLeft + step;
	scrolltimer = window.setTimeout("move_content('"+obj_name+"', '"+direction+"')", 10);
}

function slow_move_content(obj_name, direction){
	var step = 0;
	
	obj = getNode(obj_name);
	if (obj) {
		if (direction == "right"){
			step = 1;
		}else{
			step = -1;
		}
		obj.scrollLeft = obj.scrollLeft + step;
		slow_scrolltimer = window.setTimeout("slow_move_content('"+obj_name+"', '"+direction+"')", 100);
	}
}

function stopmove_content()
{
	clearTimeout(scrolltimer);
}
function slow_stopmove_content()
{
	clearTimeout(slow_scrolltimer);
}

function init_bottom_gallery() {
	if ($('#bottom_gallery').length) {
		var direction = 'right';
		$('#gallery_pictures a').css('opacity', 0.65);
		
		$('#bottom_gallery .gallery_left, #bottom_gallery .gallery_right').hide();	
		
		if ($('#gallery_pictures').width() < $('#gallery_pictures img').length * 100)
		{
			$('#bottom_gallery').hover(function()
			{
				$('#bottom_gallery .gallery_left, #bottom_gallery .gallery_right').show();	
			}, function()
			{
				$('#bottom_gallery .gallery_left, #bottom_gallery .gallery_right').hide();	
			});
			
$('#models_zoom .left, #models_zoom .right').hide();
		}		
		
		slow_move_content('gallery_pictures', direction);
		
		$('#bottom_gallery .gallery_left').hover(function() {
			slow_stopmove_content();
			move_content('gallery_pictures', 'left');
			direction = 'left';
		}, function () {
			stopmove_content();
			slow_move_content('gallery_pictures', direction);
		})
		$('#bottom_gallery .gallery_right').hover(function() {
			slow_stopmove_content();
			move_content('gallery_pictures', 'right');
			direction = 'right';
		}, function () {
			stopmove_content();
			slow_move_content('gallery_pictures', direction);
		})
		
		$('#bottom_gallery .gallery_right, #bottom_gallery .gallery_left')
			.click(function() 
			{
				return false;
			})
		
		$('#gallery_pictures img')
			.hover(function(e) 
			{
				slow_stopmove_content();
			}, function() {
//				slow_move_content('gallery_pictures', direction);
			})
		/*
		$('#gallery_pictures').mousemove(function(e) {
			$('#toolTip')
				.css('top',		e.pageY	+	15)
				.css('left',	e.pageX	+	15);
		})	
		*/
		
		$('#models_zoom .left, #models_zoom .right').hide();
		
		$('#models_zoom').hover(function()
		{
			$('#models_zoom .left, #models_zoom .right').show();
		},function()
		{
			$('#models_zoom .left, #models_zoom .right').hide();
		})
		
		
		
		$('#models_zoom .left').click(function() {
			
			if ($('img:first', $(this).parent()).size() == 0) {
				return;
			}
			var row_id = $('img:first', $(this).parent()).attr('id').split('model_image_');
			var id = row_id[1];
			if (id != undefined)
			{
				var prev = $('#thumb_'+id).prev();
				if (prev.length == 0)
				{
					prev = $('#gallery_pictures a:last');
				}
				$(prev).click();
			}
			return false;			
			
		})
		
		$('#models_zoom .right').click(function() {
			if ($('img:last', $(this).parent()).size() == 0) {
				return;
			}
			var row_id = $('img:last', $(this).parent()).attr('id').split('model_image_');
			var id = row_id[1];
			
			if (id != undefined)
			{
				var next = $('#thumb_'+id).next();
				if (next.length == 0)
				{
					
					next = $('#gallery_pictures a:first');
				}
				$(next).click();
			}
			
			return false;			
			
		})
		
		$('#gallery_pictures a').click(function() 
		{
			$('#gallery_pictures a').css('opacity', 0.65);
			$(this).css('opacity', 1);
			
			center_position(this, 'gallery_pictures');
			
			slow_stopmove_content();
			
			row_id = $(this).attr('id').split('thumb_');
			var id = row_id[1];
			
			if (id != undefined)
			{
				load_model_image(id);
			}
					
			
			
			return false;
			
		})
	}
	
}
function center_position(e, list)
{
	data = getNode(list);
	count = $('img', e).attr('id').split('index_image_');
	scroll_left = count[1] * 100 - 400;
	data.scrollLeft = scroll_left;
}

var load_model_image_processing = false;

function load_model_image(id)
{
	
		if (load_model_image_processing === true) {
			return;
		}
		load_model_image_processing = true;
		$('#modles_zoom .container').addClass('loading');
		$.get(base_url + '/_ajax_functions.php?action=modelimage&Name=' + article_name + '&id='+id,
		function(data)
		{
			
			if (data != null)
			{
				$('#models_zoom .container').fadeOut(function() {
					$('#models_zoom').height($(this).height());
					$(this).empty().append(data).fadeIn();
					
					$('img', this).each(function(){
						
						$(this).hide();
						var src = $(this).attr('src');
						var id = $(this).attr('id');
						var img = new Image();
						  
						  // wrap our new image in jQuery, then:
						  $(img).attr('style', $(this).attr('style')).attr('id', id).attr('alt', $(this).attr('alt'))
						    // once the image has loaded, execute this code
						    .load(function () {
						      // set the image hidden by default    
						      $(this).hide();
						      // with the holding div #loader, apply:
						      $('#models_zoom .container')
						        // remove the loading class (so no background spinner), 
						        .removeClass('loading')
						        // then insert our image
						        .append(this);
						    
						      // fade our image in to create a nice effect
						      $(this).fadeIn();
						      
						      $('#models_zoom').height('auto');
						    })
						    
						    // if there was an error loading the image, react accordingly
						    .error(function () {
						      // notify the user that the image could not be loaded
						    })
						    
						    // *finally*, set the src attribute of the new image to our image
						    .attr('src', src);
						    
						    $(this).remove();
						    
						    
		
							load_model_image_processing = false;				
						
					})
					
				})

		  }
		});
}

function init_feedbackform()
{
	if ($('#FeedbackForm').length != 0)
	{
		
		$('#submit_form').click(
			function()
			{
				if (check_feedbackform())
				{
					$('#FeedbackForm').sbmit();
				}
				return false;
			}
		)
		
		
		clear_inputs_hovers();
		
		$('#FeedbackForm input, .#FeedbackForm textarea').focus(
			function() 
			{
				activate_inputbox($(this));
			}
		);
		
		$('#FeedbackForm input, #FeedbackForm textarea').blur(
			function() 
			{
				clear_inputs_hovers();
			}
		);			
	}
}


function init_send_form()
{
	if ($('#send_form').length != 0)
	{
		
		$('#submit_form').click(
			function()
			{
				if (check_send_form())
				{
					$('#send_form').sbmit();
				}
				return false;
			}
		)
		
		
		clear_inputs_hovers();
		
		$('#send_form input, .#send_form textarea').focus(
			function() 
			{
				activate_inputbox($(this));
			}
		);
		
		$('#send_form input, #send_form textarea').blur(
			function() 
			{
				clear_inputs_hovers();
			}
		);			
	}
}


function activate_inputbox(inputbox) {
	
	main_div = $(inputbox).parent();
	if (!$(main_div).is('active')) {
		$(main_div).addClass('active');
	}
}

function clear_inputs_hovers() {
	$('#FeedbackForm .active, #send_form .active').removeClass('active');
}


function clearForm(form) {
  $(':input', form).each(function() {
	var type = this.type;
	var tag = this.tagName.toLowerCase(); 
	if (type == 'text' || type == 'password' || tag == 'textarea')
	  this.value = "";
	else if (type == 'checkbox' || type == 'radio')
	  this.checked = false;
	else if (tag == 'select')
	  this.selectedIndex = -1;
  });
};

function check_feedbackform() {
	var accept = true;
	
	$('#FeedbackForm div.required input, #FeedbackForm div.required textarea').each(function () {
		if ($(this).val() == "") {
			accept = false;
			$(this).parent().addClass('alert');
		} else {
			$(this).parent().removeClass('alert');
		}	
		
		if ($(this).attr('id') == 'form_email' && !$(this).val().match(new RegExp('^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]{2,})+$'))) {
			accept = false;
			$(this).parent().addClass('alert');
		} 
		
		if ($(this).attr('id') == 'form_phone' && !$(this).val().match(new RegExp('^[0-9\-\+\\s\(\)]{7,}$'))) {
			accept = false;
			$(this).parent().addClass('alert');
		} 
	});
	
	return accept;
}
function check_send_form() {
	var accept = true;
	
	$('#send_form div.required input, #send_form div.required textarea').each(function () {
		if ($(this).val() == "") {
			accept = false;
			$(this).parent().addClass('alert');
		} else {
			$(this).parent().removeClass('alert');
		}	
		
		if ($(this).attr('id') == 'form_email' && !$(this).val().match(new RegExp('^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]{2,})+$'))) {
			accept = false;
			$(this).parent().addClass('alert');
		} 
		
		if ($(this).attr('id') == 'form_phone' && !$(this).val().match(new RegExp('^[0-9\-\+\\s\(\)]{7,}$'))) {
			accept = false;
			$(this).parent().addClass('alert');
		} 
	});
	
	return accept;
}

var timer = 5000;
function stopRollBackgrounds()
{
	clearTimeout(rolling_background_timeout);
}

function init_gallery()
{
	if ($('#gallery').length != 0) 
	{
		$('#gallery img').hide();
		$('#gallery img:first').show().addClass('active');
		rolling_background_timeout = window.setTimeout('change_gallery_image()', timer);	
	}
}

function change_gallery_image() 
{
	images = $('#gallery img');
	
	current_image = $('#gallery img.active');
	if ($(current_image).length == 0)
	{
		current_image = $('#gallery img:first');
	}
	$(current_image).fadeOut('slow', function() {
		
		next_image = $(current_image).parent().next('div').children('img');
		if ($(next_image).length == 0)
		{
			next_image = $('#gallery img:first');
		}
		
		$(next_image).fadeIn('slow').addClass('active');
			
		window.setTimeout('change_gallery_image()', timer);		
		
		
	}).removeClass('active');
	

		
}

