﻿// JScript File

function fixpadding(pic){
	if (getHeight(pic) < getWidth(pic)){
		pic.parentNode.parentNode.style.paddingTop = '25' + 'px';
		pic.parentNode.parentNode.style.height = '55' + 'px';
        }
}    

var windowInnerSize = {
	width : function(){
	  if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		return window.innerWidth + window.pageXOffset;
	  } else if( document.documentElement && document.documentElement.clientWidth ) {
		//IE 6+
		return document.documentElement.clientWidth;
	  }	
	  return 0;	
	},
	height : function(){
	  if( typeof( window.innerHeight ) == 'number' ) {
		//Non-IE
		return window.innerHeight + window.pageYOffset;
	  } else if( document.documentElement && document.documentElement.clientHeight) {
		//IE 6+
		return document.documentElement.clientHeight + document.documentElement.scrollTop;
	  }
	  return 0;
	}
}
function toggleSlideDown(o,e) {
	if($(o).style.display == 'none'){
	$(e).firstChild.data = 'Hide';
	new Effect.SlideDown(o, {duration:.1, fps:40, queue: {position:'end', scope:'toggleSlider'}});
	//Fat.fade_element(o, 60, 1000, '#FFDF65', '#FFFFFF');
	} else {
	$(e).firstChild.data = 'View';
	new Effect.SlideUp(o, {duration:.1, fps:40, queue: {position:'end', scope:'toggleSlider'}});
	}
}

/***********************************
position and size helpers
**********************************/	
function getWidth(o) {return Element.getDimensions(o).width;}
function getHeight(o) {return Element.getDimensions(o).height;}
function getLeftOffset(o) {return Position.cumulativeOffset(o)[0];}
function getTopOffset(o) {return Position.cumulativeOffset(o)[1];}

/***********************************
    mouse position helpers
**********************************/	

function mouseX(e) {return Event.pointerX(e);}
function mouseY(e) {return Event.pointerY(e);}

/***********************************
  Random background color generator
**********************************/	

  function genColour(id) {  
    var obj = $(id);
    var rnd_r = String(Math.floor(Math.random()*255));
    var rnd_g = String(Math.floor(Math.random()*255));
    var rnd_b = String(Math.floor(Math.random()*255));
	var rnd_rgb = 'rgb(' + rnd_r + ', ' + rnd_g + ', ' +  + rnd_b + ')' ;
    setColour(obj, rnd_rgb);
}
function setColour(obj,col) {
    Element.setStyle(obj, {background: col });
}


/***********************************

   The Fade Anything Technique

**********************************/	

// @name      The Fade Anything Technique
// @namespace http://www.axentric.com/aside/fat/
// @version   1.0-RC1
// @author    Adam Michela

var Fat = {
	make_hex : function (r,g,b) 
	{
		r = r.toString(16); if (r.length == 1) r = '0' + r;
		g = g.toString(16); if (g.length == 1) g = '0' + g;
		b = b.toString(16); if (b.length == 1) b = '0' + b;
		return "#" + r + g + b;
	},
	fade_all : function ()
	{
		var a = document.getElementsByTagName("*");
		for (var i = 0; i < a.length; i++) 
		{
			var o = a[i];
			var r = /fade-?(\w{3,6})?/.exec(o.className);
			if (r)
			{
				if (!r[1]) r[1] = "";
				if (o.id) Fat.fade_element(o.id,null,null,"#"+r[1]);
			}
		}
	},
	fade_element : function (id, fps, duration, from, to) 
	{
		if (!fps) fps = 30;
		if (!duration) duration = 3000;
		if (!from || from=="#") from = "#FFFF33";
		if (!to) to = this.get_bgcolor(id);
		
		var frames = Math.round(fps * (duration / 1000));
		var interval = duration / frames;
		var delay = interval;
		var frame = 0;
		
		if (from.length < 7) from += from.substr(1,3);
		if (to.length < 7) to += to.substr(1,3);
		
		var rf = parseInt(from.substr(1,2),16);
		var gf = parseInt(from.substr(3,2),16);
		var bf = parseInt(from.substr(5,2),16);
		var rt = parseInt(to.substr(1,2),16);
		var gt = parseInt(to.substr(3,2),16);
		var bt = parseInt(to.substr(5,2),16);
		
		var r,g,b,h;
		while (frame < frames)
		{
			r = Math.floor(rf * ((frames-frame)/frames) + rt * (frame/frames));
			g = Math.floor(gf * ((frames-frame)/frames) + gt * (frame/frames));
			b = Math.floor(bf * ((frames-frame)/frames) + bt * (frame/frames));
			h = this.make_hex(r,g,b);
		
			setTimeout("Fat.set_bgcolor('"+id+"','"+h+"')", delay);

			frame++;
			delay = interval * frame; 
		}
		setTimeout("Fat.set_bgcolor('"+id+"','"+to+"')", delay);
	},
	set_bgcolor : function (id, c)
	{
		var o = document.getElementById(id);
		o.style.backgroundColor = c;
	},
	get_bgcolor : function (id)
	{
		var o = document.getElementById(id);
		while(o)
		{
			var c;
			if (window.getComputedStyle) c = window.getComputedStyle(o,null).getPropertyValue("background-color");
			if (o.currentStyle) c = o.currentStyle.backgroundColor;
			if ((c != "" && c != "transparent") || o.tagName == "BODY") { break; }
			o = o.parentNode;
		}
		if (c == undefined || c == "" || c == "transparent") c = "#FFFFFF";
		var rgb = c.match(/rgb\s*\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*\)/);
		if (rgb) c = this.make_hex(parseInt(rgb[1]),parseInt(rgb[2]),parseInt(rgb[3]));
		return c;
	}
}

