//jQuery.noConflict();

var images = new Array();
var actimg = 0;
var newimg = 0;
var firsttext = true;
var loaded = false;

var imgdir = 'img/home/';
var interval = 8000;
var blend = 2000;
var delay_text = 1500;

$.fn.image = function(src, f){ 
  return this.each(function(){ 
    var i = new Image(); 
    i.src = src; 
    i.onload = f; 
    i.border = 0;
    this.appendChild(i);
  }); 
} 

$(document).ready(function() {

	$.ajax({
		type: "GET",
		url: "php-include/conf/images_home.xml",
		dataType: "xml",
		success: function(xml) {
			$(xml).find('image').each(function(){
				var src = $(this).find('src').text();
				var caption = $(this).find('caption').text();
				var pid = $(this).find('pid').text();
				var color = $(this).find('color').text();
				var pos = $(this).find('pos').text();
        
        var obj = new Object();
        obj['src'] = src;
        obj['caption'] = UTF8.decode(caption);
        obj['pid'] = pid;
        obj['color'] = color;
        obj['pos'] = pos;
        
        //images.push(src);
        images.push(obj);
        
			});
      
      showFirstImage();
      
		}
	});


  $("a.showthumb").mouseover(function() {
    var left = $(this).offset().left + 10;
    var top = $(this).offset().top +20;
    var href = $(this).attr('href');
    var pid = href.substr(href.lastIndexOf('=')+1);
    
    var thumbcontainer = $("#thumbcontainer");
    thumbcontainer.css('top', top+'px');
    thumbcontainer.css('left', left+'px');
    thumbcontainer.html('<img src="photo/thumb/'+pid+'.jpg" class="thumb" border="0" alt="" />');
    thumbcontainer.fadeIn(200);
  });

  $("a.showthumb").mouseout(function() {
    var thumbcontainer = $("#thumbcontainer");
    thumbcontainer.fadeOut(0);
  });  

});


function showThread(rid) {

  var dataString = 'action=messthread&rid=' + rid;

  $.ajax({
    type:     "POST",
    url:      "/ajax.php",
    data:     dataString,
    dataType: "html",
    success: function(output) {

      //alert($('#root'+rid).html());
      $('#root'+rid).after(output);
      $('#thread'+rid).slideDown("fast");
      $('#showthread'+rid).hide();
      $('#hidethread'+rid).show();
      
      
    },
    error: function(obj, msg1, msg2) {
      alert('error' + msg1 + ' ' + msg2);
    }
    
  });
  
  return false;

}

function hideThread(rid) {

  $('#thread'+rid).slideUp("fast", function() { $('#thread'+rid).remove(); });
  $('#showthread'+rid).show();
  $('#hidethread'+rid).hide();
      
  return false;

}


function randPano() {

  var dataString = 'action=randpano';

  $.ajax({
    type:     "POST",
    url:      "/ajax.php",
    data:     dataString,
    dataType: "html",
    success: function(output) {
      $('#randpano').fadeOut(200, function() { 
                                    $('#randpano').html(output); 
                                  });
      $('#randpano').fadeIn();
      
    },
    error: function(obj, msg1, msg2) {
      alert('error' + msg1 + ' ' + msg2);
    }
    
  });
  
  return false;

}



function showFirstImage() {
  
  if (images.length > 0) {
  
    // create text container
    for (var i=1; i<images.length; i++) {
      $("#indexboxphoto").append('<div id="text'+i+'" style="display:none; color:#'+images[i]['color']+'" class="container_cap cap_'+images[i]['pos']+'">'+images[i]['caption']+'</div>');
    }

    if (images.length > 1) {
      setTimeout(showNextImage, interval);
    }
      
    // load first image
    /*
    $("#indexboxphoto").append('<div id="cont0" style="display:none; position:absolute; z-index:10; top:0px; left:0px;"></div>');
    $("#cont0").image(imgdir+images[0]['src'],function() {
      $(this).parent().fadeIn(1000);
      setTimeout(showText, 1000);
      
      if (images.length > 1) {
        setTimeout(showNextImage, 5000);
      }
      
    });
    */
  
  }
}


function showNextImage() {

  newimg = actimg + 1;

  if ((newimg) >= images.length) {
    loaded = true;
    newimg = 0;
  }
  
  if (!loaded) {
    $("#indexboxphoto").append('<div id="cont'+newimg+'" style="display:none;" class="container_img"></div>');
    $("#cont"+newimg).image(imgdir+images[newimg]['src'],function() {
    
      if (images[newimg]['pid'] != '-1') {
        $("#cont"+newimg).wrap('<a href="panorama.php?pid='+images[newimg]['pid']+'"></a>');
      }
    
      $("#cont"+newimg).fadeIn(blend);
      $("#cont"+actimg).fadeOut(blend);
      $("#text"+actimg).fadeOut(blend);
      setTimeout(showText, delay_text);
      
      actimg=newimg;
      setTimeout(showNextImage, interval);

    });

  } else {

    $("#cont"+newimg).fadeIn(blend);
    $("#cont"+actimg).fadeOut(blend);
    $("#text"+actimg).fadeOut(blend);
    setTimeout(showText, delay_text);
  
    actimg=newimg;
    setTimeout(showNextImage, interval);

  }
  
}


function showText() {
  $("#text"+actimg).fadeIn(blend);
  
}


UTF8 = {
	encode: function(s){
		for(var c, i = -1, l = (s = s.split("")).length, o = String.fromCharCode; ++i < l;
			s[i] = (c = s[i].charCodeAt(0)) >= 127 ? o(0xc0 | (c >>> 6)) + o(0x80 | (c & 0x3f)) : s[i]
		);
		return s.join("");
	},
	decode: function(s){
		for(var a, b, i = -1, l = (s = s.split("")).length, o = String.fromCharCode, c = "charCodeAt"; ++i < l;
			((a = s[i][c](0)) & 0x80) &&
			(s[i] = (a & 0xfc) == 0xc0 && ((b = s[i + 1][c](0)) & 0xc0) == 0x80 ?
			o(((a & 0x03) << 6) + (b & 0x3f)) : o(128), s[++i] = "")
		);
		return s.join("");
	}
};

