var timeout    = 500;
var closetimer = 0;
var ddmenuitem = 0;

function jsddm_open()
{
   jsddm_canceltimer();
   jsddm_close();
   ddmenuitem = $(this).find('ul').css('visibility', 'visible');
}
   

function jsddm_close()
{
    if(ddmenuitem) ddmenuitem.css('visibility', 'hidden');
}

function jsddm_timer()
{  
    closetimer = window.setTimeout(jsddm_close, timeout);
}

function jsddm_canceltimer()
{  if(closetimer)
   {  window.clearTimeout(closetimer);
      closetimer = null;
   }
}

$(document).ready(function()
{
    

    $('.menuitem').hover(function(){
        //$('.mainLilHeader').html($(this).attr('miOpen'));
//        $('.mainBigHeader').append($(this).attr('miOpen'));
//        if(($(this).attr('miOpen')!=undefined)&&($(this).attr('miOpen')=='true')){return;}
//        $(this).attr('miOpen','true');
//        $('.mainBigHeader').append($(this).attr('miOpen'));

       $(this).find('a').width(630);
       needWidth = 430 - $(this).find('span').width();
       __this = $(this)
       if(needWidth<0)
        $(this).parent().animate({paddingLeft:200+needWidth,width:650-needWidth},500,function(){ $(this).find('a').width(630);});

    },function(){

        
//        if(($(this).attr('miClose')!=undefined)&&($(this).attr('miClose')=='true')){return;}
//        $(this).attr('miClose','true');
//        $('.mainLilHeader').append($(this).attr('miClose'));
//          __this = $(this)

        $(this).parent().animate({paddingLeft:200,width:650},500);
        $(this).find('a').width(430);
         
    });
    
//    $('#jsddm li ul').each(function(){
//        height = $(this).css('height');
//        //defBodyHeight = $('body').css('height');
//        defpageLayoutHeight = $('.pageLayout').css('height');
//        $(this).hover(
//        function()
//        {
//            //alert($(this).css('height'));
//            if((defpageLayoutHeight.replace("px","")-0)<($(this).css('height').replace("px","")-0))
//            {
//                    $('.pageLayout').css('height',$(this).css('height'));
//                   // $('body').css('height',$(this).css('height'));
//             }
//            //$('html').css('height',$(this).css('height'));
//
//        },
//        function()
//        {
//            //alert(defBodyHeight);
//             $('.pageLayout').css('height',defpageLayoutHeight);
//           // $('body').css('height',defBodyHeight);
//           // $('html').css('height',$(this).css('height'));
//        }
//        );
//
//    });

    // initialize scrollable
    $(".scrollable").scrollable({size: 6 }).navigator();
    $(".scrollable .items div").each(function(){
        $(this).click(function(){
            var bigsrc = $(this).find('a').attr('href');
            var title = $(this).find('a').attr('title');
            var  src= $(this).find(".bigHref").attr('href');
            
            if(src==undefined)
            {
                src="/img/icon-nenabled.png";
                title = "can't find image";
            }
                //alert(1);
                //$('.bigimage #zoomBigPhoto').css({'opacity':'0.0'});
                $('.bigimage img').animate({
                            opacity:0.0
                 },500);
                  $('#bigImageTilte').html('Loading...');
                
                setTimeout(function(){$.preloadImages(src, function () {
                        
                        
                        $('#bigImageTilte').html(title);
                        //$('.bigimage a').attr('href',bigsrc);
                        //$('.bigimage #zoomBigPhoto').attr('href',bigsrc+"&html");
                        //alert($('.bigimage a').attr('href'));
                        
                        $('.bigimage img').attr('src',src);
                        
                         setTimeout(function(){ $('.bigimage img').animate({opacity:1.0 },500);},300);
                         $('.bigimage a').attr('href',bigsrc+"&html");
                        
                        //setTimeout(function(){ $('.bigimage #zoomBigPhoto').css({'opacity':'1'});},700);
                    });},500);
            });

    });


    $('#jsddm > li').bind('mouseover', jsddm_open)
   $('#jsddm > li').bind('mouseout',  jsddm_timer)
}
);
   jQuery.preloadImages = function () {
        if (typeof arguments[arguments.length - 1] == 'function') {
            var callback = arguments[arguments.length - 1];
        } else {
            var callback = false;
        }
        if (typeof arguments[0] == 'object') {
            var images = arguments[0];
            var n = images.length;
        } else {
            var images = arguments;
            var n = images.length - 1;
        }
        var not_loaded = n;
        for (var i = 0; i < n; i++) {
            jQuery(new Image()).load(function() {
                if (--not_loaded < 1 && typeof callback == 'function') {
                    callback();
                }
            }).attr('src', images[i]);
        }
}

document.onclick = jsddm_close;

