/*****************************************************
   Popup windows
******************************************************/
function popup(url, w, h, f, t, l) {
   var id = getRandomNumber();
   var name = "BREDBANDSBOLAGET_" + id;

   /* try to set width and height */
   w = w == null ? screen.availWidth : w;
   h = h == null ? screen.availHeight : h;

   var features = "width="+w+",height="+h;

   /* try to set left and sceenX */
   l = l == null ? Math.round((screen.availWidth-w)/2) : l;
   l = l < 0 ? 0 : l;
   features += ",left="+l+",screenX="+l;

   /* try to set top and sceenY */
   t = t == null ? Math.round((screen.availHeight-h-30)/2) : t;
   t = t < 0 ? 0 : t;
   features += ",top="+t+",screenY="+t;

   /* try to set aditional features */
   features = f == null ? features : features +  ","+f;

   var win = window.open(url, name, features);

   /* try to position window */
   try { win.moveTo(l, t); }
   catch(e) { ; }

   try { win.focus(); }
   catch(e) { ; }
}

/*****************************************************
	How tab in defaultTabBox
******************************************************/
var oldTabId = "item01";
function showTab(id)
{
	document.getElementById(oldTabId+"Tab").className = document.getElementById(oldTabId+"Tab").className.replace(/active/g, '');
	document.getElementById(oldTabId+"Content").className = document.getElementById(oldTabId+"Content").className.replace(/active/g, '');

	var sep = " ";
	if(document.getElementById(id+"Tab").className.indexOf('first') != -1) {
		sep = "";
	}

	document.getElementById(id+"Tab").className += sep + "active";
	document.getElementById(id+"Content").className += sep + "active";

	oldTabId = id;

	try {
		document.getElementById(id+"Tab").firstChild.blur();
	} catch (e) {; }
}

/*****************************************************
   Tab
******************************************************/

var ToggleTab = Class.create();

ToggleTab.prototype = {
   initialize: function(src) {
      this.src = $(src);
		this.tgts = new Array();
		this.a = this.src.firstDescendant();
		this.src.setStyle({'cursor':'pointer'});
		//Event.observe($(src), 'mousedown', this.show.bindAsEventListener(this));
   },
	get : function () {
		return this.src;
	},
	addTarget: function (tgt) {
		this.tgts.push($(tgt));
	},
	hide: function(obj) {
		this.src.removeClassName('active');
		for(var i=0; i<this.tgts.length; i++) {
			this.tgts[i].hide();
		}
	},
	show: function() {
		this.src.addClassName('active');
		for(var i=0; i<this.tgts.length; i++) {
			this.tgts[i].show();
		}
		if ( /MSIE (5\.5|6)/.test(navigator.userAgent) )
		{
			renderHTag("2", 26 , "bold");
			renderHTag("3", 22 , "regular");
			correctPNG();
		}
	}
};

var TabEffect = Class.create();

TabEffect.prototype = {
  initialize: function() {
     this.objs = new Array();
	  this.active = null;
  },
  addTab: function () {
	  var data = $A(arguments);
	  var src = data[0];
	  var obj = new ToggleTab(src);
	  for(var i=1; i<data.length; i++) {
		  obj.addTarget(data[i]);
	  }
     this.objs.push(obj);
     Event.observe($(src), 'mousedown', this.click.bindAsEventListener(this,  this.objs.length-1));
  },
  getSize : function() {
		return this.objs.length;
  },
  getObj: function (i) {
		return this.objs[i];
  },
  click: function (e) {
     var tag = Event.element(e).tagName.toLowerCase();
     var data = $A(arguments);
     var n = data[1];

     for(var i=0; i<this.objs.length; i++) {
	     if(i != n) this.objs[i].hide();
     }
	  this.objs[n].show();
  },
  enableById: function (id) {
	  var obj = null;
	  for(var i=0; i<this.objs.length; i++) {
	     if(this.objs[i].get().readAttribute('id') == id) this.objs[i].show();
		  else this.objs[i].hide();
     }
  }
};


