$().ready(function() {
		if(document.all) {
        try {document.execCommand("BackgroundImageCache",false,true);} catch(e){}
    }
    if($.browser.msie && /6.0/.test(navigator.userAgent)){
        $('#LoginBtn,#RegisterBtn,#NodePad,#GroheShop').hover(function(){$(this).addClass('hover')},function(){$(this).removeClass('hover')});;
    }
     $('#LoginBtn,#RegisterBtn,#NodePad,#GroheShop').click(function(){var _ref = $(this).attr('rel'); location.href = _ref;});

    $('button.popup').click(function(){
    		TB_show('', $(this).attr('rel'), false);
        	return false;
    });

    $('#PageTools a, #NodePad, #GroheShop').each(function(i){
        var _ctitle = $(this).attr('title');
        var _id = $(this).attr('id');
		if(_id == 'NodePad') {
			var _divid = 'pt_notepad';
		}
		else if (_id == 'GroheShop') {
			var _divid = 'pt_groheshop';
		}
		else {
          $(this).attr('rel','pt'+i);
          var _divid = 'pt'+i;
        }
        $('#PageTools').append('<div id="'+_divid+'" class="tooltip"><div><div><div><div>'+_ctitle+'</div></div></div></div></div>');
    }).mouseover(function(){
        var base = ($('#Wrapper').offset());
        var o = ($(this).offset());
        var _posTT = o.left-base.left;
        var _id = $(this).attr('id');
        if(_id == 'NodePad') {
			var _rel = 'pt_notepad';
		}
		else if (_id == 'GroheShop') {
			var _rel = 'pt_groheshop';
		}
        else {
			var _rel = $(this).attr('rel');
        }
        $('#'+_rel).show();
        var _w = parseInt($('#'+_rel)[0].offsetWidth);
        $('#'+_rel).css({'left':_posTT-(_w+10)+'px'});
    }).mouseout(function(){
        var _id = $(this).attr('id');
        if(_id == 'NodePad') {
			var _rel = 'pt_notepad';
		}
		else if (_id == 'GroheShop') {
			var _rel = 'pt_groheshop';
		}
		else {
			var _rel = $(this).attr('rel');
        }
        $('#'+_rel).hide();
    });



    $('div.tabset-box').each(function(){
        var c = this;
        $(c).tabs();
    });



    if($('#sorter').size()>0) {$('#Sorting button').hide(); $('#sorter').selectbox();}
    if($('#amount-select').size()>0) {$('#Sorting button').hide(); $('#amount-select').selectbox({'inputClass':'selectbox4','containerClass':'selectbox-wrapper4'});}
    /*if($('#topic').size()>0) {$('#topic').selectbox({'inputClass':'selectbox2','containerClass':'selectbox-wrapper2'});}*/
    $('select.jsf-text').each(function(){
        $(this).selectbox({'inputClass':'selectbox2','containerClass':'selectbox-wrapper2'});
    });
    $('.Box-toogleNext').hide();
    $('.toogleBox').each(function(){
        if($('.error',this).size()==0) {
            $(this).hide();
        } else {
            $(this).show();
        }
    });

    $('.toogleNext a').click(function(){
        var tn = $(this.parentNode).next()[0];
        if(this.className.indexOf('checked')!=-1) {
            $(tn).show();
        } else {
            $(tn).hide();
        }
    });
    $('.toogleNext label').click(function(){
        var tn = $(this.parentNode).next()[0];
        var ta = $('a',this.parentNode)[0];
        if(ta.className.indexOf('checked')!=-1) {
            $(tn).show();
        } else {
            $(tn).hide();
        }
    });
    if($('#SearchNavigation').size() > 0) {
        $('#SearchNavigation > ul > li').each(function() {
            if($('a',this).size()>0) {
                var tlinkC = $('a',this)[0].className;
                if(tlinkC.indexOf('closed')!=-1) {$('ul',this).hide();}
            }

        });
        $('#SearchNavigation > ul > li > a > img.xxxx').click(function(event) {

        	if ($(this).attr('rel')) window.location.href = $(this).attr('rel');

        	return false;
        });

        $('#SearchNavigation > ul > li > a').click(function() {
            var _c = this.className;
			if(_c == 'closeable') {
				return true;
			}
            if(_c.indexOf('closed')!=-1) {
                $('#SearchNavigation > ul > li > a').each(function() {
                    var __c = this.className;
                    if(__c.indexOf('open')!=-1) {
                        $(this).addClass('closed').removeClass('open');
                        $('ul',this.parentNode).hide();
                    }
                });
                $(this).addClass('open').removeClass('closed');
                $('ul',this.parentNode).show();
                $('span',this.parentNode).hide();
            } else if(_c.indexOf('open')!=-1) {
                $(this).addClass('closed').removeClass('open');
                $('ul',this.parentNode).hide();
                $('span',this.parentNode).show();
            }
            return false;
        });
    }

    $('.box-elm > img').each(function() {
	if ($('#ContentsMeta, #Contents').hasClass('tcc-template') || $('#ContentsMeta, #Contents').hasClass('tcc-template-reduced')) {
            return;
	}

        var _osrc = $(this).attr('src');
        $(this).attr('rel',_osrc);
    });


    var scTimer = false;

    var _oimgs = [];
    $('.product-sc > .box-list > .box-elm > img').each(function(){
        _oimgs[_oimgs.length] = {'elm':this,'src':this.src};
    });

    $('ul.search-criterias a').mouseover(function(){
        if(scTimer) {
          for(var oi=0; oi< _oimgs.length; oi++) {
            _oimgs[oi].elm.src = _oimgs[oi].src;
          }
        }
        var _oimg = $('img',this.parentNode.parentNode.parentNode.parentNode.parentNode);
        //var _oimg = $('img',this.parentNode.parentNode.parentNode.parentNode);
        var _cimg = $('img',this.parentNode);
        clearTimeout(scTimer);
        scTimer = false;
        //window.status = _cimg.size();
        if(_cimg.size()>0 && _oimg.size()>0) {
            var _csrc = _cimg.attr('src');
            _oimg[0].src = _csrc;
        }
    }).mouseout(function(){
        //var _oimg = $('img',this.parentNode.parentNode.parentNode.parentNode.parentNode);
        var _oimg = $('img',this.parentNode.parentNode.parentNode.parentNode);
        var _cimg = $('img',this.parentNode);
        if(_cimg.size()>0 && _oimg.size()>0) {
            var _osrc = _oimg.attr('rel');
            scTimer = setTimeout(function(){_oimg[0].src = _osrc},3000);
        }

    });

    $('.box-elm .h').hide();
    $('.box-elm a.v').click(function() {
        $('.h',this.parentNode.parentNode).show();
        $(this).hide();
        return false;
    });
    $('.box-elm a.h').click(function() {
        $('.h',this.parentNode.parentNode).hide();
        $('a.v',this.parentNode).show();
        return false;
    })
    $('.o .h').show();
    $('.o .v').hide();
    if($('.mo').size()>0){
        if($('.mo a').size()==0){
            $('.mo').hide();
        }
    }

     $('td.colour a').click(function(){
      var cid  = $(this).attr('id');
      var col = false;
      // the ajax call request by link id
      var dat = { };
      // using the current link url for ajax url
      var baseurl = $(this).attr('href');

      $.post(baseurl, dat, function(json) {
	      try {
	        eval('resp = ' + json);
	        if(!resp.error) {
	        	var colour = resp;
	          changeColour(resp);

	        }
	      } catch(e) { }

	      return false;
	    });

		function changeColour(__colour) {

			if(cid && __colour[cid]) {
				col = __colour[cid];

				if(col.color) {
					$('#color').attr('src', col.color);
				}

				$('div.product-detail h2').html(col.article + ' ' + col.title);
				$('div.product-text h2').html(col.headline);
				$('div.product-text ul li').html(col.text);
				//$('div.product-text').jScrollPane({showArrows:true, scrollbarWidth: 17});
				
				if($('div.product-text').height() > 200) {
					$('div.product-text').css('height', 200);
					$('div.product-text').jScrollPane({showArrows:true, scrollbarWidth: 17});
				}

				if(col.relevant_links['enlarge']) {
					$('a.enlarge').attr('href', col.relevant_links['enlarge']);
				}
				else {
					$('a.enlarge').attr('href', '#');
				}

				set = ['flow', 'dim', 'line', 'exp', 'spare', 'spare_list'];

				for(a in set) {
				
				
				
					if(col.relevant_links[set[a]]) {
						$('#'+set[a]).attr('href', col.relevant_links[set[a]]);
						$('#'+set[a]).css({'display': 'inline'});

						if (col.labels[set[a]]) $('#'+set[a]).html(col.labels[set[a]] + '<br />');
					}
					else {
						$('#'+set[a]).css({'display': 'none'});
					}
				}

				if (col.relevant_links['rotation']) {
					$('#view-threesixty').attr('href', './product_rotator.php?product=' + col.relevant_links['rotation'] + '&TB_iframe=true&height=610&width=800');
				}
				else {
					$('#view-threesixty').css({'display': 'none'});
				}
				
				if (col.relevant_links['tpi']) {
				
					$('a.tpi').remove();
					for (a in col.relevant_links['tpi']) {
						$("#tpi").clone().removeAttr('onmousedown').bind('mousedown', function() { wt_sendinfo(col.relevant_links['tpi_webtrekk'] + a, 'click'); }).css({'display': 'inline'}).removeAttr('id').addClass('tpi').attr('href', col.relevant_links['tpi'][a]).insertBefore($("#tpi"));
					}
				}
				if (col.relevant_links['gw']) {
					$('a.gw').remove();
					$("#gw").clone().removeAttr('onmousedown').bind('mousedown', function() { wt_sendinfo(col.relevant_links['gw_webtrekk'], 'click'); }).css({'display': 'inline'}).removeAttr('id').addClass('gw').attr('href', col.relevant_links['gw']).insertBefore($("#gw"));
				}
				
				if (col.relevant_links['2d_dwg']) {
					$('a.2d_dwg').remove();
					$("#2d_dwg").clone().removeAttr('onmousedown').bind('mousedown', function() { wt_sendinfo(col.relevant_links['dwg_2d_webtrekk'], 'click'); }).css({'display': 'inline'}).removeAttr('id').addClass('2d_dwg').attr('href', col.relevant_links['2d_dwg']).insertBefore($("#2d_dwg"));
				}

				if (col.relevant_links['2d_dxf']) {
					$('a.2d_dxf').remove();
					$("#2d_dxf").clone().removeAttr('onmousedown').bind('mousedown', function() { wt_sendinfo(col.relevant_links['dxf_2d_webtrekk'], 'click'); }).css({'display': 'inline'}).removeAttr('id').addClass('2d_dxf').attr('href', col.relevant_links['2d_dxf']).insertBefore($("#2d_dxf"));
				}
				
				if (col.relevant_links['3d_dxf']) {
					$('a.3d_dxf').remove();	
					$("#3d_dxf").clone().removeAttr('onmousedown').bind('mousedown', function() { wt_sendinfo(col.relevant_links['dxf_3d_webtrekk'], 'click'); }).css({'display': 'inline'}).removeAttr('id').addClass('3d_dxf').attr('href', col.relevant_links['3d_dxf']).insertBefore($("#3d_dxf"));
				}
				
			}
		}

      // finaly change the active image icon...
      $('td.colour a').removeClass('active');
      $(this).addClass('active');
      return false;
     });
});

 function createElement (tag, attribs, styles, parent, nopad) {
	var el = document.createElement(tag);
	if (attribs) setAttribs(el, attribs);
	if (nopad) setStyles(el, {padding: 0, border: 'none', margin: 0});
	if (styles) setStyles(el, styles);
	if (parent) parent.appendChild(el);
	return el;
}

 function setAttribs (el, attribs) {
	for (var x in attribs) el[x] = attribs[x];
};

