// JavaScript Document
	
	var imglock = 0;
	
	// NEW get attributes from image names
	function getAttributes(name){
		attr = new Object;
		var temp = name.split('/');
		var temp2 = temp[temp.length-1].split('.');
		var temp3 = temp2[0].split('_');
		//var offset = parseInt(temp3[temp.length-2]);
		//var index = parseInt(temp3[temp.length-1]);
		var offset = parseInt(temp3[temp3.length-2]);
		var index = parseInt(temp3[temp3.length-1]);
		if(!offset) offset = 0;
		if(!index) index = 0; 
		attr.offset = offset;
		attr.index = index;
		return attr;
	}
	
	// does the image change fade and adminimation
	function imgs(imgx){
		if(imglock == 0){
			imglock = 1;
			img = jQuery('#img2').html();
			offset = jQuery('#img2').find('img').attr('offset');
			
			jQuery('#img1').html(img);
			jQuery('#img2').hide();
			jQuery('#img2').html(imgx.find('div').html());
			jQuery('#img2').fadeIn(500);
			jQuery('#imgNav').append('<div class="img"><div style="margin-left:-'+offset+'px">'+img+'</div></div>');
			
			imgx.animate({
				width: '0'				
				}, 500, function() {
				imgx.remove();
				imglock = 0;
			});
			
			jQuery("#imgNav").find('.img').unbind();
			jQuery("#imgNav").find('.img').bind('click',function(e){
				if(imglock != 1){											
					imgx = jQuery(this);
					imgs(imgx);
				}
			});
		}
	}
	
	
	
	// init the functions
	jQuery(document).ready(function(){
		
		// NEW: set offset img nav
		jQuery("#imgNav").find('img').each(function(e){
			name = jQuery(this).attr('src');
			attr = getAttributes(name);
			// set position
			jQuery(this).parent().css('margin-left','-'+attr.offset+'px');
			jQuery(this).attr('offset',attr.offset);
			jQuery(this).attr('boxindex',attr.index);
		});
		
		// NEW: set offset img main
		name = jQuery("#img2").find('img').attr('src');
		attr = getAttributes(name);
		jQuery("#img2").find('img').attr('offset',attr.offset);
		
		// NEW: index the ddboxes
		var counter = 0;
		jQuery(".ddbox").each(function(e){
			counter ++;
			jQuery(this).attr('index',counter);						
		});
		
		// Click on big image
		jQuery('#imgMain').find('div').click(function(e){
			if(imglock != 1){
				imgx = jQuery("#imgNav").find('.img').first();
				imgs(imgx);
			}
		});
		
		// Click on slices 
		jQuery("#imgNav").find('.img').click(function(e){
			if(imglock != 1){
				imgx = jQuery(this);
				imgs(imgx);
			}
		});
		
		// NEW: click on ddbox do the magic
		jQuery(".ddbox").click(function(e){		
			index = jQuery(this).attr('index');	
			obj = jQuery("#imgNav").find('[boxindex='+index+']');
			if(obj.attr('src')){
				imgs(obj.parent().parent());
			}
		});
		
		jQuery(".box").find('h2').click(function(e){

			if(jQuery(this).hasClass('on')){
	 			jQuery(this).removeClass('on');
	 			jQuery(this).parent().find('.box-inner').slideUp(200);
	 		}else{
	 			var imgid = jQuery(this).find('input').attr('value');
	 			if(imgid){
	 				img = jQuery("#imgNav").find('#'+imgid);
	 				if(img.attr('id')){
	 					imgs(img.parent().parent());
	 				}
	 			}
	 			jQuery('.box').find('h2').removeClass('on');
	 			jQuery('.box-inner').slideUp(200);
	 			jQuery(this).addClass('on');
	 			jQuery(this).parent().find('.box-inner').slideDown(200);
	 		}
		});
		
		jQuery(".box_moduleInModule").find('h2._moduleInModule').click(function(e){
			
			if(jQuery(this).hasClass('on')){
				jQuery(this).removeClass('on');
				jQuery(this).parent().find('.box_moduleInModule-inner').slideUp(200);
			}else{
				var imgid = jQuery(this).find('input').attr('value');
				if(imgid){
					img = jQuery("#imgNav").find('#'+imgid);
					if(img.attr('id')){
						imgs(img.parent().parent());
					}
				}
				jQuery('.box_moduleInModule').find('h2._moduleInModule').removeClass('on');
				jQuery('.box_moduleInModule-inner').slideUp(200);
				jQuery(this).addClass('on');
				jQuery(this).parent().find('.box_moduleInModule-inner').slideDown(200);
			}
		});
		
		
	});
	