/*****************************************************
   Toggle
******************************************************/

var ToggleButton = Class.create();

ToggleButton.prototype = {
   initialize: function(src, tgt) {
      this.src = $(src);
      this.tgt = $(tgt);
		Event.observe($(src), 'click', this.click.bindAsEventListener(this));
   },

	hide: function(obj) {

		if(this.tgt != null && this.tgt != obj.tgt) {
	      this.tgt.addClassName('moreinfo');
		}
		this.src.checked = false;
	},

	show: function() {
		if(this.tgt != null)
			this.tgt.removeClassName('moreinfo');
		this.src.checked = true;
	},

	click: function() {
		// Change minus/plus
		jQuery(this.tgt).toggleClass('moreinfo');

      /*if(this.tgt == null) return;
            if(this.src.checked) this.tgt.removeClassName('moreinfo');
            else this.tgt.addClassName('moreinfo');
      Event.stop(e);*/
	},

	toggle: function() {
		if(this.tgt != null) {
            if(this.src.checked) this.tgt.removeClassName('moreinfo');
            else this.tgt.addClassName('moreinfo');
        }
	}

};


var ToggleEffect = Class.create();

ToggleEffect.prototype = {
  initialize: function() {
     this.objs = new Array();
     this.multiple = false;
  },

  setMultiple: function(m) {
     this.multiple = m;
  },

  addCheckBox: function (src, tgt) {
     this.objs.push(new ToggleButton(src,tgt));
	  if(!this.multiple) {
        Event.observe($(src), 'click', this.toggleCheckBoxes.bindAsEventListener(this,  this.objs.length-1));
	  }
  },

   addRadioButton: function (src, tgt) {
     this.objs.push(new ToggleButton(src,tgt));
	  if(!this.multiple) {
        Event.observe($(src), 'click', this.toggleRadioButtons.bindAsEventListener(this,  this.objs.length-1));
	  }
  },

  /*addImage: function (src) {
     this.objs.push(new ToggleImage(src,tgt));
	  if(!this.multiple) {
        Event.observe($(src), 'click', this.toggleImages.bindAsEventListener(this,  this.objs.length-1));
	  }
  },*/

  toggleCheckBoxes: function(e) {
     var tag = Event.element(e).tagName.toLowerCase();
     var data = $A(arguments);
     var n = data[1];

     for(var i=0; i<this.objs.length; i++) {
	     if(i == n) continue;
		  this.objs[i].hide(this.objs[n]);
     }
  },

  toggleRadioButtons: function(e) {
     var tag = Event.element(e).tagName.toLowerCase();
     var data = $A(arguments);
     var n = data[1];
	  this.objs[n].toggle();

	  if(!this.objs[n].src.checked) return;
     for(var i=0; i<this.objs.length; i++) {
	     if(i == n) continue;
		  this.objs[i].hide(this.objs[n]);
     }
  },

  addFocusField: function (src, fld, sld) {
     Event.observe($(src), 'click', this.toggleFocusField.bindAsEventListener(this, $(src), fld, sld));
  },

  toggleFocusField: function (e) {
    var tag = Event.element(e).tagName.toLowerCase();
    var data = $A(arguments);
    var src = data[1];
    var fld = data[2];
    var sld = data[3];

    if(src.checked)
	 	setTimeout("$('"+fld+"').focus();", 250);
    else
	 	setTimeout("$('"+sld+"').focus();", 250);
   }

};

/*****************************************************
   Show hide Kundservice FAQ rows
******************************************************/
var currentFaqId = null;
var animatingShowHideInfo = false;

function resetAnimatingShowHideInfo() {
	animatingShowHideInfo = false;
}

