﻿/* ********** Global vars and prototype functions ********** */
var _dayNames = new Array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
var _dayLongNames = new Array("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday");
var _monthNames = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
var _monthLongNames = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
if (!Array.prototype._indexOf)
{
    Array.prototype._indexOf = function(elt)
    {
        var len = this.length;

        var from = Number(arguments[1]) || 0;
        from = (from < 0) ? Math.ceil(from) : Math.floor(from);
        if (from < 0)
            from += len;

        for (; from < len; from++)
        {
            if (from in this && this[from] === elt)
            return from;
        }
        return -1;
    };
}
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}
String.prototype.RGBtoHex = function()
{
    var toHex = function(N)
    {
         if (N==null) 
            return "00";
         N=parseInt(N); 
         if (N==0 || isNaN(N)) 
            return "00";
         N=Math.max(0,N); 
         N=Math.min(N,255); 
         N=Math.round(N);
         return "0123456789ABCDEF".charAt((N-N%16)/16)
              + "0123456789ABCDEF".charAt(N%16);
    }
    
    rgb = this;
    if (rgb.indexOf("#") > -1)
    {
        return rgb.substring(1);
    }
    else
    {
        rgb = this.replace("rgb","").replace("(","").replace(")","").split(",");
        return toHex(rgb[0])+toHex(rgb[1])+toHex(rgb[2]);
    }
}

/* ---------- /Global vars and prototype functions ---------- */

function PageLoaded()
{    
    // Reset .net form action
    document.forms[1].action = window.location.href;    
    
    if (jQuery.browser.version == 6 && jQuery.browser.msie)
    {
        alert("Your browser is Microsoft Internet Explorer 6.\n\nWe highly recommend udating to the newer version (8) or installing the more secure web browser,\n Firefox - www.getfirefox.com");
    }
    
    InitImageRotator();
    
    InitCleverInputs();
    
	ImageReplaceText();	
	
	//Facebox
	jQuery(document).ready(function($) {
      $('a[rel*=facebox]').facebox()
    }) 
	
	/*jQuery("#site-top .links > ul > li").each(function(){
	    jQuery(this)
	        .mouseenter(function(){
	            jQuery(this).stop(true,false).children('div').hide().slideDown(100)
	        })
	        .mouseleave(function(){
	            jQuery(this).children('div').hide().stop(false,true);
	        })
	});*/
	$("#site-top .links > ul > li").each(function(){
	    jQuery(this).hoverIntent({
	        sensitivity: 1,     // number = sensitivity threshold (must be 1 or higher)
	        interval: 50,       // number = milliseconds for onMouseOver polling interval
	        over: function(){   // function = onMouseOver callback (required)
	            jQuery(this).children('div').slideDown(100);
	        },     
            timeout: 400,       // number = milliseconds delay before onMouseOut
            out: function(){    // function = onMouseOut callback (required)
                jQuery(this).children('div').slideUp(100);
            }
	    });
	});
	
	// Side basket toggle
	jQuery('#my-basket h2').toggle(function(){
	    jQuery(this).next().stop(true,false).slideDown(100);
	}, function(){
	    jQuery(this).next().slideUp(100);
	});
	
}

function InitImageRotator()
{
    images = jQuery('.image-rotator img');

    i = 0;
    jQuery('.image-rotator img').each(function(){
        jQuery(this).css("z-index", images.length-i);
        i++;
    });
    jQuery('.image-rotator .overlay').css("z-index", images.length+1);

    setTimeout(function(){ jQuery('.image-rotator .overlay').fadeOut(600); }, 1000);

    if (images.length > 1)
    {
        setInterval(function(){
            i = 0;
            img = jQuery('.image-rotator img:eq(0)').fadeOut(600, function(){
                jQuery(this)
                        .nextAll().each(function(){ jQuery(this).css({"z-index":images.length-(i++)}) })
                        .end().css({"z-index":images.length-(i++)}).show()
                        .parent().append(jQuery(this));
            });
        }, 5000);
    }
}

function InitCleverInputs()
{
	jQuery('.clever-input').each(function(){
		$this = jQuery(this);
		$this.data("text", { value : $this.attr("title") });
		$this
			.bind('click', function(){
				$this = jQuery(this);
				if ($this.val() == $this.data('text').value)
				{
					$this.val('')
				}
			})
			.blur(function(){
				$this = jQuery(this);
				if ($this.val() == '')
				{
					$this.val($this.data('text').value);
				}
			});
	});
}

