$(document).ready(function() {	

	// locate site in the middle of the page
	var screen_width = $(document).width();
	var screen_left_margin = 10;
	if (screen_width>960){
		screen_left_margin = parseInt((screen_width-960)/2);
	}	
	$('.ec-1').css('margin-left', screen_left_margin);
	// locate logo in the right position
	$('.ec-logo').css({left: screen_left_margin-8});
	
	// load the first proj items	
	var init_load_mod = $('.ec-1-1-menu-1[@init_load=1]').attr('mod');
	var init_load_act = $('.ec-1-1-menu-1[@init_load=1]').attr('act');	
	$.post(base_domain+'modules/',
		{act: 'aboutus' },
		function(data){
			load_project_list_action();
	  	}
	);			

	
	var load_client_data = function() {
		$('.ec-bg-img[@photo=1]').show();
		$('.ec-bg-num-1[@num=1]').css('background', '#EB4900');
		$('.ec-bg-num-1').click(function(){
			$('.ec-bg-num-1').css('background', '#676566');
			$(this).css('background', '#EB4900');
			var num = $(this).attr('num');
			$('.ec-bg-img').hide();
			$('.ec-bg-img[@photo='+num+']').show();
		});
		
		$('.ec-sm-img, ec-sm-img-left').hover(function(){
			$('img', $(this)).css('width', '67px');
			$('img', $(this)).css('height', '67px');			
			$('img', $(this)).css('border', '3px solid #ff6600');						
		}, function(){
			if (!$(this).attr('active')){
				$('img', $(this)).css('width', '73px');
				$('img', $(this)).css('height', '73px');			
				$('img', $(this)).css('border', '0');			
			}
		});
	};
	
	var load_project_list_action = function() {			
		var img_bg, tmp, bg_arr = Array(), bg_arr_length=0;
		tmp = $('.ec-1').attr('bg_imgs');
		bg_arr = tmp.split(',')
		bg_arr_length = bg_arr.length;
		var img_num = (Math.random()*10);
		img_num = parseInt(img_num);
		img_num = img_num%bg_arr_length;
		img_bg = bg_arr[img_num];
		$('.ec-ab-us').css('background', 'url(/images/dynamic/'+img_bg+') no-repeat');
		if ($('.ec-sm-img').size()>0){			
			var first_proj = $('.ec-sm-img[@index=1]');
			$(first_proj).attr('active', 'on');
			$('img', first_proj).css('width', '67px');
			$('img', first_proj).css('height', '67px');			
			$('img', first_proj).css('border', '3px solid #ff6600');			
			$.post(base_domain+'show_big_img/',
				{menu_item:$(first_proj).attr('menu_item'), client: $(first_proj).attr('client')},
				function(data){							
					load_client_data();
			  	}
			);			
		}
		$('.ec-sm-img').click(function(){
			$('.ec-sm-img').attr('active', '');
			$('.ec-sm-img img').css('width', '73px');
			$('.ec-sm-img img').css('height', '73px');					
			$('.ec-sm-img img').css('border', '0');
			$('img', this).css('width', '67px');
			$('img', this).css('height', '67px');			
			$('img', this).css('border', '3px solid #ff6600');			
			$(this).attr('active', 'on');
			$.post(base_domain+'show_big_img/',
				{menu_item:$(this).attr('menu_item'), client: $(this).attr('client')},
				function(data){							
					load_client_data();
			  	}
			);						
		});
		$('div[click=on]').click(function(){
			$.post(base_domain+'show_big_img/',
				{menu_item:$(this).attr('menu_item'), client: $(this).attr('client')},
				function(data){							
					load_client_data();
			  	}
			);						
		});
	};	

	// click & hover on menu item
	$('.ec-1-1-menu-1-2').click(function(){	
		if($(this).parent().attr('mod')=='refresh-proj'){
			$('.ec-1-1-proj-lst').css('background', '#F8F8F8');
			$('.ec-1-1-proj-lst').css('height', '550px');
		}
		else{
			$('.ec-1-1-proj-lst').css('background', 'url(/images/static/default_bg.gif) no-repeat');
			$('.ec-1-1-proj-lst').css('height', '563px');
		}
		if ($(this).attr('open')==-1)	
			return;
		$('.ec-1-1-menu-1-2[@open=1]').attr('open', '0');
		$(this).attr('open', 1);
		$('.ec-1-1-menu-1-2[@open=0]').css('color', '#3a3636');
		$(this).css('color', '#eb4900');		
		var page = $(this).parents().attr('mod');
		if (page!='refresh-proj'){
			$('#project-list').html('');
			$('#cur-client-data').html('');			
		}
		if (page!=undefined){
			
			$.post(base_domain+page+'/',				
				{act:$(this).parents().attr('act') },
				function(data){
					load_project_list_action();

			  	}
			);			
		}
	});
	
	$('.ec-1-1-menu-1-2').hover(function(){	
		if ($(this).attr('open')==0){
			$(this).css('color', '#eb4900');
		}
	}, function(){
		if ($(this).attr('open')==0){
			var lbl_color = ($(this).attr('level')==2 ? '#5E5A5B' : '#3a3636');
			$(this).css('color', lbl_color);								
		}
	});

	$('.ec-1-1-menu-1-2[@open=1]').css('color', '#eb4900');		
	
	$('.ec-1-2-2-client-1').hover(function(){
		if ($('img',this).attr('enable_hover')==1){
			var img_h = $('img',this).attr('hover_img');
			$('img',this).attr('src', img_h);
		}
	}, function(){
		if ($('img',this).attr('enable_hover')==1){
			var img_b = $('img',this).attr('base_img');
			$('img',this).attr('src', img_b);
		}	
	});
});