function showHideInfo(id) {
	if(animatingShowHideInfo) return;
	animatingShowHideInfo = true;
	
	try {
		var obj = $('row' + id);
		var objContent = obj.down('.long');
		var old = $('row' + currentFaqId);
		
		var s = Math.max(0.3, objContent.getHeight() / 100 * 0.2);
		var d = 0;
		if(old != null) { 
			var oldContent = old.down('.long');
			d = 0.25;
			new Effect.BlindUp(oldContent, { duration: d, transition: Effect.Transitions.sinoidal});
			old.removeClassName("active");
		}	
		
		if(currentFaqId != id) {
			new Effect.BlindDown(objContent, {delay: d, duration: s, transition: Effect.Transitions.sinoidal});
			obj.addClassName("active");
			currentFaqId = id;
		} else {
			currentFaqId = null;
		}
		
		setTimeout("resetAnimatingShowHideInfo()", (d+s) * 1000);
	
	} catch (e) {
		var o = $('longInformation'+id);
		if(o != null) o.toggle();
		animatingShowHideInfo = false;
	}

}

/*****************************************************
   Submit orderflow
******************************************************/

function setEventId(o, v) {
	var f = o.form;
	f._eventId.value = v;
	f.submit();
}

function setEventIdByKeyCode(o, v) {
	if (event.which && event.which == 13) {
		setEventId(o, v);
	} else if(window.event && window.event.keyCode==13) {
		setEventId(o, v);
	}
}

/*****************************************************
	Toggle product MOREINFO layer 
******************************************************/

var oldToggleObj = null;
var oldToggleFormObj = null;
function toggleEffect(n, h, o) {
	var allNodes = getElementsByClassName(n);
	if(!isArray(allNodes) || allNodes.length == 0) return false;

	var obj = allNodes[0];

	if(h == null) h = "auto";
	else h = h + "px";

	if(oldToggleObj != null) {
		endToggle(oldToggleObj);
		if(obj != oldToggleObj) {	
			startToggle(obj, h);
		} else {
			oldToggleObj = null;
		}
	} else {
		startToggle(obj, h);
	}
	
	if(oldToggleFormObj != null && oldToggleFormObj != o) {
		oldToggleFormObj.checked = false;
	}
	oldToggleFormObj = o;
}

function toggleMultipleEffect(n, h) {
	var allNodes = getElementsByClassName(n);
	if(!isArray(allNodes) || allNodes.length == 0) return false;

	var obj = allNodes[0];

	if(h == null) h = "auto";
	else h = h + "px";

	if(obj.style.display == "none" || obj.className.indexOf('moreinfo') != -1) {
		startToggle(obj, h);
		return true;
	}
	
	endToggle(obj);
	return false;
}

function startToggle(obj, h) {
	obj.style.display="block";
	obj.style.height=h;
	oldToggleObj = obj;
	Element.extend(obj);
	obj.removeClassName('moreinfo');
	obj.setStyle({'display':'block'});
}

function endToggle(obj) {
	Element.extend(obj);
	obj.addClassName('moreinfo');
	obj.setStyle({'height':'0px','display':'none'});
}

/*****************************************************
 * B2_SlideShow
 *
 * @description: Image slideshow
 */
var B2_SlideShow = Class.create();

