﻿
/*	Javascript controlled by Hakan Bilgin 	*/

Math.rnd					= function(n,x) {n=n||10; var i=(!x)?n:x-n;var p=(!x)?0:n;return p+parseInt(Math.random()*i);};
Array.prototype.subtract	= function(a) {if(!a)return this;if(!a.length)a=[a];var ra=new Array();for(var i=0;i<this.length;i++){if(a.indexOf(this[i])==-1)ra.push(this[i]);}return ra;};

if (!/msie/i.test(navigator.userAgent)) {
	Document.prototype.selectNodes =		function(XPath, XNode) {if(!XNode) XNode = this; this.ns = this.createNSResolver(this.documentElement); this.qI = this.evaluate(XPath, XNode, this.ns, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null); aResult = []; for(i=0; i<this.qI.snapshotLength; i++) aResult[i] = this.qI.snapshotItem(i); return aResult;};
	Document.prototype.selectSingleNode =	function(XPath, XNode) {if(!XNode) XNode = this; this.xI = this.selectNodes(XPath, XNode); return (this.xI.length > 0)? this.xI[0] : null ;};
	Element.prototype.selectNodes =			function(XPath) {return this.ownerDocument.selectNodes(XPath, this);};
	Element.prototype.selectSingleNode =	function(XPath) {return this.ownerDocument.selectSingleNode(XPath, this);};
	Node.prototype.__defineGetter__('xml',	function()		{return (new XMLSerializer()).serializeToString(this);});
}

function getChildren(el, a, v) {
	el = $(el);
	if (!el) return;
	var ar = new Array();
	var ac = el.getElementsByTagName('*');
	for (c=0, cl=ac.length; c<cl; c++) {
		if (v && a=='nodeName' && ac[c].nodeName.indexOf(':') > -1 && ac[c].nodeName.split(':')[1].toLowerCase() == v.toLowerCase()) {
			ar.push(ac[c]);
			continue;
		}
		if (v? (ac[c][a] == v || ac[c].getAttribute(a) == v) : (ac[c][a] || ac[c].getAttribute(a))) {
			ar.push(ac[c]);
		}
	}
	return ar;
};

