function colorMenu(id) {
	document.getElementById("m" + (id+1)).style.background = "none";
	document.getElementById("m" + (id+1)).style.backgroundColor = "#fbba00";
}

function decolorMenu(id) {
	document.getElementById("m" + (id+1)).style.backgroundColor = "";
	document.getElementById("m" + (id+1)).style.background = "url(/images/shop-pics/menusep_h.gif)";
}

function MM_swapImgRestore() {
	var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() {
	var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
	var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
	if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) {
	var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
	  d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
	if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
	for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
	if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() {
	var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
	   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function SocialBookmark(service, lang){
	var sb_url_array = document.getElementsByName('staticurl');
	var sb_url = encodeURIComponent(sb_url_array[0].content);

	var sb_title = encodeURIComponent(document.title);

	var sb_description_array = document.getElementsByName('description');
	var sb_description = sb_description_array[0].content;

	var sb_keywords_array = document.getElementsByName('keywords');
	var sb_keywords = sb_keywords_array[0].content;

	var surl = new Array();

	switch(service) {
	case 'mrwong' :
		surl[1] = 'http://www.mister-wong.de/index.php?action=addurl&bm_url='+sb_url+'&bm_description='+sb_title;
		surl[2] = 'http://www.mister-wong.com/index.php?action=addurl&bm_url='+sb_url+'&bm_description='+sb_title;
		break;

	case 'linkarena' :
		surl[1] = 'http://linkarena.com/bookmarks/addlink/?url='+sb_url+'&title='+sb_title+'&desc='+sb_description+'&tags='+sb_keywords;
		surl[2] = 'http://linkarena.com/bookmarks/addlink/?url='+sb_url+'&title='+sb_title+'&desc='+sb_description+'&tags='+sb_keywords;
		break;

	case 'live' :
		surl[1] = 'http://favorites.live.com/quickadd.aspx?marklet=1&mkt=de-de&top=0&url='+sb_url+'&title='+sb_title;
		surl[2] = 'http://favorites.live.com/quickadd.aspx?marklet=1&mkt=en-us&top=0&url='+sb_url+'&title='+sb_title;
		break;

	case 'facebook' :
		surl[1] = 'http://www.facebook.de/share.php?u='+sb_url+'&t='+sb_title;
		surl[2] = 'http://www.facebook.com/share.php?u='+sb_url+'&t='+sb_title;
		break;

	case 'google' :
		surl[1] = 'http://www.google.com/bookmarks/mark?op=add&hl=de&bkmk='+sb_url+'&title='+sb_title;
		surl[2] = 'http://www.google.com/bookmarks/mark?op=add&hl=en&bkmk='+sb_url+'&title='+sb_title;
		break;

	case 'delicious' :
		surl[1] = 'http://del.icio.us/post?url='+sb_url+'&title='+sb_title+'&notes='+sb_description+'&tags='+sb_keywords;
		surl[2] = 'http://del.icio.us/post?url='+sb_url+'&title='+sb_title+'&notes='+sb_description+'&tags='+sb_keywords;
		break;

	case 'yahoo' :
		surl[1] = 'http://myweb2.search.yahoo.com/myresults/bookmarklet?u='+sb_url+'&t='+sb_title;
		surl[2] = 'http://myweb2.search.yahoo.com/myresults/bookmarklet?u='+sb_url+'&t='+sb_title;
		break;

	case 'webnews' :
		surl[1] = 'http://www.webnews.de/einstellen?url='+sb_url+'&title='+sb_title;
		surl[2] = 'http://www.webnews.com/einstellen?url='+sb_url+'&title='+sb_title;
		break;

	case 'yigg' :
		surl[1] = 'http://yigg.de/neu?exturl='+sb_url+'&exttitle='+sb_title;
		surl[2] = 'http://yigg.de/neu?exturl='+sb_url+'&exttitle='+sb_title;
		break;

	case 'folkd':
		surl[1] = 'http://www.folkd.com/page/submit.html?url='+sb_url+'&lang=de';
		surl[2] = 'http://www.folkd.com/page/submit.html?url='+sb_url+'&lang=en';
		break;
	}

	window.open(surl[lang]);
}

// Browser Detection
function isMSIE60(){
  var browser  = navigator.userAgent.toLowerCase();
  var is_major = parseInt(navigator.appVersion);
  var is_minor = parseFloat(navigator.appVersion);
  var is_ie     = ((browser.indexOf('msie') != -1) && (browser.indexOf('opera') == -1));
  return (is_ie && (is_major == 4) && (browser.search(/msie [56]\./)!=-1) );
}

// Debug
function log(content) {
  if ( typeof(console) != 'undefined' && console != null ) {
    console.log(content);
    return;
  } else if ( typeof(opera) != 'undefined' && opera != null ) {
    opera.postError(content);
    return;
  } else {
    alert(content);
  }
}

// Menu
var MSIE60 = Class.create({
  initialize: function() {
    this.openLayers = 0;
  },
  hideSelectfields: function() {
    if (isMSIE60()) {
      this.openLayers++;
      $$('select').each(function(select) {
        select.setStyle({
          visibility: 'hidden'
        });
      });
    }
  },
  showSelectfields: function() {
    if (isMSIE60()) {
      this.openLayers--;
      if (this.openLayers == 0){
        $$('select').each(function(select) {
          select.setStyle({
            visibility: 'visible'
          });
        });
      }
    }
  }
});

window.MSIE60 = new MSIE60();

var JsNavi = Class.create({
  initialize: function(settings){
    this.content = settings.content;              
    this.positionDelta = settings.positionDelta || [0,0]; 
    this.parent = settings.parent || null;            // Elternelement, mit dem die Navi getriggert wird
    this.addToParent = settings.addToParent || null;  // Element, in das Navielement eingefügt werden soll    
    this.delay = settings.delay || 0;            
    this.delayShow = (settings.delayShow == null) ? 0.4 : settings.delayShow;    
    this.delayHide = (settings.delayHide == null) ? 0.7 : settings.delayHide; 
    this.fadeDuration = (settings.fadeDuration == null) ? 0.7 : settings.fadeDuration;
    this.arrowPosition = [0, 0];     
    this.fixedOffsetTop = settings.fixedOffsetTop || null; 
    
    this.element = null;
    this.contentbody = null;
    this.isOpen = false;

    this.headerid = settings.headerid;

    // Arrow Position
    if (this.parent.getWidth() > 0) {
      this.arrowPosition = [this.parent.getWidth()/2-6, 0];
    } else {
      this.arrowPosition = [20, 0];
    }

    this.boundShow = this.show.bind(this);
    this.boundHide = this.hide.bind(this);

    this.setUp();
  },
  setUp: function() {
    var styles = '';
    var dOffsets = document.viewport.getScrollOffsets();

    if (this.addToParent) {
      styles += 'position: absolute; ';
    }

    this.element = new Element('div', {
      className: 'jsmenu-wrapper',
      style: styles
    });

    var left = '';
    var top = '';
    if ( this.arrowPosition[0] != 0 ) {
      left = 'left: ' + this.arrowPosition[0]+'px; ';
    }
    
    if ( this.arrowPosition[1] != 0 ) {
      top = 'top: ' + this.arrowPosition[1] + 'px;'; 
    }
    
    this.element.insert(new Element('div', {
      className: 'jsmenu-arrow',
      style: left + top
    }));

    this.element.insert(new Element('div', {
      className: 'jsmenu-content-wrapper'
    }).insert(this.contentbody = new Element('div', {
      className: 'jsmenu-content'
    })));
    this.contentbody.update(this.content);
    
    this.element.observe('mouseover', function() {
      this.show();
    }.bindAsEventListener(this));

    this.element.observe('mouseout', function() {
      this.close();
    }.bindAsEventListener(this));

    if ( this.parent ) {
      this.parent.observe('mouseover', function(event) {
        this.open();
      }
      .bind(this));

      this.parent.observe('mouseout', function() {
        this.close();
      }
      .bind(this));  
    }
  },
  show: function() {
    if ( this.closeTimer ) {
      window.clearTimeout(this.closeTimer);
    }
    
    if ( !this.element ) {
      this.setUp();
    }
      
    if ( !this.isOpen ) {   
      this.element.hide();

      if ( this.addToParent ) {     
        this.addToParent.insert(this.element);
      } else {
        $('body').insert(this.element);
      }

      if ( this.parent && !this.addToParent ) {
        this.position = [this.parent.cumulativeOffset()[0] + this.positionDelta[0], this.parent.cumulativeOffset()[1] + this.positionDelta[1]];
        
        if ( this.fixedOffsetTop ) {
          this.position = [this.parent.cumulativeOffset()[0] + this.positionDelta[0], this.fixedOffsetTop + this.positionDelta[1]];
        }

        this.element.setStyle({
          left: this.position[0] + 'px',
          top:  this.position[1] + 'px'
        });

        var left = '';
        var top = '';
        if ( this.arrowPosition[0] != 0 ) {
          left = 'left: ' + this.arrowPosition[0] + 'px; ';
        }
        if ( this.arrowPosition[1] != 0 ) {
          top = 'top: ' + this.arrowPosition[1] + 'px;';
        }
        var arrow;
        if ( arrow = this.element.down('.jsmenu-arrow') ) {
          arrow.setStyle(left + top);
        }
      }

      // Wenn Menü außerhalb des Viewports liegen würde, umpositionieren
      var dimensions = this.element.getDimensions();
      var vport = document.viewport.getDimensions();
      
      while ( parseInt(this.element.getStyle('left')) + dimensions.width + 2 > vport.width ) {
        this.element.setStyle({
          'left': (parseInt(this.element.getStyle('left')) - 2) + 'px'
        });
        
        this.element.down('.jsmenu-arrow').setStyle({
          'left': (parseInt(this.element.down('.jsmenu-arrow').getStyle('left')) + 2) + 'px'
        });
      }
      
      this.element.visualEffect('appear', {
        delay: this.delay,
        duration: this.fadeDuration
      });
    }
    
    if ( !this.isOpen ) {
      window.MSIE60.hideSelectfields();
    }

    this.isOpen = true;

    colorMenu(this.headerid);

    return this.element;
  },
  hide: function() {
    if ( this.element ) {
      var up = this.element.up();

      if ( this.isOpen ) {
        window.MSIE60.showSelectfields();
      }

      this.element.visualEffect('fade', {
        duration: this.fadeDuration/1.7,
        afterFinish: function() {
          if ( up && this.isOpen ) { 
            this.element.remove();   
          }
          
          this.isOpen = false;
          
        }.bind(this)
      });
    }

    decolorMenu(this.headerid);
  },
  open: function() {
    if ( this.closeTimer ) {
      window.clearTimeout(this.closeTimer);
    }
    
    if ( this.openTimer )
      window.clearTimeout(this.openTimer);
    
    this.openTimer = this.boundShow.delay(this.delayShow);
  },
  close: function() {
    if ( this.closeTimer ) {
      window.clearTimeout(this.closeTimer);
    }
    
    if ( this.openTimer ) {
      window.clearTimeout(this.openTimer);
    }
    
    this.closeTimer = this.boundHide.delay(this.delayHide);
  }
});

function buildNavi(element, content, headerid) { 
  new JsNavi({
    content: content,
    parent: $(element),
    headerid: headerid,
    fadeDuration: 0.1,
    positionDelta: [0, 37],
    delayHide: 0.2,
    delayShow: 0.4
  });
}

function showPictures(script, product, picture) {
	popup = window.open(script + '?product=' + product + ';picture=' + picture, 'pp', 'width=460,height=350,left=150,top=100,toolbar=0,scrollbars=0,location=0,status=0,statusbar=0,menubar=0,resizable=0');
	popup.focus();
}

function showPriceGuarantee(script) {
	popup = window.open(script, 'pg', 'width=630,height=650,left=250,top=70,toolbar=0,scrollbars=1,location=0,status=0,statusbar=0,menubar=0,resizable=0');
	popup.focus();
}

function showSizeChart(script, mid, gid) {
	popup = window.open(script + '?mid=' + mid + ';gid=' + gid + ';' , 'sc', 'width=985,height=650,left=250,top=70,toolbar=0,scrollbars=1,location=0,status=0,statusbar=0,menubar=0,resizable=1');
	popup.focus();
}
