var $j = jQuery.noConflict();

$j(document).ready(function() {
      ////THUMBNAILS GALLERY////
      //Define ajax Request
      function photoAjaxRequest($photo) {
	 var $urlVars = getUrlVars();
	 $j.get('includes/photoGenerator.php',{subcurrent:$urlVars['subcurrent'],subsubcurrent:$photo,lang:$urlVars['lang'],ajaxRequest:1},function(data) {
	 $j(".photo").html(data);
      });
      }

      //On click in a link inside the gallery, do an ajax request and create a deep link
      $j(".thumbnailsGallery a").click(function() {
	 var $photo=$j(this).children('img').attr('alt');
	 photoAjaxRequest($photo);
	 $j.address.path($photo);
	 prevNextRefreshAjaxRequest();
	 return false;
	 });

      //If the URL is a deep link, process it
      if ($j.address.path().slice(1).length) {
	 $photo=$j.address.path().slice(1);
	 photoAjaxRequest($photo);
	 prevNextRefreshAjaxRequest();
      }      

      ////PREV & NEXT
      $j('.next').click(function() {
	    var vars = [], hash;
	    var hashes = $j(this).attr('href').slice(window.location.href.indexOf('?') + 1).split('&');
	    for(var i = 0; i < hashes.length; i++) {
	    hash = hashes[i].split('=');
	    vars.push(hash[0]);
	    vars[hash[0]] = hash[1];
	    }
	    $j.address.path(vars['subsubcurrent']);
	    photoAjaxRequest(vars['subsubcurrent']);
	    prevNextRefreshAjaxRequest();
	    return false;
	    });

      $j('.prev').click(function() {
	    var vars = [], hash;
	    var hashes = $j(this).attr('href').slice(window.location.href.indexOf('?') + 1).split('&');
	    for(var i = 0; i < hashes.length; i++) {
	    hash = hashes[i].split('=');
	    vars.push(hash[0]);
	    vars[hash[0]] = hash[1];
	    }
	    $j.address.path(vars['subsubcurrent']);
	    photoAjaxRequest(vars['subsubcurrent']);
	    prevNextRefreshAjaxRequest();
	    return false;
	    });

      function prevNextRefreshAjaxRequest() {
	 $urlVars = getUrlVars();
	 if ($j.address.path().slice(1).length) {
	    $subsubcurrent=$j.address.path().slice(1);
	 } else {
	    $subsubcurrent=$urlVars['subsubcurrent'];
	 }
	 $j.get('includes/photoNextPrev.php',{subcurrent:$urlVars['subcurrent'],subsubcurrent:$subsubcurrent,lang:$urlVars['lang'],ajaxRequest:'next'},function(data) {
	       if (data) {
	       $j('.next').show().attr('href',"index.php?lang="+$urlVars['lang']+"&current=stories&subcurrent="+$urlVars['subcurrent']+"&subsubcurrent="+data);
	       } else {
	       $j('.next').hide();
	       }
	       });
	 $j.get('includes/photoNextPrev.php',{subcurrent:$urlVars['subcurrent'],subsubcurrent:$subsubcurrent,lang:$urlVars['lang'],ajaxRequest:'prev'},function(data) {
	       if (data) {
	       $j('.prev').show().attr('href',"index.php?lang="+$urlVars['lang']+"&current=stories&subcurrent="+$urlVars['subcurrent']+"&subsubcurrent="+data);
	       } else {
	       $j('.prev').hide();
	       }
	       });
      };

      //Show while ajax request
      /*$j(".photo").ajaxStart(function() {
	    $j(this).html("<img src='/img/ajax_loader.gif' width='31' height='31' alt='' style='display:block;margin:150px auto 0 auto;' /><p style='text-align:center; margin-top:1em;'>Loading...");
	    $j('.photo').addClass('loading');
	    });*/

      //When ajax request stop
      /*$j(".photo").ajaxStop(function() {
	    $j('.photo').removeClass('loading');
	    });*/

      //SCROLLPANE
      $j('.thumbnailsGallery').jScrollPane({
	 'scrollbarMargin':0,
	 'scrollbarOnLeft':true,
	 });

      ////FUNCTIONS
      //Function to read a page's GET URL variables and return them as an associative array.
      function getUrlVars() {
	 var vars = [], hash;
	 var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
	 for(var i = 0; i < hashes.length; i++) {
	    hash = hashes[i].split('=');
	    vars.push(hash[0]);
	    vars[hash[0]] = hash[1];
	 }
	 return vars;
      }
});

