$(function(){
	$("#menu>ul>li").DnDMenu('#footer-menu');
	$(".QnA li>h4").QnA();
	//$(".archive li>h2").QnA();
	$("#news4home ul").rotate(6000);
	$("#latest-news ul").rotate(4000,true);
	$("#product-fig-2").rotate(4000,true,true);
	$("#product-fig-5").rotate(4000,274);
	$("#logos ul").rotate(8000,160);
	$(window).bind('hashchange',function(){
		  animHashEffect(window.location);
	 });
	$("#right-area,.go-to-top").animHash();
})


/*$(function(){
  $(window).bind('hashchange', function(){
    var hash = location.hash;
	$("#right-area").click(function(){
		//window.location=hash;
		
	});
  })
  $(window).trigger( 'hashchange' );
});*/


$.fn.animHash=function(){
	if(window.location.hash) animHashEffect(window.location);
	$(this).find('a[href*=#]').click(function(){
		animHashEffect(this);
		return false;
	});
}

animHashEffect=function(ref){
		var hash=ref.hash;
		name=hash.replace(/(.*)?\#/,'');
		if(!name) return false;
		$a=$('a[name='+name+']');
		//if(!$a.length) return false;
		top=$a.offset().top;
		$a.parent().addClass('expanded').find(':header:first').next().stop().slideDown('fast');
		$('html,body').stop().delay(200).animate({scrollTop : top},800,function(){
																 window.location=hash;							 
																 });
		//$.scrollTo(top, 800, { axis:'y' });
}

$.fn.DnDMenu=function(footerMenu){
	var $a=$(this).find('a[href!=#]');
	var $aHash=$(this).find('a[href=#]');
	$aHash.click(function(){ return false;});
	var links= $a.map(function(i,o){return $(this).attr('href');}).get();
	var curLinkSplit=window.location.href.split('/');
	var curLink=curLinkSplit[curLinkSplit.length-1]||'index.php';
	var $subLink=$(this).add(footerMenu).find('>a,a').removeClass('active').filter('[href='+curLink+']').addClass('active');
	$subLink.parents('ul').prev().addClass('active');
	$(this).filter(':first').find('a:first').addClass('first');
	$(this).filter(':has(ul)').hover(
									function(){
										$(this).find('ul').stop(true,true).slideDown('fast');
										$(this).find('a:first').addClass('hover');
									},
									function(){
										$(this).find('ul').stop(true,true).slideUp('fast');
										$(this).find('a:first').removeClass('hover');
									});
}

$.fn.QnA=function(){
	$(this).css('cursor','pointer').nextAll().hide();
	return $(this).click(function() {
    	   $(this).nextAll().slideToggle("fast", QnAToggle);
	})
}
QnAToggle=function(){
    var $li = $(this).parent();
    //var backgroundImage=/collapse/.test($li.css('backgroundImage'))?"url(images/expand.gif)":"url(images/collapse.gif)";
	//$li.css('backgroundImage',backgroundImage);
	//$li.hasClass('expanded')?$li.removeClass('expanded'):$li.addClass('expanded');
	 $li[ $li.hasClass('expanded') ? 'removeClass' : 'addClass' ]( 'expanded' );
}

$.fn.anim=function(timeout,autoHeight,next){
	if(!$(this).length) return false;
	var timer;
	var w=$(this).width()
	var h=$(this).height();
	var hMax=Math.max.apply(null,$.map($(this).children(),function(el,i){ return $(el).height(); }));
	var $children=$(this).css({position:'relative',overflow:'hidden'})
					     .children().css({position:'absolute',display:'block',left:0, top:0, width:w, height:h});
	
	if(autoHeight){
		hMax=autoHeight===true?hMax:autoHeight;
		$(this).height(hMax).children().height(hMax);
		
	}

	if(next){
		$children.bind('click',function(){
			var $next=$(this).next().size()?$(this).next():$children.eq(0);
			$next.siblings().css({zIndex:1}).fadeTo('slow',0);
			$next.css({zIndex:2}).fadeTo('slow',1);
			//return false;
		}).eq(0).css({zIndex:2,opacity:1}).siblings().css({zIndex:1,opacity:0});
		
	}
	else{
		$children.eq(0).fadeTo('slow',1,function(){
					  
			var $active=$(this).next().length?$(this).next():$children.eq(0);
			var arg=arguments.callee;
			timer=window.setTimeout(function(){
									   $active.siblings().css({zIndex:1}).fadeTo('slow',0);
									   $active.css({zIndex:2}).fadeTo('slow',1,arg)}
									   ,timeout);
		
		}).css({zIndex:2,opacity:1}).siblings().css({zIndex:1,opacity:0});
	
	}
	
}

$.fn.rotate=function(timeout,autoHeight,next){
	return this.each(function(){ $(this).anim(timeout,autoHeight,next); });

}

