$(document).ready(function(){
	/*$("#dialog").dialog({
	    'height': 700,
	    'width': 850,
	    'modal': true,
		'autoOpen': false,
		'title': '<span class="dialog-header">Please signup and go to system</span>',
		'scrollbar': false
	});*/
	
	$("ul.opened").parent().find("a:first").addClass("expanded");
	// First level
	$("a.first_level").live('click', function(){
		updateFirst(this);
		return false;
	});
	// Inner level
	$("ul.nav li ul li a").live('click', function(){
		updateInner(this);
		return false;
	});
	
	
	$(".breadcrump-item").live('click', function(){
	  var code = $(this).attr('_bcode');
      if(code != catalog.current_category){
    	tmp = $('.current').find( "[_code='" + code + "']" ); 
    	
    	if(tmp.hasClass('first_level')){
    	  updateFirst(tmp);
    	  tmp.next().children("a").removeClass('subtree-expanded').addClass('subtree-collapsed');
		  updateFirst(tmp);	    
    	}else{
  		  updateInner(tmp);
		  updateInner(tmp);	       		
    	}
	  }
      
	  return false;
	})

	$("ul.opened").show();
	$("ul.nav li ul.opened:first").parent().addClass("current");
	
	$('#search-form').live('submit', function(){
      search.execute();    
	  return false;
	})	
	
	$('#search-form-button').live('click', function(){
      search.execute();    
	  return false;
	})	
			
	$('.pager-link').live('click', function(){
		var page = $(this).attr('_page');
		var paging_target = $(this).attr('_paging_target');
	
		switch(paging_target){
		  case "product_list":
			  catalog.load_product(page, catalog.current_category, catalog.parent);
	        break;
		  case "order_list":
			  order.execute(page);
	        break;
		  case "search":
			  search.execute(page);
	        break;
                 case "invoice_list":
			  invoice.execute(page);
	        break;
		}
		
        return false;	
	})	
	
	$('#spare-aparts').click(function(){+
		window.open('http://weborder.husqvarna.com/order/index.jsp?brandid=HQ', 'husqvarna', 'status=1,toolbar=1,width=850,height=700');
		//$("#dialog").dialog("open");		
	})
	
});

function updateInner(point){
  if($(point).hasClass("subtree-expanded")){
	$(point).removeClass("subtree-expanded").addClass("subtree-collapsed").parent().find("ul").slideUp();
	return false;			
  }else{
	$(point).parent().parent().find("ul.opened").removeClass('opened').slideUp();
	$(point).parent().parent().find("a.subtree-expanded").removeClass("subtree-expanded").addClass("subtree-collapsed");
	$(point).removeClass("subtree-collapsed").addClass("subtree-expanded").parent().find("ul:first")/*.slideDown()*/.addClass('opened');
			
  }
  catalog.draw_level(point);     
}

function updateFirst(point){
  if($(point).hasClass("expanded")){
	$(point).removeClass("expanded").parent().find("ul").slideUp();
  }else{
	$("ul.nav li").removeClass("current").find("ul:first").slideUp();
	$("ul.nav li a").removeClass("expanded");
	$(point).addClass("expanded").parent().addClass("current")/*.find("ul:first").slideDown()*/;
	
	catalog.draw_level(point);
  }	
}


