/**
 *----------------------------------------------------------*-----------------*
 *     Web Project Manager                                  |   frontSystem   |
 *----------------------------------------------------------*-----------------*
 *     @category frontSystem
 *     @package javascript
 *     @version 0.1 [2008/11/29]
 *
 *     @author Tomáš Režňák <tomas.reznak@web-project-manager.com>
 *     @copyright Copyright © 2008, Tomáš Režňák
 *     @link http://www.web-project-manager.com/
 *----------------------------------------------------------------------------*
 *     definition of general javascript functions
 */
 
function check_contact() {
  if (element("message").value == "") {
    alert("Musíte vyplnit alespoň text zprávy.");
    element("message").focus();   
    return false;
  }
  return true;
}

function check_subscription() {
  if (element("subscription_email").value == "") {
    alert("Musíte vyplnit e-mailovou adresu, na kterou budou zasílány novinky.");
    element("subscription_email").focus();   
    return false;
  }
  return true;
}

$(function() {
    $("#slideshow_in").jCarouselLite({
        btnNext: ".next",
        btnPrev: ".prev",
        speed: 1000,
        scroll: 1,
        visible: 1,
        auto: 5000
    });
});


/*
 * slideshow functions
 */
 
// initital variables for slideshow
var cookie_slide_show_counter = getCookie("kometa_uvoz_image");
var slide_show_counter = (cookie_slide_show_counter ? cookie_slide_show_counter : 1);
var slide_show_running = false;
var slide_show_preload = new Array();

/**
 * preload slideshow images
 * note: call this function before calling slideShowRun()
 * 
 * @param array images    array of paths to images which should be displayed by slideshow
 * @return void
 */
function slideShowPreload(images) {
  for (i=0;i<images.length;i++) {
    slide_show_preload[i] = new Image();
    slide_show_preload[i].src = images[i];
  }
}

/**
 * run image slideshow
 * note: call slideShowPreload() before calling this function
 *       set theses variables:
 *         - slide_show_speed (in miliseconds)
 *         - slide_show_fade_duration (in seconds)
 *         
 * @return void
 */
function slideShowRun() {
	var img = document.getElementById("img_sponzor");
	var link = document.getElementById("sponzor_link_2");
	if (document.all) {
	  img.style.filter="blendTrans(duration=" + slide_show_fade_duration + ")";
    img.filters.blendTrans.Apply();
  }

  img.src = slide_show_preload[slide_show_counter].src;
  
	if (document.all){
	  img.filters.blendTrans.Play();
	}

  document.getElementById("sponzor_link_1").href = slide_webs[slide_show_counter];
  document.getElementById("sponzor_link_2").href = slide_webs[slide_show_counter];
  document.getElementById("sponzor_link_2").innerHTML = slide_captions[slide_show_counter];  
	
	setCookie("kometa_uvoz_image", slide_show_counter); 
  	
	slide_show_counter++;
	if (slide_show_counter > (slide_show_preload.length - 1)) {
	  slide_show_counter = 0;
	}
	
	var t = setTimeout('slideShowRun()', slide_show_speed);
}
 
/*
 * /slideshow functions
 */    


/*
 * general functions / START
 */

/**
 * display message dialog and return ok/cancel status according to user's decision
 *    
 * @param string msg    message to be displayed
 * @return boolean      true if message has been confirmed, false otherwise
 */
function dialogConfirm(msg) {
  var status = confirm(msg);
  
  return status;
}

/**
 * display message dialog. If confirmed, redirect to address stored in link's 'href' attribute
 *    
 * @param string msg    message to be displayed
 * @param object obj    link object to use href from
 * @return boolean      true if message has been confirmed, false otherwise
 */
function dialogConfirmRedirect(msg, obj) {
  var status = confirm(msg);
  if (status) {
    obj.href;
  }
  
  return status;
}

/*
 * general functions / END
 */

/*
 * HTML object manipulation functions / START
 */
 
/**
 * get HTML element defined by id
 *
 * @return obj    HTML element
 */
function element(id) {
  var obj = document.getElementById(id);
  
  return obj;
}
 
/**
 * hide HTML element
 *    
 * @param mixed obj    object or object's id
 * @return void
 */
function elementHide(obj) {
  if (element(obj)) {    // object identified by its id
    obj = element(obj);
  }
  
  obj.style.display = "none";
}

/**
 * show HTML element
 *    
 * @param mixed obj            object or object's id
 * @param boolean is_inline    [optional] if element is inline, pass true as second parameter
 * @return void
 */
function elementShow(obj, is_inline) {
  if (element(obj)) {    // object identified by its id
    obj = element(obj);
  }

  obj.style.display = (is_inline ? "inline" : "block");
}

/**
 * switch element class name - if element has className class_1, replace it with class_2 and vice versa
 *    
 * @param mixed obj         object or object's id
 * @param string class_1    name of class 1
 * @param string class_2    name of class 2
 * @return void
 */
