/* # RwGet # */
RwGet = {
    pathto: function(path, file) {
        var rtrim = function(str, list) {
            var charlist = !list ? 's\xA0': (list + '').replace(/([\[\]\(\)\.\?\/\*\{\}\+\$\^\:])/g, '$1');
            var re = new RegExp('[' + charlist + ']+$', 'g');
            return (str + '').replace(re, '');
        };
        var jspathto = rtrim(RwSet.pathto, "javascript.js");
        if ((path !== undefined) && (file !== undefined)) {
            jspathto = jspathto + path + file;
        } else if (path !== undefined) {
            jspathto = jspathto + path;
        }
        return jspathto;
    },
    baseurl: function(path, file) {
        var jsbaseurl = RwSet.baseurl;
        if ((path !== undefined) && (file !== undefined)) {
            jsbaseurl = jsbaseurl + path + file;
        } else if (path !== undefined) {
            jsbaseurl = jsbaseurl + path;
        }
        return jsbaseurl;
    }
};

var $nimblehost = jQuery.noConflict();

$nimblehost(document).ready(function(){
	nimblehostMenu();
	//contentFeatureLinks();
	//selectContentFeatures();
});

function nimblehostMenu() {
 	var nhMenuConfig = {
		sensitivity: 3, // number = sensitivity threshold (must be 1 or higher)    
		interval: 30, // number = milliseconds for onMouseOver polling interval    
		over: revealChildren, // function = onMouseOver callback (REQUIRED)    
		timeout: 300, // number = milliseconds delay before onMouseOut    
		out: hideChildren
		};
	
	function revealChildren(){
		$nimblehost(this).find("ul:first").css({marginTop: "15px", display: "block"});
	}
	
	function hideChildren(){
		$nimblehost(this).find("ul:first").fadeOut(300);
	}
 	
 	$nimblehost("<li class='plugins'>Plugins</li>").insertBefore($nimblehost("#plugins").parent().parent());
 	$nimblehost("<li class='stacks'>Stacks</li>").insertBefore($nimblehost("#stacks").parent().parent());
 	$nimblehost("<li class='dthemes'>Desktop Themes</li>").insertBefore($nimblehost("#dthemes").parent().parent());
 	$nimblehost("<li class='mthemes'>Mobile Themes</li>").insertBefore($nimblehost("#mthemes").parent().parent());
 	
 	
 	$nimblehost("<div class='menuPointer'></div>").prependTo($nimblehost("#mainMenu ul ul"));
 	$nimblehost("#mainMenu ul ul").css({display: "none"}); //Opera fix
	$nimblehost("#mainMenu ul li").hoverIntent(nhMenuConfig); 
}

function contentFeatureLinks() {
	if($nimblehost('#iconContentFeatureLinks').is(':visible')){
		$nimblehost('#iconContentFeatureLinks script').remove();
		$nimblehost('#iconContentFeatureLinks').appendTo('#featureLinksContainer');
	}
}

function selectContentFeatures() {
	$nimblehost('#contentFeatures').cycle({
		fx: 'scrollHorz',
		speed: 300,
		timeout: 0,
		pager: '.linksToContentFeatures',
		pagerAnchorBuilder: function(idx, slide) {
			return '.linksToContentFeatures li:eq(' + idx + ') a';
		}
	});
	$nimblehost('#positionMarker li.first').click(function() { 
	    $nimblehost('.linksToContentFeatures li:eq(0) a').trigger('click'); 
	    return false; 
	});
	$nimblehost('#positionMarker li.second').click(function() { 
	    $nimblehost('.linksToContentFeatures li:eq(1) a').trigger('click'); 
	    return false; 
	});
	$nimblehost('#positionMarker li.third').click(function() { 
	    $nimblehost('.linksToContentFeatures li:eq(2) a').trigger('click'); 
	    return false; 
	});
	$nimblehost('#positionMarker li.fourth').click(function() { 
	    $nimblehost('.linksToContentFeatures li:eq(3) a').trigger('click'); 
	    return false; 
	});
	$nimblehost('#positionMarker li.fifth').click(function() { 
	    $nimblehost('.linksToContentFeatures li:eq(4) a').trigger('click'); 
	    return false; 
	});
}

