var ROOT = window.location.href;

$(document).ready(function() {
  //$('#menu a').setTabHandler();
  
  $('img[rel=tooltip], a[rel=tooltip]').mouseover(function(e) {
		
		//Grab the title attribute's value and assign it to a variable
		var tip = $(this).attr('title');	
		
		//Remove the title attribute's to avoid the native tooltip from the browser
		$(this).attr('title','');
		
		//Append the tooltip template and its value
		$(this).parent().append('<div id="tooltip"><div class="tipHeader"></div><div class="tipBody">' + tip + '</div><div class="tipFooter"></div></div>');		
		
    
		//Show the tooltip with faceIn effect
		$('#tooltip').fadeIn('500');
		$('#tooltip').fadeTo('10',0.9);
		
	}).mousemove(function(e) {
		//Keep changing the X and Y axis for the tooltip, thus, the tooltip move along with the mouse
		$('#tooltip').css('top', e.pageY + 10 );
		$('#tooltip').css('left', e.pageX + 20 );
		
	}).mouseout(function(e) {
	
		//Put back the title attribute's value
		$(this).attr('title',$('.tipBody').html());
	
		//Remove the appended tooltip template
		$(this).parent().children('div#tooltip').remove();
		
	});


});

function showLoading(){
  $.loading({text: 'Loading...',align: 'top-center', mask: true});
}

