//General page init setup
/*
* author : oechai
* last modified : 2008.12.26.1635
*/

$(document).ready(function(){

	//png fixing for IE < 7
	if($.browser.msie && parseInt($.browser.version)<7){ pngFix(); }

	//set submenu effect
	if($(".menu>ul>li:has('.sub-menu-wrap')").length>0) {
		var menulist = $(".menu>ul>li");
		var mm_button = $(".menu>ul>li>a");
		var mm_with_submenu = $(".menu>ul>li:has('.sub-menu-wrap')");
		var submenuwrap = $('.sub-menu-wrap');
		var submenu = $('.sub-menu');
		var bottom = $('.sub-menu-wrap .bottom');
		var background = $('.sub-menu-wrap .bg');
		
		var opa = 90;
		submenu.css('background-color', "transparent");
		background.css('opacity', String(opa/100));
		bottom.css('opacity', String(opa/100));
		
		//background setup
		submenuwrap.show();
		menulist.each( function(i) {
			var c = $(this.childNodes);
			if (c.length>0){
				c.each(function(j) {
					if(this.tagName){
						if($(this).hasClass('sub-menu-wrap')) {
							this._olH = $(this).height();
							var n = submenuwrap.index(this);
							var submenu_h = $(submenu[n]).height()+parseInt($(submenu[n]).css('padding-top'));
							$(background[n]).css('height', submenu_h);
							
							if($(this).css('display')=='block'){
								$(this).css('display', 'none');
								$(this).css('height', '1px');
							}
						}
					}
				});
			}
		});
		
		//menulist button setup
		submenuwrap.hide();
		menulist.each( function(i) {
			$(this).hover(
				function(){
					var c = $(this.childNodes);
					if (c.length>0){
						c.each(function(j) {
							if(this.tagName){
								if($(this).hasClass('sub-menu-wrap')) {
									$(this).queue("fx", []);
									$(this).stop();
									if($(this).css('display')=='none') { 
										$(this).css('display', 'block');
										$(this).css('height', '1px'); 
									}
									$(this).animate({height:this._olH}, 180);
								}
							}
						});
					}
				},
				function(){
					submenuwrap.queue("fx", []);
					submenuwrap.stop();
					submenuwrap.animate({height:0}, 180, function() { $(this).css('display', 'none'); });
				}
			);
		});
	}
	
	
	//apply slide up/down fx to Tutorial's page
	if($('.list-of-tuts').length>0) {
		
		$('.thelist').css('display', 'none');
		
		$('.thelist').each(function(i){
			$(this).each(function(i){
				var count = 0;
				$(this.getElementsByTagName('span')).each( function(i) {
					if( $(this).attr('class')=='numbering' ) {
						count++;
						$(this).html(count);
					}
				});
		  	});
		});
			
		
		$('.topic').each( function(i) {
			$(this).click(function(){
				var thelist = $($('.thelist')[i]);
			
				if(thelist.css('display')=="none") {
					thelist.slideDown(500, function(){
					thelist.css('width', '100%');
					});
				} else {
					thelist.slideUp(400, function(){
					});
				}
			})
		});
	}
	
	//apply slide up/down fx to agents' page
	if($('.list-of-agents').length>0) {
		$('.thelist').css('display', 'none');
		
		$('.thelist').each(function(i){
			$(this).each(function(i){
				var count = 0;
				$(this.getElementsByTagName('span')).each( function(i) {
					if( $(this).attr('class')=='numbering' ) {
						count++;
						$(this).html(count);
					}
				});
		  	});
		});
			
		
		$('.list-wrap').each( function(i) {
			$(this).click(function(){
				var thelist = $($('.thelist')[i]);
			
				if(thelist.css('display')=="none") {
					thelist.slideDown(500, function(){
					thelist.css('width', '100%');
					});
				} else {
					thelist.slideUp(400, function(){
					});
				}
			})
		});
	}
	
	//apply scrolling effect to faq page
	if($('.list-of-faqs').length>0 && $('.overview-list').length>0 ) {
		//ref: http://www.learningjquery.com/2007/09/animated-scrolling-with-jquery-12
		$('a[href*=#]').click(function() {
			var d = new Date();
			if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
				var $target = $(this.hash);
				$target = $target.length && $target	|| $('[name=' + this.hash.slice(1) +']');
				if ($target.length) {
					var targetOffset = $target.offset().top;
					if((targetOffset-20)>=0) targetOffset = targetOffset - 20;
					if ($('html,body').scrollTop() != targetOffset) {
						$('html,body').animate({scrollTop: targetOffset}, 800);
					}
					return false;
				}
			}
		});
	}
	
	//apply slide up/down fx to download' page
	if($('h4.t2-swpatchversion').length>0) {
		$('h4.t2-swpatchversion').css('cursor', 'pointer');
		$('.patchversion-wrap').css('display', 'none');

		$('h4.t2-swpatchversion').each( function(i) {

			$(this).click(function(){
				var theObj = $($('.patchversion-wrap')[i]);
			
				if(theObj.css('display')=="none") {
					theObj.slideDown(500, function(){
					theObj.css('width', '100%');
					});
				} else {
					theObj.slideUp(400, function(){
					});
				}
			})
		});
	}
	
	//apply container sliding show  in main page
	if($('.vbm-container').length>0) {
		$('.nextSlideBtn').click( function () {
			$('.vbm-container').animate({scrollLeft: 972}, 800, function () {  });
		});
		
		$('.previousSlideBtn').click( function () {
			$('.vbm-container').animate({scrollLeft: 0}, 800, function () {  });
		});
	}
});

