jQuery(document).ready(function() {

  jQuery(".start_object h1 a").each(function() {
    var h1Link = jQuery(this).attr("href");
		//alert(h1Link);
    jQuery(this).parent().parent().addClass('start_object_with_link').bind('click', function(){window.location.href=h1Link});
  });

	jQuery('.quicktip').quicktip({
    speed:300
	});

	Cufon.replace('h1', {
		fontFamily: 'Myriad Pro',
		hover: true
	});

	jQuery("#site-nav ul ul ul").each( function() {
    var item   = jQuery(this);
		var parent_ul = item.parent().parent();
		item.css("margin-left", parent_ul.width());
  });

});



/**
 * staff
 *
 */

var staffId = '';

function renderStaff(staff_id) {
  staffId = staff_id;
  doAjaxRequest("CoMeT_function=run_module&module=ModStaff&function=render_staff&action=render_staff_ajax&staff_id=" + staff_id, renderStaffResponse);
}

function renderStaffResponse(html) {

  jQuery('#staff').children('div').removeClass('current');
  if (jQuery('#staff_' + staffId))
    jQuery('#staff_' + staffId).addClass('current');

  jQuery('#staff_item').html(html);
  Cufon.refresh('h1');
}

function renderStaffFromPath(staff_id) {
  renderStaff(staff_id);
}

/**
 * locations
 *
 */

var locationId;

function renderLocation(location_id) {
  locationId = location_id;
  doAjaxRequest("CoMeT_function=run_module&module=ModLocations&function=mod_locations&action=render_location&location_id=" + location_id, renderLocationResponse);
}

function closeLocation(location_id) {
  locationId = location_id;
  doAjaxRequest("CoMeT_function=run_module&module=ModLocations&function=mod_locations&action=close_location&location_id=" + location_id, renderLocationResponse);
}

function renderLocationResponse(html) {
  jQuery('#location_' + locationId).html(html);
}

/**
 * news
 *
 */
function renderNews(item_id) {
  doAjaxRequest("CoMeT_function=run_module&module=ModNews&function=render_news_ajax&item_id=" + item_id, renderNewsResponse);
}

function renderNewsResponse(html) {
  jQuery('#news_item').html(html);
  Cufon.refresh('h1');
}


function switchNewsYear(year, cat_id) {

  updateNewsItem(year, cat_id);
  updateNewsList(year, cat_id);

  jQuery('#news_year_menu').children('a').removeClass('current');
  if (jQuery('#year_' + year)) {
    jQuery('#year_' + year).addClass('current');
  }
}

function updateNewsItem(year, cat_id) {
  doAjaxRequest("CoMeT_function=run_module&module=ModNews&function=update_news_item&year=" + year + "&cat_id=" + cat_id, renderNewsResponse);
}

function updateNewsList(year, cat_id) {
  doAjaxRequest("CoMeT_function=run_module&module=ModNews&function=switch_news_year&year=" + year + "&cat_id=" + cat_id, updateNewsListResponse);
}

function updateNewsListResponse(html) {
  jQuery('#news_list_items').html(html);
  //jQuery('.addthis_toolbox').hide();
}

function renderNewsFromPath(news_id) {
  if(news_id) {
    renderNews(news_id);
  }
}


/**
 * common
 *
 */

function validEmail(elementValue) {
	var emailPattern = /^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;
	return emailPattern.test(elementValue);
}

function doAjaxRequest(pars, callback) {
  jQuery.ajax({
    type: "POST",
    url: "?",
    data: pars,
    success: callback
  });
}

function limitTextAreaChars(maxChars) {
  if(!maxChars) { maxChars = 200; }
  jQuery(".limit_chars").keyup(function(){
    var currChars = jQuery(this).val().length;
    if(maxChars - currChars <= 0) {
      var thisString = jQuery(this).val().substring(0, maxChars);
      jQuery(this).val(thisString);
      currChars = maxChars;
    }
    //jQuery(this).prev().children('span').html(maxChars - currChars + ' characters left');
		jQuery(this).next('span').html(maxChars - currChars + ' characters left');
  });
}