function ImageReplaceText()
{
    // Top links
	jQuery("#site-top .links > ul > li > a").each(function(){
	    $this = jQuery(this);
	    imageinfo = 'text=' + escape($this.text()).toUpperCase() + '&size=11&bgcolour=' + $this.parents('#site-top').css("background-color").RGBtoHex();
	    if ($this.hasClass("active"))
	        imageinfo += '&colour=' + $this.css("color").RGBtoHex();
	    $this.html('<img src="/resources/image.php?'+imageinfo+'" alt="' + $this.attr("title") + '" title="' + $this.attr("title") + '" />')
	});
	
	// Footer copyright and links
	jQuery(".copyright, .links a", "#site-footer").each(function(){
        $this = jQuery(this);
        imageinfo = 'text=' + escape($this.text()).toUpperCase() + '&size=11&colour=' + $this.css("color").RGBtoHex() + '&bgcolour=' + $this.parents('#site-footer').css("background-color").RGBtoHex();
        $this.html('<img src="/resources/image.php?'+imageinfo+'" alt="' + $this.text() + '" title="' + $this.text() + '" />')
    })
}

jQuery.fn.itemPaging = function () {

    function repeat(str, num) {
        return new Array( num + 1 ).join( str );
    }
  
    return this.each(function () {
        var $wrapper = jQuery(this).css('overflow', 'hidden'),
            $slider = $wrapper.find('> ul'),
            $items = $slider.find('> li'),
            $single = $items.filter(':first'),
                        
            singleWidth = $single.outerWidth(true), 
            visible = Math.ceil($wrapper.innerWidth() / singleWidth), // note: doesn't include padding or border
            currentPage = 1,
            pages = Math.ceil($items.length / visible);   
        
        $slider.width($items.length*singleWidth+"px");         
        
        $items.find('img').hover(function(){
            jQuery(this).css("opacity","0.8");
        }, function(){
            jQuery(this).css("opacity","1");
        });


        // 1. Pad so that 'visible' number will always be seen, otherwise create empty items
        if (($items.length % visible) != 0) {
            $slider.append(repeat('<li class="empty" />', visible - ($items.length % visible)));
            $items = $slider.find('> li');
        }       
        
        if (pages > 1)
        {
            $wrapper.before('<ul class="'+$wrapper.attr("class")+'-paging"><li>page <span class="pagenum">1</span> of <span class="pages">1</span></li>');
            $wrapper.prev().find('.pages').text(pages);
            
            for (var i = 1; i <= pages; i++)
            {
                itemclass = "page";
                if (i == 1) {
                     itemclass += " active";
                }
                $wrapper.prev().append('<li class="'+itemclass+'">' + i + '</li>');
                $wrapper.prev().find('li:eq('+(i)+')').bind('click', { pageNum: i }, function(e){
                    jQuery(this).addClass("active").siblings('.active').removeClass("active");
                    $wrapper.prev().find('.pagenum').text(e.data.pageNum);
                    gotoPage(e.data.pageNum);
                });
            }
        }
        
        // 4. paging function
        function gotoPage(page) {
            var dir = page < currentPage ? -1 : 1,
                n = Math.abs(currentPage - page),
                left = singleWidth * dir * visible * n;
            
            $wrapper.filter(':not(:animated)').animate({
                scrollLeft : '+=' + left
            }, 500, function () {
                if (page == 0) {
                    $wrapper.scrollLeft(singleWidth * visible * pages);
                    page = pages;
                } else if (page > pages) {
                    $wrapper.scrollLeft(singleWidth * visible);
                    // reset back to start position
                    page = 1;
                } 

                currentPage = page;
            });                
            
            return false;
        }
        
        //$wrapper.after('<a class="arrow back">&lt;</a><a class="arrow forward">&gt;</a>');
        
        // 5. Bind to the forward and back buttons
        $('a.back', this).click(function () {
            return gotoPage(currentPage - 1);                
        });
        
        $('a.forward', this).click(function () {
            return gotoPage(currentPage + 1);
        });
        
        // create a public interface to move to a specific page
        $(this).bind('goto', function (event, page) {
            gotoPage(page);
        });
    });  
};

/**
* 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);