window.onload = function () 
	{
	Fat.fade_all();
	}
	
/***********************************

 window Popup helper

**********************************/	
	
var Popup = {
  open: function(options)
  {
    this.options = {
      url: '#',
      width: 600,
      height: 500,
      name:"_blank",
      location:"no",
      menubar:"no",
      toolbar:"no",
      status:"yes",
      scrollbars:"yes",
      resizable:"yes",
      left:"",
      top:"",
      normal:false
    }
    Object.extend(this.options, options || {});

    if (this.options.normal){
        this.options.menubar = "yes";
        this.options.status = "yes";
        this.options.toolbar = "yes";
        this.options.location = "yes";
    }

    this.options.width = this.options.width < screen.availWidth?this.options.width:screen.availWidth;
    this.options.height=this.options.height < screen.availHeight?this.options.height:screen.availHeight;
    var openoptions = 'width='+this.options.width+',height='+this.options.height+',location='+this.options.location+',menubar='+this.options.menubar+',toolbar='+this.options.toolbar+',scrollbars='+this.options.scrollbars+',resizable='+this.options.resizable+',status='+this.options.status
    if (this.options.top!="")openoptions+=",top="+this.options.top;
    if (this.options.left!="")openoptions+=",left="+this.options.left;
    window.open(this.options.url, this.options.name,openoptions );
    return false;
  }
}

/***********************************

 createElement wrapper

**********************************/	

function $E(data) {
    var el;
    if ('string'==typeof data) {
        el=document.createTextNode(data);
    } else {
        el=document.createElement(data.tag);
        delete(data.tag);

        if ('undefined'!=typeof data.children) {
            if ('string'==typeof data.children ||
                'undefined'==typeof data.children.length
            ) {
                el.appendChild($E(data.children));
            } else {
                for (var i=0, child=null; 'undefined'!=typeof (child=data.children[i]); i++) {
                    el.appendChild($E(child));
                }
            }
            delete(data.children);
        }
        for (attr in data) {
            el[attr]=data[attr];
        }
    }
    return el;
}


var cardInfoHover = {   
    fetch : function(el, key){
        Event.observe(el, 'mouseout', cardInfoHover.hide, false);    
        clearTimeout(window.showtimeout);   
        window.showtimeout = window.setTimeout(function(){CardPopupService.GetCardPopupNew($(el).id, key, cardInfoHover.show);},500);
    },

    show : function(result){
        var srcEl =  $(result.source); //element from which hover was invoked
        var newNode=$E({
            tag:'div', 
            id: 'visiblepopup',
            className: 'popupcontainer'            
        })
        Element.hide(newNode);      
        if ($('visiblepopup')){$('visiblepopup').parentNode.removeChild($('visiblepopup'));}
        newNode.innerHTML = result.contents;
        document.getElementsByTagName("body").item(0).appendChild(newNode);
        
        var visiblePopup = $('visiblepopup');        
        Event.observe(srcEl, 'mouseout', cardInfoHover.hide, false);       
        Event.observe(visiblePopup, 'mouseout', cardInfoHover.hide, false);
        Event.observe(visiblePopup, 'mousemove', function(){if(window.showtimeout){window.clearTimeout(window.showtimeout)};}, false);
        clearTimeout(window.showtimeout);   
        var queue = Effect.Queues.get('imagepopup');
        if (visiblePopup && visiblePopup.style.display == 'none'){
            Position.clone(srcEl, visiblePopup, 
            {
                setWidth:false, 
                setHeight:false, 
                offsetTop:(snap(srcEl, visiblePopup)[1]), 
                offsetLeft:(snap(srcEl, visiblePopup)[0])
            });
            window.showtimeout = window.setTimeout(function(){
                Element.addClassName(srcEl, 'hoverthing');
                setTimeout(function(){Element.removeClassName(srcEl, 'hoverthing');}, 500);
                new Effect.Parallel([
                new Effect.MoveBy(visiblePopup, 5, 0, { sync: true }),
                new Effect.Appear(visiblePopup, {sync: true, fps:40, queue: {position:'end', scope:'imagepopup'}}) 
                ],
                { duration: 0.3 });},100);
                myLightbox.updateImageList(); 

        }
    },

    hide : function(){       
        clearTimeout(window.showtimeout);
        var obj = $('visiblepopup');
        if(obj==null){return;}
        if(obj.style.display == 'none'){clearTimeout(window.showtimeout);}
        else{
            window.showtimeout = window.setTimeout(function(){Effect.BlindUp(obj, {duration: 0.2});},1000);
        }
    } 
}

