$(document).ready(function() {

  positionDynamicBoxes();
	repositionDiv('#primary');

	$('#accordion').accordion({
		change: function(event, ui) {
			var clicked = $(this).children('.ui-state-active').attr('id');
			var target = $(this).children('div.ui-accordion-content-active');
			target.load('/dynamic_content?node_id=' + clicked, function() { Cufon.refresh() });
		},
		autoHeight: false,
		collapsible: true,
		active: false
	});
});



Cufon.replace('h1, h2, h3', {
	fontFamily: 'Humanist'
});


/**
 * projects
 *
 */

function renderProject(project_id) {
  closeDiv('#secondary');
  doAjaxRequest('CoMeT_function=run_module&module=ModProjects&function=render_project&project_id=' + project_id, renderProjectResponse);
}

function renderProjectResponse(html) {
	$('#primary').html(html).show().center(true);
  repositionDiv('#primary');
	Cufon.refresh();
}

var image_response_div;

function switchProjectImage(project_id, image_id) {
  closeDiv('#secondary');
  image_response_div = project_id;
  doAjaxRequest('CoMeT_function=run_module&module=ModProjects&function=switch_project_image&project_id=' + project_id + '&image_id=' + image_id, swithProjectImageResponse);
}

function swithProjectImageResponse(html) {
  $('#project_image_' + image_response_div).html(html);
}

var current_cat_id;
var current_toggle;

function listMoreProjects(cat_id, toggle) {
  current_cat_id = cat_id;
  current_toggle = toggle;
  doAjaxRequest('CoMeT_function=run_module&module=ModProjects&function=list_projects&action=list_more_projects&cat_id=' + cat_id, listMoreProjectsResponse);
}

function listMoreProjectsResponse(html) {
  $('#more_project_items_' + current_cat_id).html(html).slideDown();
  projectSwitcherLink(current_cat_id, current_toggle);
	Cufon.refresh();
}

function listLessProjects(cat_id, toggle) {
  $('#more_project_items_' + cat_id).slideUp();
  projectSwitcherLink(cat_id, 0);
}

var catId;
function projectSwitcherLink(cat_id, toggle) {
  catId = cat_id;
  doAjaxRequest('CoMeT_function=run_module&module=ModProjects&function=list_projects&action=toggle_projects&cat_id=' + cat_id + '&toggle=' + toggle, projectSwitcherLinkResponse);
}

function projectSwitcherLinkResponse(html) {
  $('#projects_toggle_' + catId).html(html);
}

function projectHeader(project_id) { // load project from page header
  renderProject(project_id);
}

function moveDivRenderedFromPath() {

	var sourceDiv = '#primary';

	var divWidth = $(sourceDiv).width();
  var divHeight = $(sourceDiv).height();
  var viewportWidth = $(window).width();
  var viewportHeight = $(window).height();

	//alert('divWidth:' + divWidth + ', divHeight:' + divHeight + ', viewportWidth:' + viewportWidth + ', viewportHeight: ' + viewportHeight)
	if(divHeight > viewportHeight) {
		yOffset = (divHeight - viewportHeight) / 2;
		$(sourceDiv).animate({
			"marginTop" : "+=" + (yOffset + 20) + "px"
		});
	}
}


/**
 * staff
 *
 */

function renderStaff(staff_id) {
  doAjaxRequest('CoMeT_function=run_module&module=ModStaff&function=render_staff&staff_id=' + staff_id, renderStaffResponse);
}

function renderStaffResponse(html) {
	$('#secondary').html(html).show().center(true);
  repositionDiv('#secondary');
	Cufon.refresh();
}

function renderProjectStaff(staff_id) {
  doAjaxRequest('CoMeT_function=run_module&module=ModStaff&function=render_staff&staff_id=' + staff_id, renderProjectStaffResponse);
}

function renderProjectStaffResponse(html) {
  $('#secondary').html(html).show().center(true);
  repositionDiv('#secondary');
}

function staffHeader(staff_id) { // load staff from page header
  renderStaff(staff_id);
}


/**
 * news
 *
 */


function renderNews(item_id) {
  doAjaxRequest('CoMeT_function=run_module&module=ModNews&function=render_news&action=render_news&item_id=' + item_id, renderNewsResponse);
}

function renderNewsResponse(html) {
  $('#primary').html(html).show().center(true);
  repositionDiv('#primary');
	Cufon.refresh();
}

var current_lang_id;
var current_cat_id;
var current_toggle;

function listMoreNews(lang_id, cat_id, toggle) {
  current_lang_id = lang_id;
  current_cat_id = cat_id;
  current_toggle = toggle;

  doAjaxRequest('CoMeT_function=run_module&module=ModNews&function=list_news&action=list_more_news&lang_id=' + lang_id + '&cat_id=' + cat_id, listMoreNewsResponse);
}

function listMoreNewsResponse(html) {
  $('#more_news_items_' + current_cat_id).html(html).slideDown();
  newsSwitcherLink(current_lang_id, current_cat_id, current_toggle);
	Cufon.refresh();
}

function listLessNews(lang_id, cat_id, toggle) {
  $('#more_news_items_' + cat_id).slideUp();
  newsSwitcherLink(lang_id, cat_id, 0);
}

var catId;
function newsSwitcherLink(lang_id, cat_id, toggle) {
  catId = cat_id;
  doAjaxRequest('CoMeT_function=run_module&module=ModNews&function=list_news&action=toggle_news&lang_id=' + lang_id + '&cat_id=' + cat_id + '&toggle=' + toggle, newsSwitcherLinkResponse);
}

function newsSwitcherLinkResponse(html) {
  $('#news_toggle_' + catId).html(html);
}


/**
 * search
 *
 */

function doSearch() {
  var searchstring = $("input#searchstring").val();
  doAjaxRequest('CoMeT_function=run_module&module=ModSearch&function=ajax_search&searchstring=' + searchstring, doSearchResponse);
}

function doSearchResponse(html) {
  closeDivs('#primary|#secondary');

  $('#search_result').html(html).show();

  $('#search_result_list a').click(function(){
    $('#search_result').hide();
  });

}

function checkEnter(e) {
  var key;
  if (window.event) {
    key = window.event.keyCode; //IE
  } else {
    key = e.which;
  }
  if (key == 13) {
    doSearch();
    return false;
  }
  return true;
}


/**
 * common
 *
 */

function positionDynamicBoxes() {
  $('#primary, #secondary').show().center(true);
}


function closeDiv(target_div) {
  $(target_div).fadeOut('normal');
}

function closeDivs(target_divs) {

  var divs = target_divs.split('|');
  var arLen=divs.length;

  for ( var i=0, len=arLen; i<len; ++i ) {
    $(divs[i]).fadeOut('fast');
  }
}


var currentAcc = '';
function accordionCloseDivs(source) {
	if(source.id != currentAcc) {
		currentAcc = source.id;
		closeDivs('#primary|#secondary');
	}
}

function repositionDiv(sourceDiv) {

  var divWidth = $(sourceDiv).width();
  var divHeight = $(sourceDiv).height();
  var viewportWidth = $(window).width();
  var viewportHeight = $(window).height();

  if(divHeight > viewportHeight) {
    yOffset = (divHeight - viewportHeight) / 2;
    $(sourceDiv).animate({
      "marginTop" : "+=" + (yOffset + 20) + "px"
    });
  }
}


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