jQuery(document).ready(function() {
	if (j1count>5) jQuery('#j1').jcarousel();
	if (j2count>5) jQuery('#j2').jcarousel({vertical: true, scroll: 1});
	$('#m1 a').click(function(obj) {
		var name = $(obj).attr('rel');
		var name = $(this).attr('href');
		name = name.substr(0,name.length-5);
		$.getJSON('/content/menu.txt', function(data) {
			$.each(data, function(key, value) { 
				if (key==name) {
					$('#j1').fadeOut(200, function(){
						$('#j1c').html('');
						$('#j1c').append("<ul id='j1'></ul>");
						for (i=0; i<value.items.length; i++)  {
							if (i>0) {
								li = "<li><a href='"+value.items[i]+".html' rel='"+value.items[i]+"' title='"+value.names[i]+"'><img src='/content/"+value.items[i]+".jpg' width='100' height='64' alt='"+value.names[i]+"' /></a></li>";
							} else {
								li = "<li class='active'><a href='"+value.items[i]+".html' rel='"+value.items[i]+"' title='"+value.names[i]+"'><img src='/content/"+value.items[i]+".jpg' width='94' height='58' alt='"+value.names[i]+"' /></a></li>";			
							}
							$('#j1').append(li);
							counter = i;
						}
						$('#j1').fadeIn(300);
						if (counter>4) { 
							jQuery('#j1').jcarousel();
						} else {
						}
						$('#j1 a').click(function(obj) {j1_change(this);return false;});
						 j1_change($('#j1 li.active a'));
					});
				}
			});
		});
		$('#m1 li.active').removeClass('active');
		$(this).parent().parent().addClass('active');
		return false;
	});
	
	$('#j1 a').click(function(obj) {
		j1_change(this);
		return false;
	});

	$('#j2 a').click(function(obj) {
		j2_change(this);
		return false;
	});
  
});


function j1_change(obj) {
		var name = $(obj).attr('rel');
		var counter = 0;
		$('#j1 li.active img').attr('width','100').attr('height','64');	
		$('#j1 li.active').removeClass('active');
		$(obj).children('img').attr('width','94').attr('height','58');
		$(obj).parent().addClass('active');
		if (name) {
		  $('#darken').fadeIn('200');
		  b = '/content/'+name+'1_b.jpg';
		  $.getJSON('/content/'+name+'.txt', function(data) {
			$('#model_title').html(data.title);
			$('#model_info').html(data.info);
			$('#subm').fadeOut('10', function(){
				$('#subm').html('');
				$('#subm').append("<ul id='j2'></ul>");
				$.each(data.images, function(i,item){
					if (i>0) {
						li = "<li><a href='/content/"+name+item+"_b.jpg' target='_blank'><img src='/content/"+name+item+".jpg' width='100' height='64' alt='' /></a></li>";
					} else {
						li = "<li class='active'><a href='/content/"+name+item+"_b.jpg' target='_blank'><img src='/content/"+name+item+".jpg' width='116' height='74' alt='' /></a></li>";				
					}
					$('#j2').append(li);
					counter = i;
               			});
				$('#j2 a').click(function(obj) { j2_change(this); return false;	});
				$.preloadImages([b], function () {
					$('#centre').css("background-image","url('"+b+"')");
					$('#subm').fadeIn('100');               			
					$('#darken').fadeOut('200');				
					if (counter>4) jQuery('#j2').jcarousel({vertical: true, scroll: 1});
				});

               		});
			//if (counter>4) 
			
		  });
		}

		return false;
}
function j2_change (obj) {
		if ($(obj).parent().hasClass('active')) return false;
		b = $(obj).attr('href');
		$('#j2 li.active img').animate({width:100, height:64}, 200);
		$('#j2 li.active').removeClass('active');
		$(obj).children('img').animate({width:116, height:74}, 200);
		$(obj).parent().addClass('active');
		$('#darken').fadeIn('300');
		$.preloadImages([b], function () {
        		$('#centre').css("background-image","url('"+b+"')");
			$('#darken').fadeOut('200');			
   		 });
		return false;
}

function fixPNG(element) {
if (/MSIE (5\.5|6).+Win/.test(navigator.userAgent)) {
	var src;
	if (element.tagName=='IMG') {
		if (/\.png$/.test(element.src)) {
			src = element.src; 
			element.src = "/images/1x1.gif";
		}
	} else {
		src = element.currentStyle.backgroundImage.match(/url\("(.+\.png)"\)/i)
		if (src) {
			src = src[1];
			element.runtimeStyle.backgroundImage="none";
		}
	}
	if (src) element.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "',sizingMethod='scale')";
}
}

jQuery.preloadImages = function () {
    if (typeof arguments[arguments.length - 1] == 'function') {
        var callback = arguments[arguments.length - 1];
    } else {
        var callback = false;
    }
    if (typeof arguments[0] == 'object') {
        var images = arguments[0];
        var n = images.length;
    } else {
        var images = arguments;
        var n = images.length - 1;
    }
    var not_loaded = n;
    for (var i = 0; i < n; i++) {
        jQuery(new Image()).load(function() {
            if (--not_loaded < 1 && typeof callback == 'function') {
                callback();
            }
        }).attr('src', images[i]);
    }
}
