var is_click_search_button = false;

$(document).ready(function(){
	set_navBar();
	set_leftMenu();
	animate_ClientList();
	
	if(typeof(current_image) != 'undefined'){
		set_galleryNavigation();
	}
	
	formControls();
	
	$('#AdvertisementForm').submit(function(){
		$(this).attr('action',$(this).attr('action')+$('#searching_word').val()+'/lang:'+$('#current_lang').val());
	});
	
	$('#searching_word').click(function(){
		if(!is_click_search_button){
			$(this).val('');
			is_click_search_button = true;
		}
	});
	
});

$(window).load(function(){
	set_navBar();
});
$(window).resize(function(){
	set_navBar();
});

var chanche_blocks_positions = false;
function set_navBar(){
	
	// if #content height < docunet.height for IE6
	if($('#content').height() <= $(document).height()){
		$(document).width($(document).width());
	}
	// if $(document).width() == 1000 for FireFox
	if($(document).width() == 1000){
		$('body > div#left_block, body > div#right_block').css('position','absolute');
		chanche_blocks_positions = true;
	}else if(chanche_blocks_positions && $(document).width() > 1000){
		$('body > div#left_block, body > div#right_block').css('position','fixed');
		chanche_blocks_positions = false;
	}
	
	// position left and right blocks
	var main = $('#wrapper').offset();
	$('#left_block').css('left',main.left-190);
	$('#right_block').css('left',main.left+620);
	$('#left_block, #right_block').css('display','block');
}

function set_leftMenu(){
	
	$('#main_menu li a.bash_item').click(function(){
		$('#main_menu li a.bash_item').removeClass('current').parent().parent('li').next('li').hide();
		$('#main_menu li a.bash_item').next('a').removeClass('main-menu-arrow-link-colapsed');
		$('#main_menu li a.bash_item').next('a').addClass('main-menu-arrow-link');
		$(this).addClass('current').parent().parent('li').next('li').show();
		$(this).addClass('current').next('a').removeClass('main-menu-arrow-link');
		$(this).addClass('current').next('a').addClass('main-menu-arrow-link-colapsed');
	});
	
}

var all_image_count = null;

function set_galleryNavigation(){
	all_image_count = Math.abs($('em#gallery_count_img').html());
	
	if(all_image_count > 1){
		$('div.project_info div.image_holder a.on_image_next').css('display','block');
	}
	
	$('#gallery_prev_img, div.image_holder a.on_image_prev').click(function(){
		prevImage();		
	});
	$('#gallery_next_img, div.image_holder a.on_image_next').click(function(){
		nextImage();
	});
	
	goToImage(null);
	
};


function prevImage(){
	current_image_t =  $(current_image).prev('li');
	var new_img = $(current_image_t).html();
	
	if(new_img){
		if($(current_image_t).attr('name') != ''){
			$("a.anchors").removeClass('current');
			$("a.anchors[href='#"+$(current_image_t).attr('name')+"']").addClass('current');
		}
		
		current_image =  $(current_image_t);
		
		
		new_img_alt = $(current_image_t).attr('alt');
		$('#image_preview').attr('alt',new_img_alt);
		$('#image_preview').attr('title',new_img_alt);
		
		if($(current_image_t).attr('title') != ''){
			$('#top_project_client_name').html($(current_image_t).attr('title'));
		}
		if($(current_image_t).attr('href') != ''){
			$('a#image_holder').attr('href',$(current_image_t).attr('href'));
		}
		
		$('#image_preview').attr({src: new_img});
					
		count = Math.abs($('#gallery_current_img').html());
		if(count > 1){
			$('#gallery_current_img').html(count-1);
		}

		if( count - 2 < 1){
			$('div.project_info div.image_holder a.on_image_prev').css('display','none');
		}
		$('div.project_info div.image_holder a.on_image_next').css('display','block');
	}
	
	set_navBar();
}	

