$(document).ready(function(){
	$.ajaxSetup({cache: true});
	// inner page
		// array not animate menu
		var notPageId = [1, 134, 139, 6, 7];
		var notParentId = [134];
	
	if($.inArray(pageId, notPageId) < 0 && $.inArray(parentId, notParentId) < 0){
		$("#adv-menu li").hide();
		$("#adv-menu .open").addClass("dropdown");
		$("#adv-menu .open li").show();
		
		// opened all inner ul and backlink
		$(".back-to-mainmenu").slideToggle(function(){
			$("#adv-menu .dropdown").slideDown(500, function(){
				$("#adv-menu .dropdown ul").each(function(){
					$(this).slideDown(500);		
				});
				
				// active current page-item
				if($(this).children('a').attr('rel') == pageId){
					$(this).children('a').children('i').attr('style', 'background: url("../img/ico-arrow-right.png") !important');
				}
				
				// opened ico
				if($(this).children('ul').size() == 0 && $(this).parent().attr('id') == 'adv-menu' && !$(this).children('a').hasClass('hasChild')){
					$(this).children('a').children('i').attr('style', 'background: url("../img/sub-menu-closed.gif") !important');
				}
			});
		});
	}
	
	// click main
	$('#main-menu li').live('click', function(){
		var rel = $(this).children().children().children().children().children('a').attr('rel');
		$("#adv-menu ul").slideUp(500);
		$(".back-to-mainmenu").slideUp(500, function(){
			$("#adv-menu ul").remove();
			$(".dropdown").removeClass('dropdown').removeClass('open');
			$('#adv-menu a[rel='+rel+']').parent().click();
		});
		return false;
	});
	
	// click left
	$('#adv-menu li').live('click', function(){
		// install vars
		var rel = $(this).children('a').attr('rel');
		var link = $(this).children('a').attr('href');
		var clParent = $(this).parent().attr('id');
		
		if(rel != '' || $(this).hasClass('back-to-mainmenu') || $(this).hasClass('open')){			
			if(!$(this).hasClass("back-to-mainmenu")){		
				if($(this).children('a').hasClass('hasChild')){	
					// ajax, get items menu
					$.get('ajaxmenu.html?id='+rel, function(data){
						if(clParent == "adv-menu"){
							$("#adv-menu").slideUp(500, function() {
								$("#adv-menu li").hide();
								$("#adv-menu").show();	
								$(".back-to-mainmenu").slideToggle();
								// adding data to html
								$("#adv-menu a[rel="+rel+"]").parent().append(data);	
								// adding classes
								$("#adv-menu a[rel="+rel+"]").parent().addClass('open').addClass('dropdown');
								$("#adv-menu .dropdown").slideDown(500, function(){
									$("#adv-menu .dropdown ul").slideDown(500);
								});	
							});
						}	else	{
							// if have ul
							if($("#adv-menu a[rel="+rel+"]").parent().children('ul').length > 0){	
								$("#adv-menu a[rel="+rel+"]").parent().siblings().children('ul').each(function(){
									if($(this).parent().hasClass('dropdown')){
										$(this).slideUp(500, function(){
											$(this).parent().removeClass('dropdown').removeClass('open');
											$(this).parent().children('a').children('i').attr('style', ' ');								
										});
									}
								});
								
								$("#adv-menu a[rel="+rel+"]").parent().toggleClass('open').toggleClass('dropdown');
								$("#adv-menu a[rel="+rel+"]").parent().children('ul').slideToggle(500);
							// if not have ul
							}	else	{
								$("#adv-menu a[rel="+rel+"]").parent().siblings().children('ul').each(function(){
									if($(this).parent().hasClass('dropdown')){
										$(this).slideUp(500, function(){
											$(this).parent().removeClass('dropdown').removeClass('open');
											$(this).parent().children('a').children('i').attr('style', ' ');								
										});
									}
								});							
								
								$("#adv-menu a[rel="+rel+"]").parent().append(data);	
								$("#adv-menu a[rel="+rel+"]").parent().addClass('open').addClass('dropdown');
								$("#adv-menu a[rel="+rel+"]").parent().children('ul').slideDown(500);		
							}
						}
					});
				}	else	{
					// redirect
					location.href = link;					
				}
			}	else	{
				// hidden all and remove classes
				$("#adv-menu ul").slideUp(500);
				$(".back-to-mainmenu").slideUp(500, function(){
					$("#adv-menu ul").remove();
					$("#adv-menu li:not(.back-to-mainmenu)").slideDown(500);
					$(".dropdown").removeClass('dropdown').removeClass('open');
				});
			}
			return false;
		}	else	{
			// redirect
			location.href = link;
		}
	});
});
