// *************** all requests

var text_resID='';
var text_lID='';

// *************************

function showTextResponse(req) {
  
  // alert('showing Response');
  if ($(text_lID)) $(text_lID).style.display="none";
  
  // alert(req.responseText);
  if ($(text_resID) && req.responseText) $(text_resID).style.display="";
  $(text_resID).innerHTML = req.responseText;
 
}

function sendRequest(url, linkID, resultID, baseform) {
  
  text_resID=resultID;
  text_lID=linkID;
  
  if ($(text_resID)) $(text_resID).innerHTML = "<img src='/img/ajax_indicator.gif'><br><br>";
  
  var params="";
  if ($(baseform)) { 
    params+=Form.serialize($(baseform)); 
  } 
  
  // alert(params);

  new Ajax.Request(
         url,
         {
		   parameters : params,
           onComplete : showTextResponse
         });

}

// *************************

function get_prev_img_width (length) {

	  var pic_width=(978-(length*5)) / length;
	  if (pic_width>90) pic_width=90;
	  return Math.round(pic_width);
	  
}

function move_preview_frame(ind) {
	
	var modus='absolute';
	
	if ($('picpreview'))  {
	
	  var preview_pic=document.getElementById('pic_' + ind);
	  var outer_wrapper=document.getElementById('outer_wrapper');
		
	  document.getElementById('picframe').style.width=get_prev_img_width(bilder.length) + 'px';
	
	  var move_x=preview_pic.offsetLeft-outer_wrapper.offsetLeft-7;
	  var move_y=-60;

	  if (ind!=0) picframe.style.display='';
	  moveItem('picframe', move_x, move_y, modus);
   }	
}

  
function showImageResponse(req) {
  
  //alert(req.responseText);
  
  // check if there is a description
  var allarray=req.responseText.split(";;");
  var showdots="";
  
  // alert(allarray[0]);
  // we only come here once to preload and set the first image 
  var imgarray=allarray[0].split(";");
  
  if (allarray[1]) {
    // description found
    description=allarray[1].split(";");
  }
  
  imgarray.reverse();
	
  var thumbnails="";
  
  for (var i=0;i < imgarray.length;i++) {
	// alert(imgarray[i]);
    bilder[i] = new Image();
    bilder[i].src = imgarray[i];
	
	// make thumbnails for gallery
	if ($('picpreview')) {
      var pic_width=get_prev_img_width(imgarray.length);
	  thumbnails+="<a href='javascript:void()' onclick='showImage(" + i + ");return false;'><img src='" + bilder[i].src + "' border='0' name='pic_" + i + "' id='pic_" + i + "' height='40' width='" + pic_width + "' /></a><img src='/img/spacer.gif' width='5'>";
	}
	
	showdots+="<span id='dot_" + i + "'><a href='javascript:void()' onclick='showImage(" + i + ");return false;'><img src='/img/player/dot.gif' style='height:10px' border='0'></a></span>&nbsp;";

  }
  
  // check second image
  if (imgarray.length>1) {
	
	// display controls if prepared
	if ($('dot_container')) $('dot_container').innerHTML=showdots;
	
	// display thumbnails, gallery only
	if ($('picpreview')) { $('picpreview').innerHTML=thumbnails; }

    rotateImages();
    intervalid=setInterval('rotateImages();',5500);
	
  } 
}

function sendImageRequest(url, linkID, resultID) {
  
  var params="";
  new Ajax.Request(
         url,
         {
		   parameters : params,
           onComplete : showImageResponse
         });
    
}

var bilder=new Array();
var index=-1;
var stopslider=0;
var resID='';
var lastresID='';
var intervalid="";
var c_default="";
var description=new Array();
var jump_to_image=0;

// called from camps and gallery via load_artist
function initImageLoad(loc) 
{
  
  if (intervalid) clearInterval(intervalid);
  	 
  resID="imageplayer1";
  var geturl="/scripts/get_images.php?location=" + loc;
  sendImageRequest(geturl, '', 'imageplayer1');
}

function setLegende ( ind ) {
  // set legende
  if ($('gallery_legende')) { 
    if (description[index]) $('gallery_legende').innerHTML=description[ind];
	else $('gallery_legende').innerHTML="";
  }
}

function set_dots (lastIndex, index) {
	if ($('dot_' + lastIndex)) $('dot_' + lastIndex).innerHTML="<a href='javascript:void()' onclick='showImage(" + lastIndex + ");return false;'><img src='/img/player/dot.gif' style='height:10px' border='0'></a>";
	if ($('dot_' + index)) $('dot_' + index).innerHTML="<a href='javascript:void()' onclick='showImage(" + index + ");return false;'><img src='/img/player/dot_sel.gif' style='height:10px' border='0'></a>";
}