function setStyles (el, styles) {
	for (var x in styles) {
		try {
			if (hs.ie && x == 'opacity')
				el.style.filter = (styles[x] == 1) ? '' : 'alpha(opacity='+ (styles[x] * 100) +')';
			else el.style[x] = styles[x];
		}
		catch (e) {}
	}
}


function addToNodePad() {
   var boxdim = {'width':'300px'};
   var message = '<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</p>';
   MessageInfo(boxdim,message);
}


function MessageInfo(boxdim,message) {
	if ( !$("#TB_HideSelect").length ) {
		$("body").append("<iframe id='TB_HideSelect'></iframe><div id='TB_overlay'></div>");
	}
	_overlaySize();
  if ( !$("#MessageWin").length ) {
    $('body').append('<div id="MessageWin"></div>');
  	var con = $('#MessageWin')[0];
  	var o = 40 / 4;
  	pos = [[0,0],[0,-4],[-2,0],[0,-8],0,[-2,-8],[0,-2],[0,-6],[-2,-2]],
  	dim = { height: (2*o) +'px', width: (2*o) +'px' };
  	table = createElement(
  	'table', { cellSpacing: 0 },
  	{
  		visibility: 'hidden',
  		position: 'absolute',
  		borderCollapse: 'collapse'
  	},
  	con,
  	true
  	);
  	tbody = createElement('tbody', null, null, table, 1);
  	td = [];
  	for (var i = 0; i <= 8; i++) {
  		if (i % 3 == 0) tr = createElement('tr', null, { height: 'auto' }, tbody, true);
  		td[i] = createElement('td', null, null, tr, true);
  		var style = i != 4 ? { 'lineHeight': '0', 'fontSize': '0'} : { 'position' : 'relative' };
  	  $(td[i]).css(style);
  	}

  	for (var i = 0; i <= 8; i++) {
  		if (pos[i]) {
  			if (document.all) {
  				var w = (i == 1 || i == 7) ? '100%' : 40 +'px';
  				var div = createElement('div', null, null, td[i], true);
  				$(div).css({ 'width': '100%', 'height': '100%', 'position': 'relative', 'overflow': 'hidden'});
  				if(i==3 || i==5) {$(div).addClass('iefixh');}
  				var xdiv = createElement ('div', null, null, div,true);
  				$(xdiv).css({
  						'filter': "progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale, src='img/main/rounded-white.png')",
  						'position': 'absolute',
  						'width': w,
  						'height': 3000+'px',
  						'left': (pos[i][0]*o)+'px',
  						'top': (pos[i][1]*o)+'px'
  					});
  			} else {
  			   $(td[i]).css({'backgroundImage': 'url(img/main/rounded-white.png)','backgroundPosition':(pos[i][0]*o)+'px '+(pos[i][1]*o)+'px'});
  			}
  			$(td[i]).css(dim);
  		}
  		if(i==4) {
  		    $(td[i]).css({'backgroundColor':'#FFFFFF'});
  		    var _box = td[i];
       }
  	}
  	$("#MessageWin").append('<div id="closeMW"></div>');
  	$('#closeMW').click(function(){
      $('#TB_HideSelect,#MessageWin,#TB_overlay').hide();
    });
  } else {
    var _box = ("#MessageWin td")[4];
  }
  $(_box).css(boxdim).html('<div class="messagebox">'+message+'</div>');

  var de = document.documentElement;
  var ww = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
	var wh = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;


  $('#MessageWin').show().css({'left':(ww-parseInt(boxdim.width))/2+'px','top':(wh-parseInt($('#MessageWin')[0].offsetHeight))/2+'px'});
  var _mwH = parseInt($('#MessageWin')[0].offsetHeight);
  $('#MessageWin .iefixh').css({'height':(_mwH-40)});
}