catalog = {
  /*default category*/		
  current_category: '',	
  parent_category: '',	
  draw_level: function(point, params){
	var ul = $(point).next();  
	var code = $(point).attr('_code');
	var parent = $(point).attr('_parent'); 
	var children = ul.children();
	  
	if(!ul.hasClass('loaded')){
	  var children_cnt = children.length;
      var current_num = 0;
	  children.each(function(){
		var li = $(this);  
		//get link "a" tag object from "li"-parent 
		var tmp = li.children().eq(0);
		var code = tmp.attr('_code');
		$('.preloader.catalog').show();
		$.post('/ajax/uploadCatalog',
		  {
			'code': code
		  }, function(data, responseCode){
			  if(util.check_response_status(responseCode)){
			    if($.trim(data) != "" && $(data).find("li").length > 0){
			      tmp.removeClass("subtree-empty").addClass("subtree-collapsed").after(data);
			    }else{
//				  li.addClass("opened");
				  tmp.after(data);
			    }
			  
			    current_num++;
			    if(current_num == children_cnt){
			      $('.preloader.catalog').hide();
			      $(point).parent().find("ul:first").slideDown();
			    }
			  }
		  }
		);
	  });
	  ul.addClass('loaded');
	}else{
	  $(point).parent().find("ul:first").slideDown();
	}
	
	catalog.load_product(1, code, parent);
  },
  
  load_product: function(page, current_category, parent){
	$('.preloader.product').show();	
	$('#content-inner').load(
			'/ajax/changePage',
			{ 
			  'page': page, 
			  'current_category': current_category, 
			  'parent': parent
			}, function(data, responseCode){
                          if(util.check_response_status(responseCode)){
                                    $('#content-inner').html(data);
                                    $('.item-img a').lightBox({fixedNavigation:true});
                                    catalog.current_category = current_category;
                                catalog.parent = parent;
                             }
		  	  $('.preloader.product').hide();				
                        }
	);	  
  }
}

search = {
  type: '',
  mode: '',
  string: '',
  _execute: function(page){
	$('.preloader.product').show();	
	$.post('/ajax/search',
	  {
		'type': this.type,
		'mode': this.mode,
		'string': this.string,
		'page': page
	  }, function(data, responseCode){
           if(util.check_response_status(responseCode)){
             $('#content-inner').html(data);
             $('.item-img a').lightBox({fixedNavigation:true});
           }
	       $('.preloader.product').hide();
	  });	  
  },
  execute: function(page){
      search.type   = $("#search-form").find('#search-type').val();
      search.mode   = $("#search-form").find('#search-mode').val();
      search.string = $("#search-form").find('#search-string').val();

      this._execute(page);
	  return false;
  }
}

order = {
  execute: function(page){
	$('.preloader.product').show();	
	$.post('/ajax/orderList',
	  {
		'page': page 
	  }, function(data, responseCode){
         if(util.check_response_status(responseCode)){
           $('#content-inner').html(data);	 
         }
		 $('.preloader.product').hide();
	  });	  
  }
}
invoice = {
  execute: function(page){
	$('.preloader.product').show();
	$.post('/ajax/invoice',
	  {
		'page': page
	  }, function(data, responseCode){
         if(util.check_response_status(responseCode)){
           $('#content-inner').html(data);
         }
		 $('.preloader.product').hide();
	  });
  }
}

/**
 * Util functions js-object
 */
util = {
    params: [],	
	/*JS translate emulation*/	
    __:function(str){
    	return str;
    },
    /*Check to digit*/	
	is_digits: function(value){
		return /^\d+$/.test(value);
	},	
	/*Show positive message*/	
	show_message: function(msg){
		this._prepare_message();
		$("#notify-message").html(msg);
		$("#notify-message").slideDown().delay(3000).slideUp();
	},
	/*Show error message*/	
	show_error: function(msg){
		this._prepare_message
		$("#notify-message").addClass('error').html(msg);
		$("#notify-message").slideDown().delay(3000).slideUp();
	},	
	/*Show positive message*/	
	show_message_nohide: function(msg){
		this._prepare_message
		$("#notify-message").html(msg);
		$("#notify-message").slideDown();
	},
	/*Show error message*/	
	show_error_nohide: function(msg){
		this._prepare_message
		$("#notify-message").addClass('error').html(msg);
		$("#notify-message").slideDown();
	},	
	_prepare_message: function(){
		$("#notify-message").hide();
		$("#notify-message").removeClass('error')
	},
	set_param: function(key, value){
	  util.params[key] = value;	
	},
	get_param: function(key){
	  return util.params[key];	
	},
	/*Check Ajax for correct*/
	check_response_status: function(responseCode){
	  if($.trim(responseCode) == "error"){
        window.location.reload(true);
        return false;
	  }else{
        return true;
	  }
	}	
}
