/* Author: 

*/

$.fn.holderPosition = function(){
    var pos; // position of an element
    var lspace = Math.round(($(window).width() - 960) / 2); // left margin of site wrapper
    var ppos = $(this).parent().offset();

    if(lspace >= 0)     pos = ppos.left - lspace;
    else                pos = ppos.left;
    
    return pos;
}

$.fn.setHolder = function() {
    var element = $(this);
    var mleft; // new position of an element
    var firstpos = element.holderPosition(); // position of an element
    
    if((firstpos + element.width()) > 960) mleft = 960 - (firstpos + element.width()); // margin-left
    else mleft = 0;
    
    element.css({'margin-left': mleft + 'px'}); // element New Position
};

    if ($('ul#menu').length > 0)
        {
                $('ul#menu li ul.selected').css('display', 'block');
                $('ul#menu > li > a').click(function(e){
                        e.preventDefault();                        
                        $('ul#menu li a').not($(this)).parent().find('ul').slideUp(400);
                        $(this).parent().find('ul').slideDown(400);
                });
        }

// Bootstrap
$(function(){

    
    $('#flags li a').hover(function(){
        $(this).children('.flag').css('background-position-y','0px');
    },
    function(){
        $(this).children('.flag').css('background-position-y','-20px');
    });
    
    $("#geo").treeview({

            persist: "location",
            collapsed: true,
            unique: true
    });
    //$('#geo > ul > li').has('li.product').css('display','block');
    
    $('.map').maphilight({
        fill: true,
	fillColor: 'fbae16',
        fillOpacity: 1,
        stroke: true,
        strokeColor:'ffffff',
        strokeWidth: 2
    });
    
    $('#star').data('maphilight', $('#star').data('maphilight'));
    
    $(".pf a[rel^='prettyPhoto']").prettyPhoto({
        animation_speed:'fast',
        slideshow:10000,
        theme: 'facebook',
        hideflash: true
    });
    $('.menunav').corner("bl 30px");
    
    if ($('ul#acc-menu').length > 0)
        {
            $('ul#acc-menu li ul.selected').css('display', 'block');
            $('ul#acc-menu > li > a').click(function(e){
                e.preventDefault();                        
                $('ul#acc-menu li a').not($(this)).parent().find('ul').slideUp(400);
                $(this).parent().find('ul').slideDown(400);
            });
        }
    
    // *************************************************************************
    $('#top-menu ul li').each(function(){
        var totalwidth = 0;
        
        if($(this).find('ul').length > 0){
            $('>ul>li',this).each(function(){
                if ( $.browser.msie ) {
                    if ( parseInt($.browser.version, 10) < 8 ) totalwidth += $(this).width() + 4;
                    else totalwidth += $(this).width() + 2;
                }else {
                    totalwidth += $(this).width() + 3;
                }
                
            });
        }
        
        $('ul', this).not('.end, .end ul').css({'width':totalwidth + 'px'}); // New Width
        $('#top-menu>ul>li>ul').css({'top':'18px'});
        
   });
   
   // Level 5 varsa width=524px
   $('#top-menu nav>ul').each(function(){
       if($(this).find('ul').length > 0){
           $(this).closest('nav').css({width:'524px'});
       }
   });
   
   $('nav','#top-menu').hide(); // Hide Last nav
   $('#top-menu>ul ul').find('ul').not('.end, .end ul').hide(); // Hide all ul not .end
   $('#top-menu>ul').find('ul').not('.end, .end ul').hide(); // Hide all ul not .end
   
   var config = {
       over: function (){
           $(this).addClass('active');
           
           if($('>ul',this).length > 0){
               $('>ul',this).not('.end').setHolder(); // Set Left-Margin
               $('>ul',this).show();
           }else if ($('>nav',this).length > 0){
               if($('>nav',this).length > 0){
                   var color = $('a',this).css('background-color');
                   
                   $(this).css({backgroundColor:color});
                   $('>nav',this).css({backgroundColor:color});
                   
                   $('>nav',this).setHolder();
                   $('>nav',this).show();
                   $('>nav li:first',this).addClass('active');
               }
           }
      }, timeout: 250,
      out: function () {
          $(this).removeClass('active');
          
          if($('>ul',this).length > 0){
              $('>ul',this).hide();
          }else if($('>nav',this).length > 0){
              $('>nav',this).hide();
              $(this).css({backgroundColor:'transparent'});
          }
      }
    };
    
    $('#top-menu>ul').find('li').not('.end li').hoverIntent(config);
    
    $('#top-menu nav>ul>li').hover(function(){
        $(this).parent('ul').find('>li').removeClass('active');
        $(this).parent('ul').find('ul').hide()
        $(this).addClass('active');
        
        $('>ul',this).show();
    });
    
    $('#top-menu nav a').hover(function(){
        $(this).closest('nav').find('.menuPic>img').attr('src',($(this).attr('alt')));
    });
    
    // ************************************************************************** 
});

$(window).load(function() {
    $('#shadow-rpt').height($('#content-wrp').height()-352);
});
