ips.system.urlroot = window.location.host.indexOf('idium.n') !== -1 ? window.location.pathname.substring(0,window.location.pathname.indexOf('/',2)) + "/" : "/";

$(function() {
  $("li.login").load(ips.system.urlroot + "?template=ajaxlogin");

   var code = []; // Array for code pushes.
   $(document).keyup(function(event){
    if (code[1]) { // third esc will activate directedit
      var loc = window.location + "";
      if (loc.indexOf('edit') !== -1) {
         window.location = loc + ";nocache=true;";
         return;
      }
      if (loc.indexOf('?') !== -1) {
         window.location = loc + ";template=edit";
      } else {
         window.location = loc + "?template=edit;";
         return;
      }
    } else if (event.keyCode === 27) {
        code.push(event.keyCode);
    } 
  });

  //GALLERY
  var $gallery = $('#gallery'); //cache
  if ($gallery.get()[0]) {
    var gallery = $gallery.galleriffic('#thumbs-adv', {
        delay:                  4000,
        numThumbs:              $(".noFolders").get()[0] ? 8 : 6,
        preloadAhead:           4,
        enableTopPager:         true,
        enableBottomPager:      true,
        imageContainerSel:      '#slideshow-adv',
        controlsContainerSel:   '#controls-adv',
        captionContainerSel:    '#caption-adv',
        loadingContainerSel:    '#loading-adv',
        renderSSControls:       true,
        renderNavControls:      true,
        playLinkText:           ips.strings.slideshow,
        pauseLinkText:          ips.strings.pause,
        prevLinkText:           ips.strings.previous,
        nextLinkText:           ips.strings.next,
        nextPageLinkText:       ips.strings.next,
        prevPageLinkText:       ips.strings.previous,
        enableHistory:          true,
        autoStart:              false,
        onChange:               function(prevIndex, nextIndex) {
				    $('#thumbs-adv ul.thumbs').children()
				    .eq(prevIndex).fadeTo('slow', 0.5).end()
				    .eq(nextIndex).fadeTo('slow', 1.0);
                                //$(".nav-controls").hide();
                                //$("#caption-adv").show();
                                },
	onTransitionOut:        undefined/*function() {
			        $('#slideshow-adv').fadeOut('slow');
				}*/,
	onTransitionIn:         function() {       
                                //$(".nav-controls").hide();        
                                //$("#caption-adv").show();                       
	                        $('#slideshow-adv').fadeIn('slow');
				},
	onPageTransitionOut:    function(callback) {
	                        $('#thumbs-adv ul.thumbs').fadeOut('fast', callback);
				},
	onPageTransitionIn:     function() {
	                        $('#thumbs-adv ul.thumbs').fadeIn('fast');
				}
    });
    $("#galleryWrapper").hover(function(){
         $(".nav-controls,.play,#caption-adv").show();
    }, function(){
         $(".nav-controls,.play,#caption-adv").hide();
    });
    } // END IF GALLERY IS ON PAGE

    // EVENT DELEGATION - Not touching dom until click
    $(".externalLink").live("click", function(){
      $(this).attr("target", "_blank");
    });
    
   var $newsletterForm = $("#newsletterForm"), //CACHE
       $input = $newsletterForm.find(".email"),
       $label = $newsletterForm.find("label");
   if ($input.val() !== "") {$label.hide()}
   $newsletterForm.submit(function(){
   if ($input.val() === "") {return false}
   });
   $input.focus(function(){$label.hide()}).blur(function(){
     if ($(this).val() === "") {$label.show()}
   });
   $newsletterForm.find("label").click(function(){
    $input.focus();
   });
    // 
    
});
