function php_urlencode (str) {
  str = escape(str);
  return str.replace(/[*+\/@]|%20/g,

  function (s) {
    switch (s) {
      case "*": s = "%2A"; break;
      case "+": s = "%2B"; break;
      case "/": s = "%2F"; break;
      case "@": s = "%40"; break;
      case "%20": s = "+"; break;
    }
    return s;
  }
  );
}

// jquery based image fader for unido slider menu

function jqsliderfade($tag) {
    //console.log($tag);
    var $photo = $($tag);
    
    // set first fade image
    var $src = $('>img', $photo.parent().parent().find("a:first").parent()).attr('src');
    $('>div', $photo).find('img').attr('src', $src);
    $('>div', $photo).fadeIn(250);
        
    var $headers = $photo.parent().parent().find("li");
    $headers.each(function() {
        var $obj = $(this);
        var $img = $('>img', this);
        var $a = $('>a', this);

        // skip li if there is no a
        if ($obj.find('a').length == 0) {
            return true;
        }

	// set a href to img src
	if ($a.attr('href') == "") {
          // this will be an image
	  $a.attr('href', $img.attr('src'));
	  $a.attr('rel', 'pp1[g1]');
        } else {
          if (String($a.attr('href')).indexOf('fileadmin', 0) >= 0) {
            // assume this is a local video
	    var $clip = "http://www.unido.org/" + $a.attr('href');
	    //$a.attr('href', "http://www.unido.org/unido09/fileadmin/templates/UNIDO-09/scripts/flowplayer.swf?flashvars=config={'clip':'" + $clip + "'}");
	    //var $clip = $a.attr('href');
	    $a.attr('href', "/fileadmin/templates/UNIDO-09/scripts/flowplayer.swf?flashvars=config={'clip':'" + $clip + "'}");
	    //$a.attr('href', "/fileadmin/templates/UNIDO-09/scripts/flowplayer.swf?width=300&amp;height=200");
            //$a.attr('href', 'http://vimeo.com/moogaloop.swf?width=580&height=326&flashvars=clip_id=4321799&server=vimeo.com&show_title=1&show_byline=1&show_portrait=0&color=&fullscreen=1');
          }
	}

	// set a href title
	$a.attr('title', $a.html());

        // create hover hooks
        $a.hover(
            function(e) {
                var $div = $('>div', $photo);
                          
                if ($('>img', $div).attr('src') == $img.attr('src')) {
                    return true;
                }

                next_step = function(div) {
                $('>img', $div).attr('src', $img.attr('src'));
                $div.fadeIn(250);
                //$('>img', $div).css("z-index", "1");
                }

                // stop animation if in progress
                if ($div.is('animated')) {
                    $div.stop().fadeTo(250, 1, next_step);
                } else {
                    $div.fadeOut(250, next_step);
                }
                //$div.css("z-index", "1");
            },
            function(e) {
            }
        );
    }); // end headers.each
}

function jqsliderfadenews($tag) {
    //console.log($tag);
    
    var $photo = $($tag).find('#currentNewsPic');
   	//console.log($photo);
    
    //var $photo = $($tag);
    
    // set first fade image
    var $src = $('>img', $photo.parent().find("a:first").parent()).attr('src');
    $('>div', $photo).find('img').attr('src', $src);
    $test = $('>div', $photo).find('img').attr('src');
    //console.log($test);
    $('>div', $photo).fadeIn(250);
    
    //console.log($src);
    
    var $headers = $photo.parent().find("li");
    $headers.each(function() {
        var $obj = $(this);
        var $img = $('>img', this);
        var $a = $('>a', this);

        // skip li if there is no a
        if ($obj.find('a').length == 0) {
            return true;
        }

        // create hover hooks
        $a.hover(
            function(e) {
                var $div = $('>div', $photo);
                          
                if ($('>img', $div).attr('src') == $img.attr('src')) {
                    return true;
                }

                next_step = function(div) {
                $('>img', $div).attr('src', $img.attr('src'));
                $div.fadeIn(250);
                //$('>img', $div).css("z-index", "1");
                }

                // stop animation if in progress
                if ($div.is('animated')) {
                    $div.stop().fadeTo(250, 1, next_step);
                } else {
                    $div.fadeOut(250, next_step);
                }
                //$div.css("z-index", "1");
            },
            function(e) {
            }
        );
    }); // end headers.each
}


$(document).ready(function() {
  //jqsliderfade("#relatedCurrentPhoto");
  // iterate over all 'relatedCurrentPhoto' ids
  $("div[id='relatedCurrentPhoto']").each(function(){
    jqsliderfade(this);
  });
  jqsliderfadenews(".twoQuarterColBoxNews");
  jqsliderfadenews(".twoQuarterColBoxEvents");
  jqsliderfadenews(".twoQuarterColBoxFeatures");
  jqsliderfadenews(".quarterColBoxMultimedia");  

  // since the href is hacked load pretty photo now
  $("a[rel^='pp1']").prettyPhoto({
    theme: 'dark_square'
  });
}); // end document.ready