function nextImage(){
	var current_image_t = new Image();
	current_image_t =  $(current_image).next('li');
	var new_img = $(current_image_t).html();
	
	var hash=location.hash;
	if (hash){
		elm_name = hash.substring(1,hash.length);
	}
	
	if(new_img){
		if($(current_image_t).attr('name') != ''){
			$("a.anchors").removeClass('current');
			$("a.anchors[href='#"+$(current_image_t).attr('name')+"']").addClass('current');
		}
	
		current_image =  $(current_image_t);
		
		new_img_alt = $(current_image_t).attr('alt');
		$('#image_preview').attr('alt',new_img_alt);
		$('#image_preview').attr('title',new_img_alt);
		
		if($(current_image_t).attr('title') != ''){
			$('#top_project_client_name').html($(current_image_t).attr('title'));
		}
		
		if($(current_image_t).attr('href') != ''){
			$('a#image_holder').attr('href',$(current_image_t).attr('href'));
		}

		$('#image_preview').attr('src',new_img);
			
		count = Math.abs($('#gallery_current_img').html());
		$('#gallery_current_img').html(count+1);
		
		set_navBar();
		
		if(all_image_count > 1){
			if( count+2 > all_image_count){
				$('div.project_info div.image_holder a.on_image_next').css('display','none');
			}
			$('div.project_info div.image_holder a.on_image_prev').css('display','block');
		}		
		
		return true;
	}else{
		return false;
	}
}


function goToImage(elm_name){
	if(!elm_name){
		var hash=location.hash;
		if (hash){
			elm_name = hash.substring(1,hash.length);
		}
	}
	
	if(!elm_name){
		$("a.anchors:first").addClass('current');
		return;
	}else{
		$("a.anchors").removeClass('current');
		$("a.anchors[href='#"+elm_name+"']").addClass('current');
	}
	
	current_image_t =  $("#hidden_gallery_list li[name='"+elm_name+"']");
	var new_img = $(current_image_t).html();

	if(new_img){
		current_image =  $(current_image_t);
		
		new_img_alt = $(current_image_t).attr('alt');
		$('#image_preview').attr('alt',new_img_alt);
		$('#image_preview').attr('title',new_img_alt);
		
		if($(current_image_t).attr('title') != ''){
			$('#top_project_client_name').html($(current_image_t).attr('title'));
		}
		if($(current_image_t).attr('href') != ''){
			$('a#image_holder').attr('href',$(current_image_t).attr('href'));
		}
		
		$('#image_preview').attr('src',new_img);
		
		count = $(current_image).attr('value');
		
		$('#gallery_current_img').html(count);
	}
	
	set_navBar();
	
	return false;
}

var api_scrollable = '';
function animate_ClientList(){
	api_scrollable = $("div#right_block div#client_scrollable").scrollable({ 
		next: 'a.client_scrollable_next',
		prev: 'a.client_scrollable_prev',
		items: 'ol#client_list', 
		vertical:true,
		size: 23, 		
		speed: 200,
		easing: 'linear',
		clickable: false,
		api: true
	});	
	api_scrollable.move(api_scrollable_move);
	
	// $("div.scrollable").scrollable(); 
	$('a.client_scrollable_next').mouseover(function(){
	
		$(this).everyTime(100, 'next_client', function() {
			api_scrollable.next();
		});
		/*$(this).oneTime(100, function() {
			api_scrollable.scrollable().next();
		});*/
	});
	$('a.client_scrollable_next').mouseout(function(){
		$(this).stopTime('next_client');
	});
	
	$('a.client_scrollable_prev').mouseover(function(){
		$(this).everyTime(100, 'prev_client', function() {
			api_scrollable.prev();
		});
		/*$(this).oneTime(100, function() {
			api_scrollable.scrollable().prev()
		});*/
	});
	$('a.client_scrollable_prev').mouseout(function(){
		$(this).stopTime('prev_client');
	});
	
}


function formControls(){
	
	// onSubmit clear defaults values 
	$("button#submit_form").click(function(){
		// inputs
		$("input[default]").each(function(){
			if($(this).attr('default') == $(this).val())  
				$(this).val('');
		});
		//textarea
		$("textarea[default]").each(function(){
			if($(this).attr('default') == $(this).html())  
				$(this).html('');
		});
	});
	
	// input onClick clear default value 
	$("input[default]").click(function(){
		if($(this).attr('default') == $(this).val()) 
			$(this).val('');
	}).blur(function(){		// input onBlur return back default value 
		if($(this).val() == '') 
			$(this).val($(this).attr('default')); 
	});

	// textarea onClick clear default value 
	$("textarea[default]").click(function(){
		if($(this).attr('default') == $(this).html()) 
			$(this).html('');
	}).blur(function(){		// textarea onBlur return back default value 
		if($(this).html() == '') 
			$(this).html($(this).attr('default')); 
	});
}