function hideLoading(){
  $.loading(false);
}
/*
(function($)
{
  $.fn.makeBgGradation = function(startColor, endColor, callback)
  {
      var el = $(this);
      var forIE = "progid:DXImageTransform.Microsoft.gradient(startColorstr='"+startColor+"', endColorstr='"+endColor+"')";
      var forMozila = "-moz-linear-gradient(top, "+startColor+",  "+endColor+")";
      var forOpera = "-webkit-gradient(linear, left top, left bottom, from("+startColor+"), to("+endColor+"))";
      
      el.css('background',startColor); 
      el.css('filter',forIE); 
      el.css('background',forOpera);
      el.css('background',forMozila);    
      if ( $.isFunction(callback) ) callback();
  }
  
  $.fn.transparentBg = function(value, callback)
  {
    var el = $(this);
    value = (value > 1) ? value/100 : value;
    el.css('filter','alpha(opacity='+(value*100)+')');
	  el.css('-khtml-opacity',value);
	  el.css('-moz-opacity',value);
	  el.css('opacity',value);
    if ( $.isFunction(callback) ) callback();
  }

   //IF a tab element is clicked, the function below will handle the request 
   $.fn.setTabHandler = function(){
     $menuAnchor = $(this);
        if ($menuAnchor.length) {
            $menuAnchor.click(function(e) {
                e.preventDefault();
                $clickedLink = $(this);
                $(this).parents("ul").children("li").removeClass();
                $(this).parent().addClass('active');
                menuBg = $(this).css('background-color');
                showLoading();
                $.ajax({
                    url: $(this).attr('href'),
                    beforeSend: function() {
                        if ( !$('#subNav').length ) {
                            $('#mainContent').prepend('<div id="subNav"></div>');
                        }
                        $('#subNav').css('background-color',menuBg);
                        $('#subNavContainer').animate({
                          height: "0",
                          opacity: 0,
                        }, 600 );
                        $('#subNav').children().fadeOut('slow');
                        //$('#subNav').html('<img src="./img/ajax-loader-small.gif">');
                    },
                    complete: function(data) {
                        hideLoading();
                        $('#subNavContainer').remove();
                        $('#subNav').remove();
                        $('#mainContent').prepend(data.responseText);
                        $('#bannerFeature').hide();
                        $('#mainPicText').hide();
                        $('#subNav a').setSubTabHandler();
                        $('#mainContent').find('div#subNavContainer').showProducts();
                    }
                });
            });
        }
    };
   
    //IF a sub menu from tab menu is clicked, the function below will handle the request  
   $.fn.setSubTabHandler = function (){
      $(this).click(function(e) {
        e.preventDefault();
        
        if($('#mainPic').length < 1){
          $('#page-open-line').remove(); 
          $('#search_results_block').remove();
             
          if($('#contentDetail').length > 0){
            $('#contentDetail').remove();
          }
              
          $('#mainContent').append('<div id="mainPic"></div>');
        }
            
        $('#subNav a').removeClass('selected');
        $(this).addClass('selected');
        $('#bannerFeature').hide();
            
        if($('#subNavContainer').size() < 1){
          $('#mainPic').prepend('<div id="subNavContainer"></div>');
        }
        
        //$.preloader = '<div style="text-align:center;border-bottom:1px solid #8e9ca6;"><img src="./img/ajax-loader-small.gif" /></div>';
        //$('#subNavContainer').html($.preloader);
        $(this).showCategoryDetail();
            
          return false;
        });
    }

    $.fn.showCategoryDetail = function(){
      _subTabURL = $(this).attr('href');
      showLoading();
      $.get(_subTabURL, function(content){
        $('#subNavContainer').slideUp('normal');
        $('#subNavContainer').html(content);
        $('#subNavContainer').slideDown('normal');
        if($('#subNav').first()){
          _startColor = $('#subNav').first().css('background-color');
          _callBack = $('#subNavContainer').transparentBg(0.75);
          $('#subNavContainer').makeBgGradation(_startColor,'#a8a8a8',_callBack);
          $('#subNavContainer li').css('color','#000000');
        }
        
        $('#subNavContainer .open-list a, #subNavContainer li.title').onClickList();
        hideLoading();
        
      });
    }//$(this).showProducts();

    $.fn.showProducts = function(){
         $currentTarget = $(this);
         $parentTarget = $(this).parent();
        $(this).find('#contentDetail a.prev').click(function(e) {
            e.preventDefault();
            _uriPrevProduct = $(this).attr('href');
            showLoading();
            $.get(_uriPrevProduct, function(result) {
              if($currentTarget.is("div#subNavContainer")){
                $currentTarget.remove();
                $parentTarget.prepend(result);
                $parentTarget.find("div#subNavContainer").first().showProducts();
              }else{
                $('#productContainer.active').html(result);
                $('#productContainer.active').showProducts();
              }              
              hideLoading();
            });
        });
        
        $(this).find('#filterArea select').multiSelect(null,null,function(el){
          if(el.selectedValuesString() != ""){
            _hostNow = window.location.href;
            showLoading();
            $.product_filter_container = $(el).parent().parent();
            $.get(_hostNow+'products/filter/q:'+el.selectedValuesString() , function(result) {
                if($('#filterArea').parent().parent().attr('id') == "contentDetail"){
                  $('#filterArea').parent().parent().find('#search_results_block').remove();
                }
                $.product_filter_container.children().last().remove();
                $.product_filter_container.append(result);
                hideLoading();
            });
          }
        });
        
        
        $(this).find('#contentDetail a.next').click(function(e) {
            e.preventDefault();
            uriNexProduct = $(this).attr('href');
            showLoading();
            $.get(uriNexProduct, function(result) {
              if($currentTarget.is("div#subNavContainer")){
                $currentTarget.remove();
                $parentTarget.prepend(result);
                $parentTarget.find("div#subNavContainer").first().showProducts();
              }else{
                $('#productContainer.active').html(result);
                $('#productContainer.active').showProducts();
              }              
              hideLoading();
            });
        });
        
        $(this).find('#search_desc a.limiter').bindAsPagination();
        
        $(this).find('#subNavChildren a').click(function(e) {
           e.preventDefault();
           _menuLink = $(this);
           _menuLink.parent().children().removeClass('selected')
           _menuLink.addClass('selected');
           var mainParent = _menuLink.parent().parent(); //page-open-line
           var superParent = $(mainParent).parent();
           var crumbArea = _menuLink.parent().next();
           _uriSubChild = _menuLink.attr('href');
           showLoading();
           
           $.get(_uriSubChild, function(result) {
             if(result && result.length > 0){
               if($(crumbArea).attr('id') == "search_desc"){
                 $(crumbArea).remove();
               }
               $(superParent).children().last().remove();
               $(superParent).append(result);
               $(superParent).children().last().showProducts();
               $(superParent).children().last().find('#top-right-arrow').hide();
             }
             hideLoading();
           });
           
           return false;
        });
        
        $(this).find('.readmore').click(function(e) {
          e.preventDefault();
          $(this).hide();   
          $(this).next().show();
          $(this).next().next().show();
        });
        
        $(this).find('.unReadMore').click(function(e) {
          e.preventDefault();
          $(this).hide();   
          $(this).prev().hide();
          $(this).prev().prev().show();
        });
        
    }
    
    $.fn.bindAsPagination = function(){
      _listPagination = $(this);
      $(_listPagination).click(function(e) {
          e.preventDefault();
          _uriPagination = $(this).attr('href');
          _el = $(this);
          showLoading();
          $.get(_uriPagination, function(result) {
              _contentDetail = $(_el).parent().parent().parent();
              _productContainer = _contentDetail.parent();
              if($(_productContainer).attr('id') == "contentDetail"){
                $(_productContainer).children().last().remove();
                $(_productContainer).append(result);
                $(_productContainer).children().last().showProducts();
                $(_productContainer).children().last().find('#top-right-arrow').hide();
              }else if(_productContainer.is('div#subNavContainer')){
                $targetParent = _productContainer.parent();
                _productContainer.remove();
                $targetParent.prepend(result);
                $targetParent.find('div#subNavContainer').showProducts();
              }else{
                _productContainer.html(result);
                $(_productContainer).showProducts();
                $(_productContainer).onCloseProduct(_productContainer.prev());
              }
              hideLoading();
          });
          return false;
       });
    }
    
    $.fn.onClickList = function(){
      _listOpenBtns = $(this);
      $(_listOpenBtns).click(function(e){
        e.preventDefault();
        if($(this)[0].tagName.match(/LI/i)){
          _item = $(this).next().children("a").first();
        }else{
          _item = $(this);
        }
        
        $('#mainContent #productContainer').each(function(i,val){
            $(val).slideUp('normal');
            $(val).children().remove();
            $(val).prev().fadeIn('normal');
            $(val).removeClass('active');
          });
        _itemContainer = _item.parent().parent();
        _productContainer = $(_itemContainer).next();
        showLoading();
        $.get(_item.attr('href'),
                 function(content){
                   $(_itemContainer).fadeOut('normal');
                   $(_productContainer).addClass('active');
                   $(_productContainer).html(content);
                   $(_productContainer).slideDown('normal',$(_productContainer).show());
                   $(_productContainer).showProducts();
                   $(_productContainer).onCloseProduct(_itemContainer);
                   hideLoading();
                 });
        
        return false;
      })
    }
    
    $.fn.onCloseProduct = function(_listContainer){
      _listDetail = $(this);
      _closeBtn = $(_listDetail).find('.showChildren').first();
      $(_closeBtn).click(function(e){
        e.preventDefault();
        _productContainer = $(this).parent().parent().parent().parent().parent();
        $(_productContainer).slideUp('normal');
        $(_productContainer).children().remove();
        $(_productContainer).prev().fadeIn('normal');
        
        //$(_listDetail).slideUp('normal');
        //$(_listDetail).children().remove();
         //$(_listContainer).fadeIn('normal');
      });
    }

})(jQuery);*/