function switchClass(obj, class_1, class_2) {
  if (element(obj)) {    // object identified by its id
    obj = element(obj);
  }
  
  if (obj.className == "") {    // class name empty -> set class name from class_1
    obj.className = class_1;
  } else {
    if (obj.className.indexOf(class_1) != -1) {    // class_1 exists -> replace with class_2
      obj.className = obj.className.replace(class_1, class_2);
    } else {
      if ((obj.className.indexOf(class_2) != -1) && (class_2 != "")) {    // class_2 exists -> replace with class_1
        obj.className = obj.className.replace(class_2, class_1);
      } else {    // if not found, add class_1 to existing
        obj.className = obj.className + " " + class_1;
      }
    }
  }
}

/*
 * HTML object manipulation functions / END
 */


/**
 * display user card
 *
 * @param integer id         user id  
 * @param object obj         reference object used for card positioning
 * @param string template    card template which generates card content
 * @param string script      URL leading to ajax script processing requests
 * @return void
 */
function cardShowUser(id, obj, template, script) {
  var width = 533;    // approximate card width (in pixels)
  var pos = elementPosition(obj);
  var left, top;
  if ((pos.x) > (width + 5)) {
    left = (pos.x - width);
  } else {
    left = pos.x;
  }
  top = pos.y;

  var ajax = new traxi();
  ajax.action_script = script;
  ajax.execute_response = true;
  ajax.set("action", "card");
  ajax.set("type", "user");
  ajax.set("id", id);
  ajax.set("card_template", template);
  ajax.set("left", left + "px");
  ajax.set("top", top + "px");
  ajax.run();	  
}

/**
 * hide card (typically when user clicks on 'close' link within the card)
 *    
 * @return void
 */
function cardClose() {
  var obj = element("card_div");
  if (obj) {
    obj.parentNode.removeChild(obj);
  }
}

/**
 * calculate element absolute position
 *    
 * @param object obj     reference object used for positioning
 * @param string type    [optional] type of coordinates to be returned
 *                         - 'bottomright' or not set: coordinates of bottom right corner
 *                         - 'topleft': coordinates of top left corner
 * @return object        element position (x, y)
 */
function elementPosition(obj, type) {
  var pos = {x:0, y:0};
	
  if (type == null) {
    type = "bottomright";
  }
	
  pos.x = obj.offsetLeft;
  pos.y = obj.offsetTop;
	
  if (type == "bottomright") {
    pos.x += obj.offsetWidth;
    pos.y += obj.offsetHeight;
  }

  while((obj = obj.offsetParent) != null) {
    pos.x += obj.offsetLeft;
    pos.y += obj.offsetTop;
  }
	
  return pos;
}

/*
 * card functions / END
 */


/* 
 * tab functions / START
 */
 
var active_tab = 1;

/**
 * hide tab
 *    
 * @param string tab_name    name of tab to hide
 * @return void
 */
function tabHide(tab_name) {
  var tab = element("tabname_" + tab_name);
  if (tab) {
    elementHide(tab);
  }
}

/**
 * show tab
 *    
 * @param string tab_name    name of tab to show
 * @return void
 */
function tabShow(tab_name) {
  var tab = element("tabname_" + tab_name);
  if (tab) {
    elementShow(tab, true);
  }
}

/**
 * change active tab to tab_name
 *    
 * @param string tab_name    name of tab which should be set as active
 * @return void
 */
function tabChange(tab_name) {
	elementHide("tab_" + active_tab);
	element("tabname_" + active_tab).className = "";
	
	elementShow("tab_" + tab_name);
	element("tabname_" + tab_name).className = "active";
	active_tab = tab_name;
	
	// try to put focus on the first field in active tab, when tab includes a form
	var child = element("tab_" + active_tab).childNodes[0];
  var type;
  var field;

	while (child) {
	  if (child.tagName == "FORM") {
	    for (i=0;i<child.elements.length;i++) {
	      field = child.elements[i];
	    
	      try {
          type = field.type;
        } catch (e) {}
	    
	      if (type && type != "hidden") {
		      if (formGetFieldParentTabName(field) == active_tab) {
		        if (type == "text" || type == "textarea") {
	  	        try {
	              field.focus();
	            } catch (e) {}
		        }
		            
		        return;
		      }
	      }
	    }
	  }
	  
	  try {
      child = child.childNodes[0];
    } catch (e) {}
	}
}

/*
 * tab functions / END
 */
 
/**
 * get cookie value
 *         
 * @param string name    name of cookie
 * @return mixed         value of cookie if cookie set, undefined otherwise
 */
function getCookie(name) {
  var t;
  var search = name + "=";
  
  if (document.cookie.length) {
    var start = document.cookie.indexOf(search);
    if (start != -1) {
      start += search.length;
      end = document.cookie.indexOf(";", start);
      
      if (end == -1) {
        end = document.cookie.length;
      }
      
      t = unescape(document.cookie.substring(start, end));
    }
  }
  
  return t;
}

/**
 * set cookie value
 *         
 * @param string name            name of cookie
 * @param string value           value of cookie
 * @param integer days_expire    [optional] number of days after which cookie will expire
 * @return void
 */
function setCookie(name, value, days_expire) {
	if (days_expire) {
		var expires = new Date();
		expires.setTime(expires.getTime() + 1000*60*60*24*days_expire);
	}
	
	document.cookie = name + "=" + escape(value) + (days_expire == null ? "" : (";expires=" + expires.toGMTString())) + ";path=/";
}

