kubis = {

	products : {
		
		load : true,
		menuLoad : true,
		loading : false,
		initialised : false,
		parent_id : null,
		child_id : null,
		menu : 'categories',
		section : 'products',
		menu1 : 'categories',
		menu2 : 'ranges',

		restart : function(){
			this.load = true;
			this.init();
		},
		
		init : function(){
			
			if(this.load){
				$('#photoHolder a.code').click(function(){
													$(this).blur();
													var href = $(this).attr('href');
													if(!href.match(/^#/)){
														href = href.match(/(#\d+)$/)[1];
													}
													kubis.products.child(href);
													return false;
												});
			}
			
			if(this.menuLoad){
				//alert(1);
				$('#menu label').click(function(){
											$(this).blur();
											kubis.products.parent('#'+$(this).parent().attr('rel'));
											return false;
										});
										
				$('#menu a').click(function(){
									$(this).blur();
									kubis.products.child($(this).attr('rel'));
									return false;
								});
			}
		
			if(this.initialised)
				return;
				
			$('#gallery .header .view a').click(function(){
														$(this).blur();
														if(kubis.products.loading){
															return false;
														}
														if(!$(this).attr('class').match(/active/)){
															$('#gallery .header .view a').toggleClass('active');
															kubis.products.view($(this).attr('rel'));
														}
														return false;
													});
			
			this.initialised = true;
			this.load = false;
			this.menuLoad = false;
		},
		
		view : function(view){
			if(this.loading)
				return;
			if(view!=this.menu1 && view!=this.menu2)
				return;
			this.loading = true;
			this.menu = view;
			
			$('#loader').show();
			$('#menuHolder').hide();
			$('#photoHolder').hide();
			$('#thumbnails .arrow').hide();
			$('#thumbnailHolder').hide();
			$('#parentName').hide();
			$('#enquiry').hide();
			
			$.getJSON("ajax/"+kubis.products.section+"/menu;"+kubis.products.section+"/first-photo;"+kubis.products.section+"/thumbnails;"+kubis.products.section+"/parent;"+kubis.products.section+"/widget1;"+kubis.products.section+"/widget2;"+kubis.products.section+"/enquiry",
						{'menu' : view},
						function(json){
							$('#menuHolder').html(json[0]);
							$('#photoHolder').html(json[1]);
							$('#thumbnailHolder').html(json[2]);
							$('#parentName').html(json[3]);
							$('#rangeInfo .box').eq(0).html(json[4]);
							$('#rangeInfo .box').eq(1).html(json[5]);
							$('#enquiry select').html(json[6]);
							
							$(function(){
								$('#menuHolder').show();
								$('#photoHolder').show();
								$('#thumbnails .arrow').show();
								$('#thumbnailHolder').show();
								$('#parentName').show();
								$('#enquiry').show();
								$('#loader').hide();
								kubis.products.loading = false;
								kubis.products.menuLoad = true;
								kubis.restart();
							});
						});
		},
		
		child : function(id){
			if(this.loading){
				return;
			}
			var match = id.match(/^#(\d+)$/);
			if(!match){
				return;
			}
			else if(this.child_id==match[1]){ //already in the child
				return;
			}
			else {
				this.child_id = id = match[1];
			}
			
			this.loading = true;

			$('#menu ul ul li a').removeClass('selected');
			$('#menu li[rel='+this.parent_id+'] a[rel=#'+id+']').addClass('selected');
			
			$('#loader').show();
			$('#photoHolder').hide();
			$('#thumbnails .arrow').hide();
			$('#thumbnailHolder').hide();
			$('#parentName').hide();
			$('#enquiry').hide();
			$('#rangeInfo .box').eq(1).html('');
			
			$.getJSON("ajax/"+kubis.products.section+"/first-photo;"+kubis.products.section+"/thumbnails;"+kubis.products.section+"/parent;"+kubis.products.section+"/widget1;"+kubis.products.section+"/widget2;"+kubis.products.section+"/enquiry",
						{'menu' : this.menu, 'parent' : this.parent_id, 'child' : this.child_id},
						function(json){
							$('#photoHolder').html(json[0]);
							$('#thumbnailHolder').html(json[1]);
							$('#parentName').html(json[2]);
							$('#rangeInfo .box').eq(0).html(json[3]);
							$('#rangeInfo .box').eq(1).html(json[4]);
							$('#enquiry select').html(json[5]);
							
							$(function(){
								$('#photoHolder').show();
								$('#thumbnails .arrow').show();
								$('#thumbnailHolder').show();
								$('#parentName').show();
								$('#enquiry').show();
								$('#loader').hide();
								kubis.products.loading = false;
								var parent_id = kubis.products.parent_id;
								kubis.restart();
								kubis.products.setParent(parent_id);
							});
						});
		},
		
		parent : function(id){
			if(this.loading){
				return;
			}
			var match = id.match(/^#(\d+)$/);
			if(!match){
				return;
			}
			else if(this.parent_id==match[1] && this.child_id==null){ //already in the parent
				return;
			}
			else {
				this.parent_id = id = match[1];
				this.child_id = null;
			}
			
			this.loading = true;
			
			$('#menu ul ul li a').removeClass('selected');
			
			$('#loader').show();
			$('#photoHolder').hide();
			$('#thumbnails .arrow').hide();
			$('#thumbnailHolder').hide();
			$('#enquiry').hide();
			$('#parentName').hide();
			
			$.getJSON("ajax/"+kubis.products.section+"/first-photo;"+kubis.products.section+"/thumbnails;"+kubis.products.section+"/parent;"+kubis.products.section+"/widget1;"+kubis.products.section+"/widget2;"+kubis.products.section+"/enquiry",
						{'menu' : this.menu, 'parent' : this.parent_id},
						function(json){
							$('#photoHolder').html(json[0]);
							$('#thumbnailHolder').html(json[1]);
							$('#parentName').html(json[2]);
							$('#rangeInfo .box').eq(0).html(json[3]);
							$('#rangeInfo .box').eq(1).html(json[4]);
							$('#enquiry select').html(json[5]);
							
							$(function(){
								$('#photoHolder').show();
								$('#thumbnails .arrow').show();
								$('#thumbnailHolder').show();
								$('#parentName').show();
								$('#enquiry').show();
								$('#loader').hide();
								kubis.products.loading = false;
								var parent_id = kubis.products.parent_id;
								kubis.restart();
								kubis.products.setParent(parent_id);
							});
						});
		},
		
		setParent : function(id){
			if(!id.match(/^(\d+)$/)){
				return;
			}
			this.parent_id = id;
			$('#menuHolder>ul>li').each(function(){
											if($(this).attr('rel')==kubis.products.parent_id){
												$(this).removeClass('closed');
												$(this).children('ul').show();
											}
											else {
												$(this).addClass('closed');
												$(this).children('ul').hide();
											}
										});
		}
	
	},
	
	gallery : {
		
		load : true,
		menu : 0,
		initialised : false,
		
		restart : function(){
			this.load = true;
			this.init();
		},
		
		init : function(){
			
			var categories = $('#gallery #menuHolder>ul>li');
			categories.each(function(i){
								if(i>0){
									$(this).addClass('closed');
									$(this).children('ul').hide();
								}
								else {
									kubis.products.parent_id = $(this).attr('rel');
									$(this).removeClass('closed');
									$(this).children('ul').show();
								}
							});
			
			$('#gallery #menuHolder>ul label').css('cursor', 'pointer');
			
			if(!this.load)
				return;
			
			$('#gallery #menuHolder>ul label').click(function(){
													var li = $(this).parent();
													if(li.attr('class').match(/closed/)){ //close others and open this
														$('#parentName').html($(this).html());
														$('#loader').show();
													
														var rel = li.attr('rel');
														
														var others = $('#gallery #menu ul li[rel!='+rel+'] ul');
														//others. //IE 7 stuffs up alignment otherwise
														li.children('ul').removeClass('closing');
														
														others.each(function(i){
															if($(this).parent().attr('rel')!=(rel-1))
																$(this).addClass('closing');
															$(this).slideUp("slow", function(){
																							$('#gallery #menu ul li[rel!='+rel+']').addClass('closed');
																						});
														});
														
														/*others.slideUp("slow", function(){
																						$('#gallery #menu ul li[rel!='+rel+']').addClass('closed');
																					});*/
														li.children('ul').slideDown();
														li.removeClass('closed');
													}
													else { return; //close this (ignored for now)
														li.children('ul').addClass('closing'); //IE 7 stuffs up alignment otherwise
														li.children('ul').slideUp("slow", function(){
																							li.addClass('closed');
																							li.children('ul').removeClass('closing');
																						});
													}
												});			
			
			this.initialised = true;
			this.load = false;
			
		}
	
	},
	
	slider : {
		
		load : true,
		total : 0,
		current : 0,
		shift : 0,
		initialised : false,
	
		restart : function(){
			this.load = true;
			this.init();
		},
		
		init : function(){
			if($('#slider').length==0) {
				return;
			}
			
			this.total = $('#slider .product').length;
			
			var product = $('#slider .product').eq(0);
			this.shift = this.nopx(product.css('height')) + this.nopx(product.css('margin-bottom'));
			
			if(this.load){
				$('#sliderHolder a').click(function(){
												$(this).blur();
												kubis.products.menu = "ranges";
												$('#gallery .header .view a').toggleClass('active');
												
												$('#menuHolder').hide();
												
												var rel = $(this).attr('rel');
												rel = rel.substring(rel.indexOf('#')+1, rel.length)
												kubis.products.parent_id = rel;
																								
												var href = $(this).attr('href');
												href = href.substring(href.indexOf('#')+1, href.length);
												kubis.products.child('#'+href);
												
												$.getJSON("ajax/"+kubis.products.section+"/menu",
														{'menu' : 'ranges'},
														function(json){
															$('#menuHolder').html(json[0]);
															
															$(function(){
																$('#menuHolder').show();
																kubis.products.menuLoad = true;
																kubis.restart();
																kubis.products.setParent(rel);
																$('#menuHolder a').removeClass('selected');
																$('#menuHolder a[rel=#'+href+']').addClass('selected');
															});
														});
														
												return false;
											});
			}
			
			if(this.total>3){
				$('#arrow_down').show();
				$('#arrow_up').show();
				
				if(this.initialised)
					return;
					
				this.current = 0;
				
				$('#arrow_down').mouseover(function(){
												$(this).children('img').eq(0).attr('src', 'images/arrow-down_hover.jpg');
											});
				$('#arrow_down').mouseout(function(){
												$(this).children('img').eq(0).attr('src', 'images/arrow-down.jpg');
											});
				$('#arrow_up').mouseover(function(){
												$(this).children('img').eq(0).attr('src', 'images/arrow-up_hover.jpg');
											});
				$('#arrow_up').mouseout(function(){
												$(this).children('img').eq(0).attr('src', 'images/arrow-up.jpg');
											});							
				
				if(!this.load)
					return;
				
				$('#arrow_down').click(function(){
											kubis.slider.next();
											$(this).blur();
											return false;
										});
										
				$('#arrow_up').click(function(){
											kubis.slider.prev();
											$(this).blur();
											return false;
										});
			}
			else {
				$('#arrow_down').hide();
				$('#arrow_up').hide();
			}
			this.initialised = true;
			this.load = false;
		},
		
		next : function(){
			if(this.current<(this.total-3)){
				this.move(this.current+1);
			}
		},
		
		prev : function(){
			if(this.current>0){
				this.move(this.current-1);
			}
		},
		
		move : function(num){
		
			num = Math.min(this.total-3, Math.max(0, num));
		
			if(num==0){
				//$('#arrow_up').hide();
			}
			else {
				//$('#arrow_up').show();
			}
			if(num==(this.total-3)){
				//$('#arrow_down').hide();
			}
			else {
				//$('#arrow_down').show();
			}
			
			$('#sliderHolder').animate({top : -num*this.shift+'px'});
			
			this.current = num;
		},
		
		nopx : function(val){
			return kubis.nopx(val);
		}
		
	},
	
	thumbnails : {
		
		load : true,
		total : 0,
		current : 0,
		shift : 0,
		largeShift : 0,
		largeCurrent : 0,
		buffer : false,
		moving : false,
		initialised : false,
		active : 0,

		restart : function(){
			this.load = true;
			this.active = 0;
			this.init();
			this.move(0);
			this.largeCurrent = 0;
		},
	
		init : function(){
			this.total = $('#thumbnails .thumb').length;
			
			//preload the large images
			var largePath = $('#photoHolder .photo').eq(0).children('img').eq(0).attr('src').match(/(.*?)([^/]*)$/i)[1];
			$('#thumbnails .thumb img').each(function(){
													if(!$(this).hasClass('overlay')){
														var smallSrc = $(this).attr('src').match(/([^/]*)$/i)[1];
														var largeSrc = largePath+smallSrc;
														var newLarge = $("<img>").attr("src", largeSrc);
														newLarge.css("display", "none");
														$('body').append(newLarge);
													}
												});
			
			$('#thumbnails .thumb img.overlay').each(function(i){
														if(i>0)
															$(this).attr('src', 'images/thumbnail_inactive.png');
													});
			
			var thumb = $('#thumbnails .thumb').eq(0);
			this.shift = this.nopx(thumb.css('width')) + this.nopx(thumb.css('margin-right'));
			$('#thumbnailHolder').css('width', this.shift*this.total+'px');
			
			this.largeShift = this.nopx($('#photoContent').css('width'));
			
			if(this.load){
				this.current = 0;
				$('#thumbnailHolder').css('left', '0px');
				
				$('#thumbnails .thumb a').click(function(){
													var num = $(this).attr('href');
													num = num.substring(num.indexOf('#')+1, num.length);
													kubis.thumbnails.showLarge(num);
												});
												
				$('#thumbnails .thumb a').click(function(){ return false; });
			}
			
			if(this.total>5){
				$('#thumbnails .arrow').show();
			}
			else {
				$('#thumbnails .arrow').hide();
			}
			
			if(this.initialised)
				return;
			
			$('#arrow_left').click(function(){
										$(this).blur();
										kubis.thumbnails.prev();
										return false;
									});
									
			$('#arrow_right').click(function(){
										$(this).blur();
										kubis.thumbnails.next();
										return false;
									});
			
			if(this.initialised)
				return;
				
			this.initialised = true;
			this.load = false;
			
		},
		
		next : function(){
			this.move(this.current+5);
		},
		
		prev : function(){
			this.move(this.current-5);
		},
		
		move : function(num){
			num = Math.max(0, Math.min(this.total-5, num));
			$('#thumbnailHolder').animate({'left' : -num*this.shift+'px'}, 'slow');
			
			if(num==0){
				$('#arrow_left').children('img').attr('src', 'images/arrow-left_small_inactive.png');
			}
			else {
				$('#arrow_left').children('img').attr('src', 'images/arrow-left_small_active.png');
			}
			
			if(num==this.total-5){
				$('#arrow_right').children('img').attr('src', 'images/arrow-right_small_inactive.png');
			}
			else {
				$('#arrow_right').children('img').attr('src', 'images/arrow-right_small_active.png');
			}
			
			this.current = num;
		},
		
		showLarge : function(num){
			if(this.moving!==false){
				this.buffer = num;
				return;
			}

			num = Math.max(0, Math.min(this.total, num));
			if(num==this.largeCurrent) return;

			this.moving = num;
			this.buffer = false;
			this.largeCurrent = num;
			$('#loader').show();

			var thumbnails = $('#thumbnailHolder img.overlay');
			thumbnails.attr('src', 'images/thumbnail_inactive.png');
			thumbnails.eq(num).attr('src', 'images/thumbnail_active.png');
			
			var show = $('#thumbnailHolder a').eq(num);
			var src = show.children('img').eq(0).attr('src').match(/([^/]*)$/i)[1]; //get everything after the last /
			var rel = show.attr('rel');
			var match = rel.match(/^{(.*?)}(\d+)/);
			if(match){
				rel = "Click here to view this "+match[1];
				var href = "#"+match[2];
			}
	
			var photos = $('#photoHolder .photo');
			var current = photos.eq(0); 
			var next = photos.eq(1);
			
			next.children('.code').html(rel);
			if(match){
				next.children('.code').attr('href', href);
			}
			
			var currentSrc = current.children('img').eq(0).attr('src').match(/([^/]*)$/i)[1]; //as above
			var path = current.children('img').eq(0).attr('src');
			path = path.substring(0, path.length-currentSrc.length);
			next.children('img').eq(0).attr('src', path+src);
			
			$(function(){
				$('#loader').hide();
				
				if(true || kubis.thumbnails.active<num){
					var start = 0;
					var end = -kubis.thumbnails.largeShift;
					var before = $('#photoHolder .num0');
					var after = $('#photoHolder .num1');
					var swapBack = false;
				}
				else {
					var start = -kubis.thumbnails.largeShift;
					var end = 0;
					var before = $('#photoHolder .num1');
					var after = $('#photoHolder .num0');
					var swapBack = true;
				}
				
				//before.insertBefore(after); //swap images accordingly for left/right animation
				
				$('#photoHolder').css('left', start+'px');
				
				$('#photoHolder').animate({'left' : end+'px'}, "slow", function(){
					
					if(swapBack){
						before.insertAfter(after); //swap back
					}
					kubis.thumbnails.active = num;
					
					var photos = $('#photoHolder .photo');
					var current = photos.eq(1); 
					var old = photos.eq(0);
					
					//swap the info over to the old one
					old.children('.code').html(current.children('.code').html());
					old.children('.code').attr('href', current.children('.code').attr('href'));
					old.children('img').attr('src', current.children('img').attr('src'));

					
					//move it back
					//$(this).css('left', '0px');
					
					kubis.thumbnails.moving = false;
					if(kubis.thumbnails.buffer){
						kubis.thumbnails.showLarge(kubis.thumbnails.buffer);
					}
				});
			});
			
		},
		
		nopx : function(val){
			return kubis.nopx(val);
		}
	
	},
	
	nopx : function(val){
		var len = val.length;
		return Math.round(val.substring(0, len-2));	
	},
	
	init : function(){
		this.gallery.init();
		this.slider.init();
		this.thumbnails.init();
		this.products.init();
	},
	
	restart : function(){
		this.gallery.restart();
		this.slider.restart();
		this.thumbnails.restart();
		this.products.restart();
	}
	
}

$(function(){
	kubis.init();
});