function _overlaySize(){
	if (window.innerHeight && window.scrollMaxY || window.innerWidth && window.scrollMaxX) {
		yScroll = window.innerHeight + window.scrollMaxY;
		xScroll = window.innerWidth + window.scrollMaxX;
		var deff = document.documentElement;
		var wff = (deff&&deff.clientWidth) || document.body.clientWidth || window.innerWidth || self.innerWidth;
		var hff = (deff&&deff.clientHeight) || document.body.clientHeight || window.innerHeight || self.innerHeight;
		xScroll -= (window.innerWidth - wff);
		yScroll -= (window.innerHeight - hff);
	} else if (document.body.scrollHeight > document.body.offsetHeight || document.body.scrollWidth > document.body.offsetWidth){ // all but Explorer Mac
		yScroll = document.body.scrollHeight;
		xScroll = document.body.scrollWidth;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		yScroll = document.body.offsetHeight;
		xScroll = document.body.offsetWidth;
  	}
	$("#TB_overlay").css({"height": yScroll, "width": xScroll});
	$("#TB_HideSelect").css({"height": yScroll,"width": xScroll});
	$('#TB_HideSelect,#TB_overlay').show();
}

// jQuery Selector escaping
function jq (str) {
    return str.replace(/([#;&,\.\+\*\~':"\!\^$\[\]\(\)=>\|])/g, "\\$1");
};
