// javascript for homepage

// ---------------------------------------
// define global array for storing values from contact form
var contactValues = new Array();

//---------------------------------------
// erases the field and saves the hint to global variable if it is first-time-click 
function eraseInput(id) {
  var input = document.getElementById(id);
  var val = input.value;
  
  if (contactValues[id] === undefined || contactValues[id] == val) {
	  contactValues[id] = val;
	  input.value='';
  }  
  
}

//---------------------------------------
// field left empty and we have the hint, we put it back
function fillInput(id) {
	var input = document.getElementById(id);
	var val = input.value;
	if (val == '' && contactValues[id] != '') input.value = contactValues[id];
}


//---------------------------------------
// the function for switching cases
function switchCase(cfg) {
	jQuery('#page_home div.case_wrap_text strong.title').hide().html(case_switch_cfg[cfg]['title']).fadeIn();
	jQuery('#page_home div.case_wrap_text div.description').hide().html(case_switch_cfg[cfg]['description_hp']).fadeIn();
	jQuery('#page_home div.case_wrap a.read_more').hide().attr('href',case_switch_cfg[cfg]['url']).fadeIn();
	jQuery('#page_home div.case_image_wrap img').hide().attr('src',case_switch_cfg[cfg]['img_src']).fadeIn();
}


//---------------------------------------
// preload images in list
function getImagesList() {		
	var images = [];
	for (i in case_switch_cfg) {
		images.push(case_switch_cfg[i]['img_src']);
	}
	// remove the first image- its already loaded
	images.shift();
	return images;
}


// ---------------------------------------
// image auto rotate
function autoRotate() {
	var active_li = jQuery('#page_home div.case_switcher ul li a.active').parent();
	var next_li = jQuery(active_li).next();
	if (next_li.size() == 0) {
		next_li = jQuery(active_li).siblings(':first');
	}
	var next_a = jQuery(next_li).children('a');
	
	caseChange(next_a);
}	

//---------------------------------------
// main function for changing case
// obj is the <a> for switching case
function caseChange(obj) {
	jQuery('#page_home div.case_switcher ul li a').removeClass('active');
	jQuery(obj).addClass('active');
	// calling the switchCase function to change image and texts
	var index = jQuery(obj).parent().index();
	switchCase(index);
}



//==========================================
// run this scripts on DOM load
jQuery(document).ready(function() {
	// activate image controls
	jQuery('#page_home div.case_switcher ul li a:first').addClass('active');
	jQuery('#page_home div.case_switcher ul li a').click(function() {
		caseChange(this);
		clearInterval(interval);
		// start new autorotate
		/*interval = setInterval(autoRotate, 5000);*/		
	});
	
	// preload images
	var images = getImagesList();
	jQuery.preload(images);	
	
	// start images autorotate
	interval = setInterval(autoRotate, 4000);
	
});