var cdl = {
	lng : 'en',
	init : function() {
		if ($('.big_puff_slide').length) cdl.big_puff.init();
		if ($('.about_cdl').length) cdl.about_cdl.init();
		if ($('.look_book_slider').length) cdl.look.init();
		if ($('.catalog_products').length) cdl.catalog.init();
		//if ($('.quotes').length) cdl.quotes.init();
		if ($('.btn_sProduct').length) $('.btn_sProduct').trigger('click');
		if ($('.fld_input').length) cdl.newsletter.init();
		
		$('div[_aurl]').bind('click', function() {
			if (!this.getAttribute('_aurl')) return;
			document.location.href = '/catalog/'+ this.getAttribute('_aurl');
		});
		
		$('.coming_soon').bind('click', function() {
			
			return;
			$('.newsletter_overlay').css('height', $(document).height() +'px');
			$('.newsletter_overlay').fadeIn(300, function() {
				$('.newsletter_close').bind('click', function() {
					$('.newsletter_overlay').fadeOut(300, function() {});
				});
			});
		});

	},
	newsletter : {
		msg : 'Your e-mail address here...',
		init : function() {
			$('.fld_input #stay_updated').bind('focus', function() {
				if ($(this).val() == cdl.newsletter.msg) $(this).val('');
			});
			$('.fld_input #stay_updated').bind('blur', function() {
				if ($(this).val() == '') $(this).val(cdl.newsletter.msg);
			});
		},
		validate : function() {
			var email = $('#stay_updated').val();
			if (!email) {
				alert('Please enter an email address')
				return;
			}
			$.ajax({
				type: "POST",
				url: "/xhr",
				data: {email : email},
				success: function(msg) {
					if (msg == 0) alert('An error occurred. Please try again');
					if (msg == 2) {
						alert('Please specify a valid email address.');
						return;
					} else {
						if ($('.page_foot').length) {					
							$('.stay_updated, .fld_input, .btn_ok').fadeOut('slow', function() {
								$('.page_foot').fadeIn('slow').html('<div class="msg_success">'+ "Thanks! We'll keep in touch!" +'</div>');
							});
						} else {
							$('.stay_updated, .fld_input, .btn_ok').fadeOut('slow', function() {
								$('.newsletter_body').empty();
								$('.newsletter_body').append('<div class="msg_success">'+ "Thanks! We'll keep in touch!" +'</div>');				
								$('.newsletter_body').fadeIn('slow');
								setTimeout(function() {$('.newsletter_overlay').fadeOut(300)}, 3000);
							});
						}
					}
				}
			});
		}
	},
	big_puff : {
		anim : false,
		nexttime : 10000,
		step : 1,
		data : {
			1 : {
				bg 		  : '#f3f3f3 url(/resources/images/bg_01.jpg) 50% 0% no-repeat', /*a0d66e*/
				logo	  : 'cdl_logo',
				one_line  : 'one_liner',
				stay_up	  : 'stay_updated_black',
				find_style: 'black'
				//arrow : 'green'
			},
			2 : {
				bg  	  : '#c15842 url(/resources/images/bg_02.jpg) 50% 0% no-repeat',
				logo	  : 'cdl_logo',
				one_line  : 'one_liner_white',
				stay_up   : 'stay_updated_white',
				find_style: 'white'
				//arrow : 'white'
			},
			3 : {
				bg  	  : '#009bcd url(/resources/images/bg_03.jpg) 50% 0% no-repeat',
				logo	  : 'cdl_logo',
				one_line  : 'one_liner_white',
				stay_up   : 'stay_updated_white',
				find_style: 'black'
				//arrow : 'black'
			},
			4 : {
				bg  	  : '#000 url(/resources/images/bg_04.jpg) 0% 0% repeat-x',
				logo	  : 'cdl_logo',
				one_line  : 'one_liner_white',
				stay_up   : 'stay_updated_white',
				find_style: 'white'
				//arrow : 'white'
			},
			5 : {
				bg  	  : '#3A3028 url(/resources/images/bg_05.jpg) 50% 0% no-repeat', /*024768*/
				logo	  : 'cdl_logo_white',
				one_line  : 'one_liner_white',				
				stay_up   : 'stay_updated_white',
				find_style: 'black'
				//arrow : 'black'
			}
		},
		init : function() {
			var w = ($('.big_puff_slide .big_puff').length * 480) + 10;
			$('.big_puff_slide').css('width', w +'px');
			$('.big_puff_slide .big_puff').fadeIn(200, function() {
				$('.big_puff .ar_thin_right').fadeIn(200, function() {
					$('.big_puff_0'+ cdl.big_puff.step +' .txt_puff').css('opacity', '.1');
					$('.big_puff_0'+ cdl.big_puff.step +' .txt_puff').css('display', 'block');
					$('.big_puff_0'+ cdl.big_puff.step +' .txt_puff').animate({'left': '0px', 'opacity': '1'}, 400);
				});
				$('.big_puff .ar_thin_left').fadeIn(200, function() {
					$('.big_puff_0'+ cdl.big_puff.step +' .txt_puff').css('opacity', '.1');
					$('.big_puff_0'+ cdl.big_puff.step +' .txt_puff').css('display', 'block');
					$('.big_puff_0'+ cdl.big_puff.step +' .txt_puff').animate({'left': '0px', 'opacity': '1'}, 400);
				});
			});
			
			cdl.big_puff.next = setInterval('cdl.big_puff.go(1);', cdl.big_puff.nexttime);
		},
		go : function(step) {
			if (typeof(step) == 'object') {
				if (step.className.indexOf('thumb_overlay') > -1) return;
				var thumbs = $('.thumb_slide .thumb');
				for (var i=0; i<thumbs.length; i++) {
					if (thumbs[i] == step) break;
				}
				var step = i - cdl.big_puff.step + 1;
			}
			if (cdl.big_puff.anim) return;
			cdl.big_puff.anim = true;
			var old_step    = cdl.big_puff.step;
			var old_fys     = cdl.big_puff.data[old_step].find_style;
			var old_logo    = cdl.big_puff.data[old_step].logo;
			var old_stay_up = cdl.big_puff.data[old_step].stay_up;
			var old_one_line = cdl.big_puff.data[old_step].one_line;
			//var old_arrow_color = cdl.big_puff.data[old_step].arrow;
			
			cdl.big_puff.step += step;
			
			if (cdl.big_puff.step == 6) cdl.big_puff.step = 1;
			if (cdl.big_puff.step == 0) cdl.big_puff.step = 5;
			
			$('.big_puff_0'+ cdl.big_puff.step +' .txt_puff').css('left', '-15px');
			
			$('.thumb_overlay').fadeOut(200);
			
			var l = -(cdl.big_puff.step-1) * 480;
			$('.bg_overlay').css('background', cdl.big_puff.data[cdl.big_puff.step].bg);
			
			$('.big_puff_slide').animate({left: l +'px'}, 480, function() {
																		
				$('.big_puff_0'+ old_step +' .txt_puff').hide();
			
				$('.thumb_overlay').css('left', ((cdl.big_puff.step-1)*54 + (cdl.big_puff.step-1)*20) +'px');
				$('.thumb_overlay').fadeIn(200);

				$('.thumb_title').removeClass(old_fys +'_title');
				$('.thumb_title').addClass(cdl.big_puff.data[cdl.big_puff.step].find_style +'_title');
				
				// logo
				$('.page_head a.sprite').removeClass(old_logo);
				$('.page_head a.sprite').addClass(cdl.big_puff.data[cdl.big_puff.step].logo);

				// one liner
				$('.page_head a.sprite').next().removeClass(old_one_line);
				$('.page_head a.sprite').next().addClass(cdl.big_puff.data[cdl.big_puff.step].one_line);

				// stay updated
				//$('.page_foot .sprite').removeClass(old_stay_up);				
				//$('.page_foot .sprite').addClass('stay_updated '+cdl.big_puff.data[cdl.big_puff.step].stay_up);
				
				
				$('.big_puff_0'+ cdl.big_puff.step +' .txt_puff').css('opacity', '.1');
				$('.big_puff_0'+ cdl.big_puff.step +' .txt_puff').css('display', 'block');
				$('.big_puff_0'+ cdl.big_puff.step +' .txt_puff').animate({'left': '0px', 'opacity': '1'}, 400);
				cdl.big_puff.anim = false;
			});
			clearInterval(cdl.big_puff.next);
			cdl.big_puff.next = setInterval('cdl.big_puff.go(1);', cdl.big_puff.nexttime);
		}
	},
	about_cdl : {
		step : 1,
		init : function() {
			cdl.about_cdl.lion_uw();
		},
		lion_uw : function() {
			$('.lion_uw').fadeOut(500, function() {
				$('.lion_uw')[0].className = 'sprite lion_uw uw_0'+ cdl.about_cdl.step;
				$('.lion_uw').fadeIn(500, function() {
					cdl.about_cdl.step++;
					if (cdl.about_cdl.step > 9) cdl.about_cdl.step = 1;
					setTimeout(cdl.about_cdl.lion_uw, 2500);
				});
			});
		}
	},
	look : {
		anim : false,
		step : 1,
		init : function() {
			cdl.look.anim = true;
			$('.ar_thin_left').hide();
			$('.look_'+ cdl.look.step +' .one_liner').fadeIn(300, function() {
				cdl.look.anim = false;
			});
		},
		go : function(step) {
			if (cdl.look.anim) return;
			var old_step = cdl.look.step;
			cdl.look.anim = true;
			cdl.look.step += step;
			
			if (cdl.look.step >= 11) $('.ar_thin_right').fadeOut(300);
			if (cdl.look.step <= 1) $('.ar_thin_left').fadeOut(300);
			if (cdl.look.step > 1 && cdl.look.step < 11) $('.big_puff_ar').fadeIn(300);
			
			$('.look_'+ old_step +' .one_liner').fadeOut(100, function() {
				var l = -(cdl.look.step-1) * 980;
				$('.look_book_slider').animate({left: l +'px'}, 1200, function() {
					//$('.counter')[0].innerHTML = cdl.look.step +'/11';
					$('.look_'+ cdl.look.step +' .one_liner').fadeIn(300);
					cdl.look.anim = false;
				});
			});
		}
	},
	quotes : {
		showtime : 600,
		concurrent : 3,
		el : new Array(),
		showing : new Array(),
		que : new Array(),
		init : function() {
			cdl.quotes.divs = $('.quote');
			for (var i=0; i<cdl.quotes.divs.length; i++) {
				cdl.quotes.divs[i].className = cdl.quotes.divs[i].className +' q'+ (i+1);
				cdl.quotes.que.push('q'+ (i+1));
			}
			cdl.quotes.show(4);
		},
		show : function(j) {
			for (var i=0; i<j; i++) {
				//var qEl = cdl.quotes.que[Math.rnd(cdl.quotes.que.length)];
				var qEl = cdl.quotes.que[i];
				cdl.quotes.showing = [qEl].concat(cdl.quotes.showing);
				cdl.quotes.que = cdl.quotes.que.subtract(cdl.quotes.showing);
				cdl.quotes.place(qEl);
			}
			//console.log(cdl.quotes.que);
			setTimeout(function() {
				var rmv = cdl.quotes.showing.pop();
				$('.'+ rmv).fadeOut(1000, function() {
					cdl.quotes.que.push(rmv);
					cdl.quotes.el = cdl.quotes.el.subtract([this]);
					cdl.quotes.show(1);
				});
			}, 2000);
		},
		place : function(qEl) {
			var sw = $('.quotes').width();
			var sh = $('.quotes').height() - 60;
			
			var qEl = $('.'+ qEl);
			var ew = qEl.width();
			var eh = qEl.height();
			
			var tries = 1000;
			
			while (cdl.quotes.el.length < 300 && --tries > 0) {
				var lft = Math.max(0, Math.floor(Math.random()*sw) - ew);
				var top = Math.max(0, Math.floor(Math.random()*sh) - eh);
				var overlap = false;
				for (var z0=0; z0<cdl.quotes.el.length; z0++) {
					overlap = (lft > cdl.quotes.el[z0].offsetLeft - ew - 1
						&& lft < cdl.quotes.el[z0].offsetLeft + cdl.quotes.el[z0].offsetWidth + 1
						&& top > cdl.quotes.el[z0].offsetTop - eh - 1
						&& top < cdl.quotes.el[z0].offsetTop + cdl.quotes.el[z0].offsetHeight + 1);
					if (overlap) break;
				}
				if (overlap) continue;
				
				//$(qEl).css('top', (top+66) +'px');
				//$(qEl).css('left', lft +'px');
				$(qEl).fadeIn(300);
				
				cdl.quotes.el.push($(qEl)[0]);
				tries = 1000;
				break;
			}
		}
	},
	catalog : {
		xml : false,
		xsl : false,
		init : function() {
			
			// sort products color correction in safari
			if (xdbc.is_mac) {
				if (jQuery.browser.safari == true || jQuery.browser.mozilla == true) {
					$('.box_gray .box_body, .box_gray .box_tm, .box_gray .box_bm').addClass('sColor');
					$('.btn_sProduct .box_body, .btn_sProduct .box_tm, .btn_sProduct .box_bm').addClass('sColor');					
				}
			}

			xdbc.load('/resources/xsl/products.xsl', function(resp) {
				cdl.catalog.xsl = resp;
				xdbc.load('/resources/xml/products.xml', function(resp) {
					cdl.catalog.xml = resp;
					
					var url = window.location.href.split('/');
					if (url[4] && url[5]) {
						cdl.catalog.render();
						cdl.catalog.product();
					} else if (url[4]) {
						cdl.catalog.render();
						cdl.catalog.product();
					} else cdl.catalog.render();
					
				});
			});
		},
		render : function() {
			var caring = new Array('60C machine wash, No bleach, Medium heat ironing, No tumble dry', '40C machine wash, No bleach, Medium heat ironing, No tumble dry');
			xdbc.transform(cdl.catalog.xml, cdl.catalog.xsl, $('.catalog_body')[0]);
			
			$('.button.btn_more').bind('mouseover', function() {this.className = this.className.replace(/ hover/ig, '') +' hover';});
			$('.button.btn_more').bind('mouseout', function() {this.className = this.className.replace(/ hover/ig, '');});
			$('.button.btn_more, .prod_img').bind('click', function() {
				$('.catalog_overlay').css('height', $(document).height() +'px');
				$('.catalog_overlay')[0].innerHTML = '<div class="dropshadow_box_2x2 hideMe">&#160;</div>';
				
				var prod_box = this.parentNode.parentNode;
				var new_box = $('.catalog_overlay')[0].appendChild(prod_box.cloneNode(true));
				var offset = $(prod_box).offset();
				
				$(new_box).css('position', 'absolute');
				$(new_box).css('top', offset.top +'px');
				$(new_box).css('left', offset.left +'px');
				
				var l = parseInt((($(document).width()/2)-240)-offset.left);
				l = (l<0)? '-='+ Math.abs(l) : '+='+ Math.abs(l) ;
				var t = parseInt((($(window).height()/2)-240)-offset.top) + $(document).scrollTop();
				t = (t<0)? '-='+ Math.abs(t) : '+='+ Math.abs(t) ;

				$('.box_body *', new_box).css('display', 'none');
				$('.catalog_overlay').fadeIn(300, function() {
					$(new_box).animate({top: t +'px', left: l +'px', width: '480px'}, 400);
					$('.box_body', new_box).animate({height: '460px'}, 400, function() {
						
						var offset = $(new_box).offset();
						$('.dropshadow_box_2x2').css('top', (offset.top-37) +'px');
						$('.dropshadow_box_2x2').css('left', (offset.left-37) +'px');
						$('.dropshadow_box_2x2').fadeIn(900);
						
						var xProduct = cdl.catalog.xml.selectSingleNode('//*[@id="'+ new_box.getAttribute('pid') +'"]');
						var cdl_lining = (xProduct.getAttribute('cdl_lining') == 'Yes') ? 'CDL LINING' : '';
						
						var str = '<div class="sprite close_bb"><span>&#160;</span></div>'
									+'<img class="bp_img" src="/resources/images/prod/'+ xProduct.getAttribute('id') +'.jpg" alt=""/>'
									+'<div class="poslay bp_title"><b>'+ xProduct.getAttribute('theme') +'</b><br/>'+ xProduct.getAttribute('art_name') + '&#160;' + xProduct.getAttribute('id').substr(0,14) +'</div>'
									+'<a href="http://cdlselector.timarco.se/" target="_new" class="poslay bp_coming_soon"><span>&#160;</span></a>'									
									+'<div class="poslay bp_details_1">'
									+ xProduct.getAttribute('color_name') + '&#160;' + xProduct.getAttribute('color_nr') +'<br/>'
									+ xProduct.getAttribute('construction')
									+'</div>'
									+'<div class="poslay bp_details_2">'
									+ cdl_lining +'<br/>'
									+'</div>'
									+'<div class="poslay bp_sizes">Available in: <b>S <span>/</span> M <span>/</span> L <span>/</span> XL</b></div>'
									+'<div class="poslay bp_price"><b>SEK</b> '+ xProduct.getAttribute('price_sek') +',00 <span>/</span> <b>EUR</b> '+ xProduct.getAttribute('price_euro') +'</div>';
						/*						
						var str = '<div class="sprite close_bb"><span>&#160;</span></div>'
									+'<img class="bp_img" src="/resources/images/prod/'+ xProduct.getAttribute('id') +'.jpg" alt=""/>'
									+'<div class="poslay bp_title">'+ xProduct.getAttribute('theme') +'</div>'
									+'<div class="poslay bp_coming_soon">&#160;</div>'									
									+'<div class="poslay bp_details_1">'
									+'	<b>NAME:</b> '+ xProduct.getAttribute('art_name') +'<br/>'
									+'	<b>ART. NO:</b> '+ xProduct.getAttribute('id') +'<br/>'
									+'	<b>COLOUR:</b> '+ xProduct.getAttribute('color_name') +'<br/>'
									+'	<b>COLOUR NO:</b> '+ xProduct.getAttribute('color_nr') +'<br/>'
									+'</div>'
									+'<div class="poslay bp_details_2">'
									+'	<b>CONSTRUCTION:</b> '+ xProduct.getAttribute('construction') +'<br/>'
									+ cdl_lining +'<br/>'
									//+'	<b>CDL LININGS:</b> '+ xProduct.getAttribute('cdl_linings') +'<br/>'
									+'	<b>CARE INSTRUCTION:</b> '+ caring[xProduct.getAttribute('care_instruction')]
									+'</div>'
									+'<div class="poslay bp_sizes">S <span>/</span> M <span>/</span> L <span>/</span> XL</div>'
									+'<div class="poslay bp_price"><b>SEK</b> '+ xProduct.getAttribute('price_sek') +',00 <span>/</span> <b>EUR</b> '+ xProduct.getAttribute('price_euro') +'</div>';
						*/
						
						$('.box_body', new_box).html(str);
						$('.close_bb').bind('click', function() {
							$('.catalog_overlay').fadeOut(300, function() {
								
							});
						});
						
					});
				});
				return;
			});
		},
		filter : function() {
			var f = '';
			var opts = $('div[_aid]');
			for (var i=0, il=opts.length; i<il; i++) {
				if (opts[i].className.indexOf('active') == -1) continue;
				f += '@'+ opts[i].getAttribute('_aid') +'="'+ opts[i].getAttribute('_avalue') +'"';
				f += ' or ';
			}
			f = f.slice(0, -4);
			f = (f != '')? '['+ f +']' : '' ;
			
			var xFilter = cdl.catalog.xsl.selectSingleNode('//xsl:for-each');
			xFilter.setAttribute('select', '//i'+ f);
			
			cdl.catalog.render();
		},
		product : function() {
			var url = window.location.href.split('/');
			if (url[4] && url[5]) {
				var permalink = '/'+ url[4] +'/'+ url[5];
				var el = $('div[permalink='+ permalink +']');
				var img = el.children('.box_body').children().get(0);
				$(img).trigger('click');
			}
			if (url[4]) {
				var words = url[4].split('-');
				var theme = words[0].slice(0,1).toUpperCase() + words[0].substr(1, words[0].length - 1) +' '+ words[1].slice(0,1).toUpperCase() + words[1].substr(1, words[1].length - 1);
				var el = $('div[_avalue='+ theme +']');
				$(el).trigger('click');
			}
		}
	}
};