function rotateImages(direction)
{	
	lastresID=resID;
	var lastIndex=index;
	
    if (!direction) index=index+1-0;
	else {
	  index=index-1-0;
	  if (index<0) index=bilder.length-1;
	}
	
	index=index%bilder.length;
	
    if (resID=="imageplayer1") { resID="imageplayer2"; } else { resID="imageplayer1"; }

    // adjust size of imageplayer to size of image
	if ($('picpreview') && bilder[index].height) {
	  $('imageplayer').style.height =  bilder[index].height + 74 + 'px';
    }
	
	// do this before setting src, otherwise it is flickering
    $(resID).setStyle({opacity: 0});
	
	$(resID).src = bilder[index].src;
    $('loader').style.display='none';
	
	ParallelEffect(lastresID, resID);

    if ($('dot_container')) set_dots (lastIndex, index);
	// setLegende(index);
	
	// calculate position of preview frame
	if (!jump_to_image) move_preview_frame(index);
	else jump_to_image=0;

}

function ParallelEffect(lastresID, resID) {

      new Effect.Parallel(
         [
         new Effect.Opacity(lastresID,{ transition: Effect.Transitions.linear, from: 1, to: 0, sync:true, fps: 100 }),
         new Effect.Opacity(resID,{ transition: Effect.Transitions.linear, from: 0, to: 1, sync:true, fps: 100 })
         ],
         {duration: 3}
         );
}

// called from gallery
function loadArtist(lang) {
  artist=get_artist();
  initImageLoad('img/index/' + artist + '/');
}

function showImage(ind) {
  jump_to_image=1;
  set_dots (index, ind);
  move_preview_frame(ind);
  index=ind-1;
  setLegende(index);
  // if jump_to_image is set, preview pane will not be adjusted
  nextImage();
}
function nextImage() {
  if (intervalid) clearInterval(intervalid);
  intervalid='';
  rotateImages();
}
function previousImage() {
  if (intervalid) clearInterval(intervalid);
  intervalid='';
  rotateImages("-");
}
function stopPlayer() {
  if (intervalid) clearInterval(intervalid);
  intervalid='';
}
function startPlayer(id) {
  if (!id) { 
    rotateImages(); 
    intervalid=setInterval('rotateImages();',5500);
  }
}
function showPage(index) {
  if (intervalid) stopPlayer(intervalid);
}

function moveItem(id, x, y, modus)
{	
  var elem=document.getElementById(id);
  new Effect.Move(elem, { x: x, y: y, mode: modus });
}

 // *************************
 
function showNavMap (element, country, lang) {
		
 if ($('mapnav_top')) {
   if ($('mapnav_top').style.display='none') $('mapnav_top').style.display='';
   if (element.style.display='none') element.style.display='';
   roll_navig_map(country, 0, lang);
 }
}

function set_maps(location, lang) {
	
  country=location;
  
  // check first if country comes as parameter else take default
  if (!location) {
    var loc="" + window.location + "";
    var country=loc.split("location=");
    country=country[1];
    // if more than one parameter
    if (country && country.indexOf("&")!=-1) {
      country=country.split("&")[0];
    }
  }
  
  if (!country && c_default) country=c_default;	
  showNavMap($('mapnav'), country, lang);
  
}	

var country_array=new Array("botswana", "namibia", "zambia", "zimbabwe", "malawi", "seychelles", "southafrica", "mozambique", "tanzania");

function roll_navig_map (id, off, lang) {
			
  if (lang=='de') lang='';
		
  for (var i = 0; i < country_array.length; i++) {
    if (id==country_array[i]) {
		
	  if (off==1 && $(id)) { 
	    $(id).style.display='none';
		$('textnav').innerHTML = "&nbsp;";
	  }
	  else {
		if ($(id)) {
	      $(id).style.display='';
		  var imgcont="<img src='/img/maps/rollover/text_" + id;
		  if (lang) imgcont+="_" + lang;
		  imgcont+=".gif'>";
		  $('textnav').innerHTML = imgcont;
		} 
	  }
	} else {
	  if ($(country_array[i])) $(country_array[i]).style.display='none';
	}
  }
}

function get_artist() {
  var loc="" + window.location + "";
  var check=loc.split("art=");
  
  if (!check[1]) check[1]="";
  return check[1];
  
}
	
function get_tour() {

  var loc="" + window.location + "";
  var check=loc.split("tour=");
  return check[1];

}	