/**
* hoverIntent r6 // 2011.02.26 // jQuery 1.5.1+
* <http://cherne.net/brian/resources/jquery.hoverIntent.html>
* 
* @param  f  onMouseOver function || An object with configuration options
* @param  g  onMouseOut function  || Nothing (use configuration options object)
* @author    Brian Cherne brian(at)cherne(dot)net
*/
(function($){$.fn.hoverIntent=function(f,g){var cfg={sensitivity:7,interval:100,timeout:0};cfg=$.extend(cfg,g?{over:f,out:g}:f);var cX,cY,pX,pY;var track=function(ev){cX=ev.pageX;cY=ev.pageY};var compare=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);if((Math.abs(pX-cX)+Math.abs(pY-cY))<cfg.sensitivity){$(ob).unbind("mousemove",track);ob.hoverIntent_s=1;return cfg.over.apply(ob,[ev])}else{pX=cX;pY=cY;ob.hoverIntent_t=setTimeout(function(){compare(ev,ob)},cfg.interval)}};var delay=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);ob.hoverIntent_s=0;return cfg.out.apply(ob,[ev])};var handleHover=function(e){var ev=jQuery.extend({},e);var ob=this;if(ob.hoverIntent_t){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t)}if(e.type=="mouseenter"){pX=ev.pageX;pY=ev.pageY;$(ob).bind("mousemove",track);if(ob.hoverIntent_s!=1){ob.hoverIntent_t=setTimeout(function(){compare(ev,ob)},cfg.interval)}}else{$(ob).unbind("mousemove",track);if(ob.hoverIntent_s==1){ob.hoverIntent_t=setTimeout(function(){delay(ev,ob)},cfg.timeout)}}};return this.bind('mouseenter',handleHover).bind('mouseleave',handleHover)}})(jQuery);

/* Based on the script by Cabel Sasser and ported to jQuery by Steve Smith of OrderedList.com. Rewritten for use with RapidWeaver Themes by Jonathan Head
 * of NimbleHost.com - http://themes.nimblehost.com
 */

