
/**
* hoverIntent r5 // 2007.03.27 // jQuery 1.1.2+
* <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@cherne.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 p=(e.type=="mouseover"?e.fromElement:e.toElement)||e.relatedTarget;while(p&&p!=this){try{p=p.parentNode;}catch(e){p=this;}}if(p==this){return false;}var ev=jQuery.extend({},e);var ob=this;if(ob.hoverIntent_t){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);}if(e.type=="mouseover"){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.mouseover(handleHover).mouseout(handleHover);};})(jQuery);


/*
 * All code copyright 2009 Sonassi
 * www.sonassi.com
 *
 * Apart4u.co.uk custom left JS/CSS compat. nav
 */

jQuery.noConflict();

function shrinkMenu(obj) {
	// When the menu is hidden, change the active state
	obj.removeClass("nav-active");
	obj.css("background","url(/images/menu_plus-minus.gif) -218px 6px no-repeat");	
}

function showMenu(obj) {
	obj.addClass("nav-active");
	obj.css("background","url(/images/menu_plus-minus.gif) 4px 6px no-repeat");	
}

function str_replace (search, replace, subject, count) {
    var i = 0, j = 0, temp = '', repl = '', sl = 0, fl = 0,
            f = [].concat(search),
            r = [].concat(replace),
            s = subject,
            ra = r instanceof Array, sa = s instanceof Array;
    s = [].concat(s);
    if (count) {
        this.window[count] = 0;
    }
 
    for (i=0, sl=s.length; i < sl; i++) {
        if (s[i] === '') {
            continue;
        }
        for (j=0, fl=f.length; j < fl; j++) {
            temp = s[i]+'';
            repl = ra ? (r[j] !== undefined ? r[j] : '') : r[0];
            s[i] = (temp).split(f[j]).join(repl);
            if (count && s[i] !== temp) {
                this.window[count] += (temp.length-s[i].length)/f[j].length;}
        }
    }
    return sa ? s : s[0];
}

jQuery(document).ready(function(){
	
	// Find the active category and restore the menu
	if (jQuery("body").hasClass("catalog-category-view")) {
		var active_cat = jQuery("body").attr("class").split(' ').slice(-1);
		showMenu(jQuery(".nav-"+active_cat).parents(".has-children"));
		
		jQuery(".nav-"+active_cat).parents(".has-children").children("ul").slideToggle(0);
		jQuery(".nav-"+active_cat).addClass("selected-cat");
	}
		
	// Top nav
	// Override the CSS menu for people who have JS enabled.
	jQuery(".all-categories").removeClass("all-categories_no_js");	
	jQuery(".all-categories-dropdown").css({
		'left': 'auto',
		'display' : 'none'
	});
	
	var menuconfig = {    
	     sensitivity: 3, // number = sensitivity threshold (must be 1 or higher)    
	     interval: 100, // number = milliseconds for onMouseOver polling interval    
	     over: function() { jQuery(".all-categories-dropdown").slideDown(400); }, 
	     timeout: 900,     
	     out: function() { jQuery(".all-categories-dropdown").slideUp(300);	}
	};
	
	jQuery(".all-categories").hoverIntent(menuconfig);
	
	// Side nav
	// Override the CSS menu for people who have JS enabled.
	jQuery(".side_nav").removeClass("side_nav_nojs");	
	jQuery("li.has-children").css("background","url(/images/menu_plus-minus.gif) -218px 6px no-repeat");	
	
	jQuery(".side_nav .level0 a").click(function() {
		window.location = jQuery(this).attr("href");
		return false;
		exit();
	});
	
	jQuery(".side_nav li").click(function() {
		
		// Allow non-expanding links to click through
		if (!jQuery(this).hasClass("has-children")) {
			
			window.location = jQuery(this).children("a").attr("href");
			return false;
			exit();
		} else {
			
			// If another element is in view, hide the current active element		
			if (!jQuery(this).hasClass("nav-active") && jQuery(this).hasClass("level0")) {
				jQuery(".nav-active").children("ul").toggle(100);
				shrinkMenu(jQuery(".nav-active"));
			}
			
			jQuery(this).children("ul").slideToggle(200, function() {
				if(jQuery(this).css("display") == "none") {
					shrinkMenu(jQuery(this).parent());
				} else {			
					showMenu(jQuery(this).parent());
				}
			});
			
			// Return false on link click for top level nav
			return false;

		}		

	})
	
	// Expand the menu when a root category is selected
	if (jQuery(".selected-cat").length > 0) {
		jQuery(".selected-cat").children("ul").slideToggle(0);
		showMenu(jQuery(".selected-cat"));
	}	
	
	// For Blog section
	if (jQuery("#page").length > 0) {
		if (jQuery("#page").hasClass(".single")) {
			jQuery(".entry").after(jQuery("div.sociable"));
			jQuery("div.entry").find("a").find("img").parent().addClass("minimise-image").after("<div class='zoomit'>Click image to zoom</div>");
			
		}
	}
	
});