$(document).ready(function(){
	
	/*	INDEX PAGE	*/
	$('.big_puff .ar_thin_left').bind('click', function() {cdl.big_puff.go(-1);});
	$('.big_puff .ar_thin_right').bind('click', function() {cdl.big_puff.go(1);});
	$('.thumb_slide .thumb').bind('click', function() {
		cdl.big_puff.go(this);
	});
	
	$('.button.btn_ok').bind('mouseover', function() {this.className = this.className.replace(/ box_white/ig, '') +' box_gray';});
	$('.button.btn_ok').bind('mouseout', function() {this.className = this.className.replace(/ box_gray/ig, '') +' box_white';});
	$('.button.btn_ok').bind('click', function() {cdl.newsletter.validate();});
	
	/*	LOOKBOOK	*/
	$('.look_book .ar_thin_left').bind('click', function() {cdl.look.go(-1);});
	$('.look_book .ar_thin_right').bind('click', function() {cdl.look.go(1);});
	
	/*	ABOUT CDL	*/
	$('.languages .flags').bind('click', function() {
		var flags = $('.languages li');
		for (var i=0; i<flags.length; i++) {
			flags[i].className = '';
		}
		this.parentNode.className = 'active';
		
		var lng = this.className.match(/flag_../);
		$('.sprite_lng').fadeOut(500, function() {
			$('.sprite_lng')[0].className = 'sprite_lng lng_'+ lng[0].slice(-2);
			$('.sprite_lng').fadeIn(500);
		});
	});
	
	/*	OPTION CONTROL	*/
	$('.box_filter .option').bind('mouseover', function() {
		this.className = this.className.replace(/ hover/ig, '') +' hover';
	});
	
	$('.box_filter .option').bind('mouseout', function() {
		this.className = this.className.replace(/ hover/ig, '');
	});
	
	$('.box_filter .option').bind('click', function() {
		var cEl = $('.check', this)[0];
		var cElAll = $('.box_filter').children('.option').children('div');
		if (cEl.className.indexOf('active') == -1) {
			var colors = $('.f_colour').children('.sprite');			
			for (var i=0; i<colors.length; i++) {
				colors[i].className = colors[i].className.replace(/ active| hover/ig, '');
			}
			for (var i=0; i<$('.box_filter .option').length; i++) {
				cElAll[i].className = 'sprite check';
			}
			var cn = (cEl.className.indexOf('active') == -1)? ' active' : '';
			cEl.className = cEl.className.replace(/ active/ig, '') + cn;
			cdl.catalog.filter();
		} else {
			for (var i=0; i<$('.box_filter .option').length; i++) {
				cElAll[i].className = 'sprite check';
			}
			cdl.catalog.filter();
		}
	});
	
	/*	COLOUR CONTROL	*/
	$('.f_colour .colour').bind('click', function() {
		var colors = $('.f_colour').children('.sprite');
		if (this.className.indexOf('active') == -1) {
			var cElAll = $('.box_filter').children('.option').children('div');
			for (var i=0; i<$('.box_filter .option').length; i++) {
				cElAll[i].className = 'sprite check';
			}
			for (var i=0; i<colors.length; i++) {
				colors[i].className = colors[i].className.replace(/ active| hover/ig, '');
			}			
			var cn = (this.className.indexOf('active') == -1)? ' active' : '';
			this.className = this.className.replace(/ active| hover/ig, '') + cn;
			cdl.catalog.filter();
		} else {
			for (var i=0; i<colors.length; i++) {
				colors[i].className = colors[i].className.replace(/ active| hover/ig, '');
			}
			cdl.catalog.filter();
		}
		});
	
	$('.btn_sProduct').bind('click', function() {
		if ($('.btn_sProduct')[0].className.indexOf('box_gray') == -1) {
			$('.btn_sProduct').addClass('box_gray');
			$('.div_sProduct').animate({height: '150px'}, 300, function() {
				$('.sProduct').fadeIn(400, function() {
					$('.div_sProduct .top_right').show();
				});
			});
		} else {
			$('.div_sProduct .top_right').hide();
			$('.sProduct').fadeOut(400, function() {
				$('.div_sProduct').animate({height: '10px'}, 300, function() {
					$('.btn_sProduct').removeClass('box_gray');
				});
			});
		}
	});
	
	cdl.init();
});