var $fz = jQuery.noConflict(); 
jQuery.fn.fancyZoom = function(options){
  
  var options   = options || {};
  var directory = options && options.directory ? options.directory : RwGet.pathto("images");
  var zooming   = false;

  if ($fz('#zoom').length == 0) {
    var ext = $fz.browser.msie ? 'gif' : 'png';
    var html = '<div id="zoom" style="display:none;position:relative;z-index:1000;font-size:1.2em;"> \
                  <table id="zoom_table" style="border-collapse:collapse; width:100%; height:100%;"> \
                    <tbody> \
                      <tr> \
                        <td class="tl" style="background:url(' + directory + '/tl.' + ext + ') 0 0 no-repeat; width:20px height:20px; overflow:hidden;" /> \
                        <td class="tm" style="background:url(' + directory + '/tm.' + ext + ') 0 0 repeat-x; height:20px; overflow:hidden;" /> \
                        <td class="tr" style="background:url(' + directory + '/tr.' + ext + ') 100% 0 no-repeat; width:20px height:20px; overflow:hidden;" /> \
                      </tr> \
                      <tr> \
                        <td class="ml" style="background:url(' + directory + '/ml.' + ext + ') 0 0 repeat-y; width:20px; overflow:hidden;" /> \
                        <td class="mm" style="background:#fff; vertical-align:top; padding:10px;"> \
                          <div id="zoom_content"> \
                          </div> \
                        </td> \
                        <td class="mr" style="background:url(' + directory + '/mr.' + ext + ') 100% 0 repeat-y;  width:20px; overflow:hidden;" /> \
                      </tr> \
                      <tr> \
                        <td class="bl" style="background:url(' + directory + '/bl.' + ext + ') 0 100% no-repeat; width:20px height:20px; overflow:hidden;" /> \
                        <td class="bm" style="background:url(' + directory + '/bm.' + ext + ') 0 100% repeat-x; height:20px; overflow:hidden;" /> \
                        <td class="br" style="background:url(' + directory + '/br.' + ext + ') 100% 100% no-repeat; width:20px height:20px; overflow:hidden;" /> \
                      </tr> \
                    </tbody> \
                  </table> \
                  <a href="#" title="Close" id="zoom_close" style="position:absolute; top:0; left:0;"> \
                    <img src="' + directory + '/closebox.' + ext + '" alt="Close" style="border:none; margin:0; padding:0;" /> \
                  </a> \
                </div>';
                
    $fz('body').append(html);
    
    $fz('html').click(function(e){if($fz(e.target).parents('#zoom:visible').length == 0) hide();});
    $fz(document).keyup(function(event){
        if (event.keyCode == 27 && $fz('#zoom:visible').length > 0) hide();
    });
    
    $fz('#zoom_close').click(hide);
  }
  
  var zoom          = $fz('#zoom');
  var zoom_table    = $fz('#zoom_table');
  var zoom_close    = $fz('#zoom_close');
  var zoom_content  = $fz('#zoom_content');
  var middle_row    = $fz('td.ml,td.mm,td.mr');
  
  this.each(function(i) {
    $fz($fz(this).attr('href')).hide();
    $fz(this).click(show);
  });
  
  return this;
  
  function show(e) {
    if (zooming) return false;
		zooming         = true;
		var content_div = $fz($fz(this).attr('href'));
  	var zoom_width  = options.width;
		var zoom_height = options.height;
		
		var width       = window.innerWidth || (window.document.documentElement.clientWidth || window.document.body.clientWidth);
  	var height      = window.innerHeight || (window.document.documentElement.clientHeight || window.document.body.clientHeight);
  	var x           = window.pageXOffset || (window.document.documentElement.scrollLeft || window.document.body.scrollLeft);
  	var y           = window.pageYOffset || (window.document.documentElement.scrollTop || window.document.body.scrollTop);
  	var window_size = {'width':width, 'height':height, 'x':x, 'y':y}
	
		var width              = (zoom_width || content_div.width()) + 60;
		var height             = (zoom_height || content_div.height()) + 60;
		var d                  = window_size;
		
		// ensure that newTop is at least 0 so it doesn't hide close button
		var newTop             = Math.max((d.height/2) - (height/2) + y, 0);
		var newLeft            = (d.width/2) - (width/2);
		var curTop             = e.pageY;
		var curLeft            = e.pageX;
		
		zoom_close.attr('curTop', curTop);
		zoom_close.attr('curLeft', curLeft);
		zoom_close.attr('scaleImg', options.scaleImg ? 'true' : 'false');
		
    $fz('#zoom').hide().css({
			position	: 'absolute',
			top				: curTop + 'px',
			left			: curLeft + 'px',
			width     : '1px',
			height    : '1px'
		});
    
    fixBackgroundsForIE();
    zoom_close.hide();
    
    if (options.closeOnClick) {
      $fz('#zoom').click(hide);
    }
    
		if (options.scaleImg) {
  		zoom_content.html(content_div.html());
  		$fz('#zoom_content img').css('width', '100%');
		} else {
		  zoom_content.html('');
		}
    
    $fz('#zoom').animate({
      top     : newTop + 'px',
      left    : newLeft + 'px',
      opacity : "show",
      width   : width,
      height  : height
    }, 500, null, function() {
      if (options.scaleImg != true) {
    		zoom_content.html(content_div.html());
  		}
			unfixBackgroundsForIE();
			zoom_close.show();
			zooming = false;
    })
    return false;
  }
  
  function hide() {
    if (zooming) return false;
		zooming         = true;
	  $fz('#zoom').unbind('click');
		fixBackgroundsForIE();
		if (zoom_close.attr('scaleImg') != 'true') {
  		zoom_content.html('');
		}
		zoom_close.hide();
		$fz('#zoom').animate({
      top     : zoom_close.attr('curTop') + 'px',
      left    : zoom_close.attr('curLeft') + 'px',
      opacity : "hide",
      width   : '1px',
      height  : '1px'
    }, 500, null, function() {
      if (zoom_close.attr('scaleImg') == 'true') {
    		zoom_content.html('');
  		}
      unfixBackgroundsForIE();
			zooming = false;
    });
    return false;
  }
  
  function switchBackgroundImagesTo(to) {
    $fz('#zoom_table td').each(function(i) {
      var bg = $fz(this).css('background-image').replace(/\.(png|gif|none)\"\)$fz/, '.' + to + '")');
      $fz(this).css('background-image', bg);
    });
    var close_img = zoom_close.children('img');
    var new_img = close_img.attr('src').replace(/\.(png|gif|none)$fz/, '.' + to);
    close_img.attr('src', new_img);
  }
  
  function fixBackgroundsForIE() {
    if ($fz.browser.msie && parseFloat($fz.browser.version) >= 7) {
      switchBackgroundImagesTo('gif'); 
    }
	}
  
  function unfixBackgroundsForIE() {
    if ($fz.browser.msie && $fz.browser.version >= 7) {
      switchBackgroundImagesTo('png'); 
    }
	}
}

$nimblehost(document).ready(function() {
	if(((navigator.userAgent.match(/iPhone/i))||(navigator.userAgent.match(/iPod/i))||(navigator.userAgent.match(/Android/))||(navigator.userAgent.match(/webOS/))) && ($nimblehost('#mobileMenuTab').is(':visible'))){
		$nimblehost("a.zoom").fancyZoom({width:250});
	}
	else { $nimblehost("a.zoom").fancyZoom(); }
});