function snap(srcEl, obj) {
	var x = 0;
	var y = 0;	
    var scrollBarPadding=30;
    
	if((getLeftOffset(srcEl) + getWidth(obj)) > windowInnerSize.width()-scrollBarPadding){	
	    x=getWidth(srcEl)-getWidth(obj)-getWidth(srcEl);
	}else{
	    x=getWidth(srcEl);
	}

	if(getTopOffset(srcEl)+ getHeight(obj)> windowInnerSize.height()-scrollBarPadding){
    	y= -getHeight(obj)+getHeight(srcEl);
	}
	return [x, y];
}

var cartService = {
    addOrRemoveItem : function(el, ItemID){  
        clearTimeout(window.carttimeout);    
        window.carttimeout = setTimeout(function(){CartService.AddOrRemoveItem($(el).id, ItemID, cartService.completed);},0);
    }, 
    completed : function(result){
        if (result.status.indexOf('Remove') > -1)
        {
        Element.removeClassName(result.source, 'addtocart');
        Element.addClassName(result.source, 'removefromcart');
        Effect.Appear(Element.cleanWhitespace($(result.source).parentNode).firstChild, {duration: 0.3});
        }else{
        Element.removeClassName(result.source, 'removefromcart');
        Element.addClassName(result.source, 'addtocart');
        Effect.Fade(Element.cleanWhitespace($(result.source).parentNode).firstChild, {duration: 0.3});
        }        
        SetCartInfo();
        $(result.source).innerHTML = result.status.split(",")[0];
        Fat.fade_element("shoppingcart", 60, 1000, "#FFF1A2", "#908676");  
    }    
}

function woosh(source, target){
Position.absolutize(source);
    window.to = window.setTimeout(function(){
    new Effect.Parallel([
    new Effect.Shrink(source, {sync: true}),
    new Effect.MoveBy(source,getTopOffset(target),getLeftOffset(target),{sync: true, mode: 'absolute' })
   // new Effect.Appear(source, {sync: true, fps:40 }) 
    ],
    { duration: 1.0 });},100);
}


function shrink(pic){
if (pic.clientHeight < pic.clientWidth){
	pic.style.height = '28' + 'px';
	pic.style.width = '40' + 'px';
	pic.style.left = '5' + 'px';
    pic.style.top = '-5' + 'px';
	}else{
	pic.style.height = '40' + 'px';
	pic.style.width = '28' + 'px';
    pic.style.left = '-1' + 'px';
    pic.style.top = '-15' + 'px';
}
}  

function setStatusUpdating(id){
    document.getElementById('status' + id).innerHTML = 'Updating...';
}

function disableMe(source){
    source.disabled = true;
}

var mac = navigator.appVersion.indexOf("Mac")>-1
function getCookieVal (offset) {
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1)
		endstr = document.cookie.length;
		return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie (name) {
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) {
		var j = i + alen;
	    if (document.cookie.substring(i, j) == arg)
	    	return getCookieVal (j);
	    	i = document.cookie.indexOf(" ", i) + 1;
		    if (i == 0) break; 
		}
		if (name == "basket") {
	  		return "Basket Empty";
		} else {
			return "null";
		}
}

function SetCartInfo() {
	var HeaderCartInfo, HeaderCartData;
	HeaderCartInfo = GetCookie('cartInfo');
	
	if (mac && navigator.userAgent.indexOf("MSIE")>-1) 
	{
	    // Do Nothing
	} 
	else 
	{
		if (HeaderCartInfo == "null") 
		{	
		    $('cartInfo').innerHTML = '0 items: <b>$0.00</b>';
		} 
		else
		{ 
		    HeaderCartData = HeaderCartInfo.split("|");   		
		    if (HeaderCartData[0] == "1") 
		    {
		        $('cartInfo').innerHTML = '1 item: <b>' + HeaderCartData[1] + '</b>';
		    } 
		    else 
		    {
		        $('cartInfo').innerHTML = HeaderCartData[0] + ' items: <b>' + HeaderCartData[1] + '</b>';
		    }
        }
	}
}