$ (function() {

	$('a').click(function(){this.blur();});
	
	$('.grid').hide();
	$('#set_01').show();
	
	// Position property menu
	var listSpaceRemaining = $('#properties').height() - $('.items').height();
	var listPosition = listSpaceRemaining / 2;
	$('.items').css('margin-top', listPosition);
	
	/* popUp */
	
	$(".video").click(function(){ 
		var ref = this.href;
		popUp(ref,'Video','560','480','yes');
		return false;
	});
	
	// Hover + Click settings
	
	// Primary nav rollover image swaps (need to add cacheing)
	$('#header ul li a img').hover(
		function () {
			//alert(this.src.split('_o.gif')[1]);
			if(!$(this).parent().parent().hasClass('selected')){
				var imageLoc = this.src.split('.gif')[0] + '_o.gif';
				$(this).attr({src: imageLoc});
			}
		}, 
		function () {
			if(!$(this).parent().parent().hasClass('selected')){
				var imageLoc = this.src.split('http://www.21stcenturygroup.co.uk/_o.gif')[0] + '.gif';
				$(this).attr({src: imageLoc});
			}
		}
	);
	
	// left hand properties navigation rolovers + clicks
	$('#properties .item').hover(
		function () {
			if(!$(this).hasClass('selected')){
				$('a, p', this).css({color: '#fff'});
			}
		}, 
		function () {
			if(!$(this).hasClass('selected')){
				$('a, p', this).css({color: '#94948F'});
			}
		}
	);
	
	$('#properties .item').click(
		function(){
			if(!$(this).hasClass('selected')){
				var ref = $('a', this).attr("href");
				document.location.href = ref;
			}
		}
	);
	
	
	
	// ImageViewer
	
	var curr_num = 1;
	var buttonNext = '#images_nav .next';
	var buttonPrevious = '#images_nav .previous';
	var setName = '#set_0';
	
	$('#overlay').fadeOut('slow');
	
	$('#images .grid .row a').click(
		function(){
			var ref = $(this).attr("href");
			var path_prefix = ref.split('img')[0];
			var multi_shot = null;
			//document.location.href = ref;
			//alert(ref)
			if($(this).hasClass('after')){
				multi_shot = 'after';
			}
			var so = new SWFObject(path_prefix+"swf/imageViewer.swf", "imageViewer", "527", "543", "6");
			so.addParam("scale", "noscale");
			so.addParam("wmode", "transparent");
			so.addVariable("ref", ref);
			so.addVariable("multi_shot", multi_shot);
			so.write("image");
			return false;
		}
	);
	
	$(buttonNext).click(function(){
		var buttPressed = $(this);
		var next_num = curr_num+1;
		var nextSet = $(setName+next_num);
		if(nextSet.length){
			$('#overlay').fadeIn(function(){
				$(setName+curr_num).hide();
				$(setName+next_num).show(function(){
					$('#overlay').fadeOut('slow');
					//$('#set_num').html(next_num);
					curr_num = next_num;
					checkPosition(buttPressed, curr_num, 'up');
				});
			});
		};
	});
	$(buttonPrevious).click(function(){
		var buttPressed = $(this);
		var next_num = curr_num-1;
		if(curr_num > 1){
			$('#overlay').fadeIn(function(){
				$(setName+curr_num).hide();
				$(setName+next_num).show(function(){
					$('#overlay').fadeOut('slow');
					//$('#set_num').html(next_num);
					curr_num = next_num;
					checkPosition(buttPressed, curr_num, 'down');
				});
			});
		};
	});
	
	function checkPosition(buttPressed, curr_num, direction){
		
		if (direction == 'down'){
			var prevNum = curr_num - 1;
			var prevSet = $(setName+prevNum);
			$(buttonNext).css({color: '#DBDBD9'});
			if(!prevSet.length) {
				buttPressed.css({color: '#94948F'});
			};
		};
	
		if (direction == 'up'){
			var nextNum = curr_num + 1;
			var nextSet = $(setName+nextNum);
			$(buttonPrevious).css({color: '#DBDBD9'});
			if(!nextSet.length){
				buttPressed.css({color: '#94948F'});	
			};
		};

	};
	
	var nextNum = curr_num + 1;
	var nextSet = $(setName+nextNum);
	if(!nextSet.length){
		$(buttonNext).css({color: '#94948F'});	
	};
	$(buttonPrevious).css({color: '#94948F'});

});


var win= null; // (screen.width-w)/2;
function popUp(mypage,myname,w,h,scroll){
  var winl = 50;
  var wint = 50;
  var settings  ='height='+h+',';
      settings +='width='+w+',';
      settings +='top='+wint+',';
      settings +='left='+winl+',';
      settings +='scrollbars='+scroll+',';
      settings +='resizable=no';
  win=window.open(mypage,myname,settings);
  if(parseInt(navigator.appVersion) >= 4){win.window.focus();}
}