// ********************

function roll_camp(id, roll, is_small) {
  if (roll) $(id).src="/img/camp_roll.gif";
  else {
    if (is_small) $(id).src="/img/camp_s.gif";
    else $(id).src="/img/camp.gif";
  }	
}

function roll_trail(id, roll) {
  if (roll) $(id).src="/img/canoe_roll.gif";
  else $(id).src="/img/canoe.gif";
}


var tooltip=function(){
 var id = 'tt';
 var top = 3;
 var left = 3;
 var maxw = 300;
 var speed = 10;
 var timer = 20;
 var endalpha = 95;
 var alpha = 0;
 var tt,t,c,b,h;
 var ie = document.all ? true : false;
 return{
  show:function(v,w){
   if(tt == null){
    tt = document.createElement('div');
    tt.setAttribute('id',id);
    t = document.createElement('div');
    t.setAttribute('id',id + 'top');
    c = document.createElement('div');
    c.setAttribute('id',id + 'cont');
    b = document.createElement('div');
    b.setAttribute('id',id + 'bot');
    tt.appendChild(t);
    tt.appendChild(c);
    tt.appendChild(b);
    document.body.appendChild(tt);
    tt.style.opacity = 0;
    tt.style.filter = 'alpha(opacity=0)';
    document.onmousemove = this.pos;
   }
   tt.style.display = 'block';
   
   // set layer like for maps
   tt.style.zIndex = 20;
   
   c.innerHTML = v;
   tt.style.width = w ? w + 'px' : 'auto';
   if(!w && ie){
    t.style.display = 'none';
    b.style.display = 'none';
    tt.style.width = tt.offsetWidth;
    t.style.display = 'block';
    b.style.display = 'block';
   }
  if(tt.offsetWidth > maxw){tt.style.width = maxw + 'px'}
  h = parseInt(tt.offsetHeight) + top;
  clearInterval(tt.timer);
  tt.timer = setInterval(function(){tooltip.fade(1)},timer);
  },
  pos:function(e){
   var u = ie ? event.clientY + document.documentElement.scrollTop : e.pageY;
   var l = ie ? event.clientX + document.documentElement.scrollLeft : e.pageX;
   tt.style.top = (u - h) + 'px';
   tt.style.left = (l + left) + 'px';
  },
  fade:function(d){
   var a = alpha;
   if((a != endalpha && d == 1) || (a != 0 && d == -1)){
    var i = speed;
   if(endalpha - a < speed && d == 1){
    i = endalpha - a;
   }else if(alpha < speed && d == -1){
     i = a;
   }
   alpha = a + (i * d);
   tt.style.opacity = alpha * .01;
   tt.style.filter = 'alpha(opacity=' + alpha + ')';
  }else{
    clearInterval(tt.timer);
     if(d == -1){tt.style.display = 'none'}
  }
 },
 hide:function(){
  clearInterval(tt.timer);
   tt.timer = setInterval(function(){tooltip.fade(-1)},timer);
  }
 };
}();

// ************************3

function travelconfig (insert_ids) {
  for (var i=0;i < insert_ids.length; i++) {
    new Draggable(insert_ids[i]);
  }
}


// ******************

var nav_all=Array("nav_float_swissafrican", "nav_float_countries", "nav_float_journeys", "nav_float_camps", "nav_float_info");

function capture_navout (e, d) {
	
	if (!e) var e = window.event;
	var tg = (window.event) ? e.srcElement : e.target;
	if (tg.nodeName != 'DIV') return;
	var reltg = (e.relatedTarget) ? e.relatedTarget : e.toElement;
	while (reltg != tg && reltg.nodeName != 'BODY')
		reltg= reltg.parentNode
	if (reltg== tg) return;
	// Mouseout took place when mouse actually left layer
	// Handle event
	d.style.display='none';
}

function show_navigation(id) {
  var elem=document.getElementById(id);  
  elem.style.display='';
  
  // disable all others
  for (var i=0;i < nav_all.length;i++) {
    if (nav_all[i]!=id && document.getElementById(nav_all[i])) { 
	  document.getElementById(nav_all[i]).style.display='none';
	} 
  }
  
}

function maximizeWindow() { 
	window.moveTo(0, 0);
	window.resizeTo(screen.availWidth, screen.availHeight);
}

function show_price_info(id, text) {
	
	elem=document.getElementById(id);
	elem.innerHTML=text;
	elem.style.display='';
}

function toggle_element (name) {

  if (document.getElementById(name).style.display=='') document.getElementById(name).style.display='none';
  else document.getElementById(name).style.display='';

}

