$(document).ready(function(){
	applyShadow('.cat_title');
	contents('.assyst_menu');
	doubles('.right_menu');
	anchorImitate();
});

function imageSelect( id, imagesrc, caption, width, height )
{
	if( document.getElementById("caption") != null )
	{
		document.getElementById("caption").innerHTML = ( caption.length > 0 ) ? unescape( caption ) + '<br />' : ''; 
	}
	document.getElementById("mainimage").src = imagesrc;
	
	document.getElementById("mainimageSrc").href = imagesrc;
	
	//document.getElementById("mainimage1").alt = caption;
	document.getElementById("mainimage").className = "caption";
	//document.getElementById("mainimage1").height = height;
	//flag new image thumbnail
	var lis = document.getElementsByTagName('li');
	for (var i=0;i<lis.length;i++)
	{
		if (lis[i].className.indexOf("currentImage") > -1)
		{
			lis[i].className = '';
		}
	}
	document.getElementById("image"+id).className = "currentImage";
	
}

$(document).ready(function(){
	$('.main_navi li').each(function() {
		var col = $('.main_navi li').length;
		var colY = 8;
		var colStr = Math.ceil(col/colY);
		
		var widthCell = 98/col;
		
		if (col < colY) {			
			$('.main_navi li').css('width', widthCell + '%');
		}

		if ((col > colY) && (col < colY*colStr)) {
			var colStr = Math.floor(col/colY);		
			var colLast = col - colY*colStr;
				
			for (var i = 1; i < (colY-colLast); i++) {
					var elt = $('.main_navi li:last-child');
					$(elt).after("<li class='blank'></li>");
			}
		}
		
		/*var h = $('.main_navi li:first-child').height();
		
		for (var i = 1; i <= col; i++) {
			var elt = $('.main_navi li').eq(i).height();
			if (elt > h) {
				h = elt - 7;
			}					
		}
		
		$('.main_navi li.active .linebttm').css('top', h + 'px');*/
	});
		
	var elt = $('.main_navi li.blank:last-child');
	$(elt).after("<li class='blank'></li>");
	
	$('.blank:last-child').css('width', '13%');
	
	$('.news ul li:last-child').css('border-bottom', '0');
	
	$('.tab td:last-child').css('text-align', 'right');
	$('.tab tr:last-child td').css('border', '0');
	
	$('.tab_s td:last-child').css({'text-align' : 'left', 'border-left' : '3px solid #fff'});
	$('.tab_s .tab_title td:last-child').css('border-left', '0');
	$('.tab_s .tab_title td:first-child').css('border-right', '0');
	
});


function applyShadow(elt) {
	$(elt).each(function() {
		var value = $(this).text();	  
		$(this).append("<span class='shadowed'>"+ value +"</span>");			
	});
}



function anchorImitate() {
	$('.anchor_imitate').each(function() {
		$(this).bind('click', function() {
			var link = $(this).attr('href');
			link = link.replace('#', '');
			$('.thing').each(function() {
				$(this).css('display', 'none');
			});
			$('.things').each(function() {
				$(this).css('display', 'none');
			});
			$('div#'+link).css('display', 'block');
		
		});
	});
}


function contents(givenClass)  {
	
	var elt = givenClass + ' ul li';	
	
	$('.thing').each(function(i) {
		$(this).hide();
		var thingId = 'elt' + i;
		$(this).attr('id', thingId);
	});
		
$(elt).each(function(i) {
		var eltName = 'elt' + i;
		$(this).attr('name', eltName);
	});

	var myHash = document.location.hash;
	
	
	if (myHash !== '') {
		$(myHash).css('display', 'block');
		var cnt = myHash.replace('#','');
		$(elt+'[name='+cnt+']').addClass('active');	
	} else {
		var firstli = $(elt)[0];
		$(firstli).addClass('active');
		var firstThing = $('.thing')[0];
		$(firstThing).css('display', 'block');
		myHash = '#' + $(firstThing).attr('id');
	}

	$(elt).each(function(i) {

		$(this).bind("click", function() {
			if (!$(this).hasClass('active')) {
				
				var anchorName = $(this).attr('name');
				//document.location.hash = anchorName;
					
				$(elt).removeClass('active');
					
				$('.thing').hide();
				
				$(this).addClass('active');

				 
				$('.thing[id=' + $(this).attr('name') + ']').toggle();
				if (!jQuery.support.cssFloat) { 
					$(document).scrollTop(0);
				}
			}
		});
			
		
	});
}

function doubles(givenClass)  {
	
	var elt = givenClass + ' ul li';	
	
	$('.things').each(function(i) {
		$(this).hide();
		var thingId = 'elts' + i;
		$(this).attr('id', thingId);
	});
		
$(elt).each(function(i) {
		var eltName = 'elts' + i;
		$(this).attr('name', eltName);
	});

	var myHash = document.location.hash;
	
	
	if (myHash !== '') {
		$(myHash).css('display', 'block');
		var cnt = myHash.replace('#','');
		$(elt+'[name='+cnt+']').addClass('active');	
	} else {
		var firstli = $(elt)[0];
		$(firstli).addClass('active');
		var firstThing = $('.things')[0];
		$(firstThing).css('display', 'block');
		myHash = '#' + $(firstThing).attr('id');
	}

	$(elt).each(function(i) {

		$(this).bind("click", function() {
			if (!$(this).hasClass('active')) {
				
				var anchorName = $(this).attr('name');
				//document.location.hash = anchorName;
					
				$(elt).removeClass('active');
					
				$('.things').hide();
				
				$(this).addClass('active');

				$('.things[id=' + $(this).attr('name') + ']').toggle();
				if (!jQuery.support.cssFloat) { 
					$(document).scrollTop(0);
				}
			}
		});
			
		
	});
}

function setCookie (name, value, expires, path, domain, secure) {
      document.cookie = name + "=" + escape(value) +
       ((expires) ? "; expires=" + expires : "") +
       ((path) ? "; path=" + path : "") +
       ((domain) ? "; domain=" + domain : "") +
       ((secure) ? "; secure" : "");
}

function getCookie (name) {
	var cookie = " " + document.cookie;
	var search = " " + name + "=";
	var setStr = null;
	var offset = 0;
	var end = 0;
	if (cookie.length > 0) {
		offset = cookie.indexOf(search);
		if (offset != -1) {
			offset += search.length;
			end = cookie.indexOf(";", offset)
			if (end == -1) {
				end = cookie.length;
			}
			setStr = cookie.substring(offset, end);
		}
	}
	return(setStr);
}