/*	XDBC	*/
var xdbc = {
	is_ie6 : /msie 6/i.test(navigator.userAgent),
	is_ie : /msie/i.test(navigator.userAgent),
	is_gk : /gecko/i.test(navigator.userAgent),
	is_wk : /webkit/i.test(navigator.userAgent),
	is_win: /win|windows/i.test(navigator.userAgent),
	is_mac: /mac/i.test(navigator.userAgent),
	is_ax : typeof(ActiveXObject) == 'function',
	debug		: false,
	escStr		: ['"=$34;', '\'=$39;', '<=$60;', '>=$62;'],
	xslPath		: '/include/xsl/',
	flushQue	: function()	{xdbc.que = xdbc.load('<data></data>');},
	$			: function(s)	{return (typeof(s) == 'string')? document.getElementById(s):s;},
	uniqId : function (l) {
		var l = l || 16;
		var a = 'abcdefghijklmnopqrstuvwxyz_ABCDEFGHIJKLMNOPQRSTUVWXYZ-1234567890';
		var s = a.charAt((Math.random()*50));
		var d = new Date().valueOf();
		var k = Math.random().toString().slice(2);
		for (r=0; r<l; r++) s += a.charAt((Math.random()*256)%a.length);
		return s.slice(0,l);
	},
	init : function() {
		xdbc.prefix();
		xdbc.xslDoc = new Array();
		xdbc.flushQue();
	},
	load : function(xSrc, xCallback) {
		if (xSrc.indexOf('<') > -1) {
			if (xdbc.is_ax) {
				var xDom = new ActiveXObject(xdbc.otype['DomDocument']);
				if (xDom.loadXML(xSrc)) return xDom;
			} else if (xdbc.is_gk) {
				var xDom = new DOMParser().parseFromString(xSrc, 'text/xml');
				if (xDom.firstChild.nodeName != 'parsererror') return xDom;
			}
			xdbc.ctrlXml(false, xSrc);
		} else {
			var xhr = {
				src		 : xSrc,
				callback : (typeof(xCallback) == 'function')? xCallback : null,
				async	 : (typeof(xCallback) != 'undefined'),
				http	 : xdbc.is_ax? new ActiveXObject(xdbc.otype['XmlHttp']) : new XMLHttpRequest()
			};
			xhr.http.open('GET', xSrc +'?'+ xdbc.uniqId(), xhr.async);
			xhr.http.send('');
			if (xhr.async) {
				if (xhr.http.readyState == 4) {
					xhr.dom = xdbc.ctrlXml(xhr.http.responseXML, xhr.src);
					if (xhr.callback) xhr.callback(xhr.dom);
					return xhr;
				} else {
					xhr.http.onreadystatechange = function() {
						if (xhr.http.readyState != 4) return;
						xhr.dom = xdbc.ctrlXml(xhr.http.responseXML, xhr.src);
						if (xhr.callback) xhr.callback(xhr.dom);
					}
					return xhr;
				}
			} else return xdbc.ctrlXml(xhr.http.responseXML, xhr.src);
		}
	},
	ctrlXml : function(xObj, xSrc) {
		xSrc = xSrc || '';
		if (!xdbc.is_ie && xObj == null || xObj.xml.indexOf('<parsererror') > 0) xObj = false;
		return (typeof(xObj) == 'object' && xObj.xml != '')? xObj : xdbc.doError('Invalid XML structure:\n'+ xSrc);
	},
	prefix : function() {
		xdbc.otype = new Array();
		var t = ['DomDocument', 'XmlHttp'];
		var b = ['Microsoft', 'MSXML', 'MSXML2', 'MSXML3'];
		for (n=0; n<t.length; n++) {
			for (p=0; p<b.length; p++) {
				try {
					new ActiveXObject(b[p] +'.'+ t[0]);
					xdbc.otype[t[n]] = b[p] +'.'+ t[n];
				}
				catch (e) {}
			}
		}
	},
	xslRam : function($src, $callback) {
		if (xdbc.xslDoc[$src]) return xdbc.xslDoc[$src];
		else {
			var callback = $callback || true;
			xdbc.xslDoc[$src] = xdbc.load($src, callback);
			return xdbc.xslDoc[$src];
		}
	},
	htmlChar : function(sFrag) {
		sFrag = sFrag.replace(/<\?xml.+?\?>/i, '');
		sFrag = sFrag.replace(/&amp;/ig, '&');
		sFrag = sFrag.replace(/&lt;/ig, '<');
		sFrag = sFrag.replace(/&gt;/ig, '>');
		return sFrag;
	},
	transform : function($xml, $xsl, $el, $callback) {
		if (typeof($xml) == 'string') $xml = xdbc.load($xml, true);
		if (typeof($xsl) == 'string') $xsl = xdbc.xslRam($xsl);
		if (!$xml.dom && $xml.documentElement || $xml.xml) $xml = {dom:$xml}
		if (!$xsl.dom && $xsl.documentElement) $xsl = {dom:$xsl}
		if (!$xml.dom || !$xsl.dom) {
			setTimeout(function() {
				xdbc.transform($xml, $xsl, $el, $callback);
			}, 500);
			return;
		}
		xdbc.transform.recentXML = $xml.dom;
		$el = xdbc.$($el);
		if (xdbc.is_ax) {
			var frg = $xml.dom.transformNode($xsl.dom.documentElement);
			if (typeof($el) == 'function') $el(xdbc.htmlChar(frg));
			else $el.innerHTML = xdbc.htmlChar(frg);
		} else {
			var xslPrc = new XSLTProcessor();
			xslPrc.importStylesheet($xsl.dom);
			var frg = xdbc.htmlChar(xslPrc.transformToFragment($xml.dom, document).xml);
			
			if (typeof($el) == 'function') $el(frg);
			else {
				var rng = $el.ownerDocument.createRange();
				rng.setStartBefore($el);
				$el.innerHTML = '';
				$el.appendChild(rng.createContextualFragment(frg));
			}
		}
		if ($callback) {
			var $cb_param = ($callback.indexOf('(') > -1)? '' : '(xdbc.transform.recentXML);';
			try {eval($callback + $cb_param);}
			catch (e) {/* REPORT TO DOERROR? */};
		}
		$xml = $xsl = null;
	},
	escape : function($iStr) {
		for ($e=0; $e<xdbc.escStr.length; $e++) 
			$iStr = $iStr.replace(new RegExp(xdbc.escStr[$e].slice(0,1), 'g'), xdbc.escStr[$e].slice(2));
		return $iStr;
	},
	unescape : function(xml) {
		var xAll = xml.selectNodes('//*');
		for (x=0; x<xAll.length; x++) {
			if (!xAll[x].attributes) continue;
			for (a=0; a<xAll[x].attributes.length; a++) {
				xVal = unescape(xAll[x].attributes.item(a).value);
				xAll[x].attributes.item(a).value = xVal;
			}
		}
	}
}
xdbc.init();