B2_SlideShow.prototype = {
    initialize: function(id, options) {
        if (!options) options = {}
		var defaultOptions = {
            knapp_off : '/portal/wcmobject/document/img/rightpuff_knapp_off.gif',
            knapp_on : '/portal/wcmobject/document/img/rightpuff_knapp_on.gif',
            arrow_left : '/portal/wcmobject/document/img/rightpuff_arrow_left.gif',
            arrow_right : '/portal/wcmobject/document/img/rightpuff_arrow_right.gif',
			space: 14
		}
		Object.extend(defaultOptions, options);	
		this.options = defaultOptions;
        this.container = $(id) || new Element('div');
        this.container.setStyle({'position':'relative','overflow':'hidden'});
        this.element = this.container.appendChild(new Element('div', {
            'style':'position:relative;'
        }));
        this.dummy = this.container.appendChild(new Element('div', {
            'style'     : 'position:absolute;width:100%;height:30px;background:#fff'
        })).setOpacity(0);
        this.buttons = this.container.appendChild(new Element('div', {
            'style'     : 'position:absolute;top:12px;'
        }));
        this.transition = this.element.getStyle('position') == 'relative';
        this.arr = new Array();
        this.index = 0;
        this.pe = null;
        this.cur = null;
        this.width = parseInt(this.container.getStyle('width'));
        this.delay = null;
    },
    add : function(src, href) {
        var el = new Element('img', {'src' : src, 'alt' : '', 'border' : '0'});
        if(href) {
            var tmp = el;
            el = new Element('a', {'href' : href});
            el.appendChild(tmp);
        }
        el.setStyle({display:'block',position:'absolute',top:'0',left:(this.width*this.arr.length) + 'px'});
        this.element.appendChild(el);
      
        this.arr.push(el);
    },
    getButton : function(src1, src2) {
        var elements = this.buttons.childElements();
        
        var button = new Element('a', {
            'style'     : 'cursor:pointer;position:relative;left:' + ((elements.length * (this.options.space+4))) + 'px'
        });
        
        var img1 = new Element('img', {
            'src'       : src1, 
            'border'    : '0',
            'style'     : 'position:absolute;width:'+this.options.space+'px;height:'+this.options.space+'px;'
        });
        button.appendChild(img1);
        
        if(src2) {
            var img2 = new Element('img', {
                'src'       : src2, 
                'border'    : '0',
                'style'     : 'position:absolute;display:none;'
            });
            button.appendChild(img2);
        }
        
        return button;
    },
    addButtons : function() {
        var left = this.getButton(this.options.arrow_left);
        left.observe("click", this.prev.bind(this));
        this.buttons.appendChild(left);
        
        for(var i=0; i<this.arr.length; i++) {
            var knapp = this.getButton(this.options.knapp_off, this.options.knapp_on);
            knapp.observe("click", this.show.bind(this, i));
            this.buttons.appendChild(knapp);
        }
        
        var right = this.getButton(this.options.arrow_right);
        right.observe("click", this.next.bind(this));
        this.buttons.appendChild(right);
        var x = (this.width - this.buttons.childElements().length * (this.options.space+4))  / 2;
        this.buttons.setStyle({'left' : x + 'px'});
    },
    updateButtons : function() {
        var elements = this.buttons.childElements();
      
        for(var i=0; i<elements.length-2; i++) {
           if(i == this.index) elements[i+1].down(1).appear({duration:0.2});
           else elements[i+1].down(1).fade({duration:0.1});
        }
        
    },
    run : function(delay) {
        if (this.arr.length > 1) this.addButtons();
        this.stop(false);
        if(typeof delay == "number" && delay > 0) {
            this.delay = delay;
        }
        this.show();
    },
    prev : function(event) {
        if(--this.index < 0) {
            this.index = this.arr.length-1;
        }
        this.show();
        try { event.stop(); } catch(e) {;}
    },
    next : function(event) {
        if(++this.index >= this.arr.length) {
            this.index = 0;
        }
        this.show();
        try { event.stop(); } catch(e) {;}
    },
    start : function() {
        this.stop(false);
        if(this.delay) {
            this.pe = new PeriodicalExecuter(this.next.bind(this), this.delay);
        }
    },
    stop : function(remove) {
        if(this.pe) this.pe.stop();
        if(remove) {
            this.element.remove();
        }
    },
    isIndex : function(index) {
        return typeof (index) == "number" && index >= 0 && index < this.arr.length;
    },
    show : function(index) {
        this.start();
        this.index = this.isIndex(index) ? index : this.index;
        this.cur = this.arr[this.index];
        this.updateButtons();
        new Effect.Move(this.element, {
            duration: 0.4, 
            mode : 'absolute', 
            x: (this.width * this.index * -1),
            y: 0
        });   
    }
};