/* slidemeue PlugIn START */

/*********************
/**
 * @global => ACTIONS for PC & EWT after EMO
 */
(function($){
    $.triumpgBgImg = (function(){
        var viewport, imgSize, imgDim, staticImg, imgNum;
        function calcViewport(){
            viewport = {
                height: $(window).height(),
                width: $(window).width()
            };
        }
        
        function calcImgSize(img){
            if (img[0]) {
                imgDim = {
                    height: img.height(),
                    width: img.width()
                };
            }
        }
        
        function adjustBgImg(){
            img = staticImg;
            calcViewport();
            
            var windowRatio = viewport.width / viewport.height, imgRatio = imgDim.width / imgDim.height;
            if (windowRatio < imgRatio) {
                img.css({
                    width: 'auto',
                    height: viewport.height + 'px'
                });
            }
            else {
                img.css({
                    width: '100%',
                    height: 'auto'
                });
            }
        }
        function showNext(){
        
            staticImg = $('#bg-img img:last');
            calcImgSize(staticImg);
            adjustBgImg(staticImg);
            $('#overlay').animate({
                opacity: 0.9,
                backgroundColor: '#ffffff'
            }, {
                duration: 400,
                complete: function(){
                    staticImg.css({
                        opacity: 1
                    });
                    $('#bg-img img:first').remove();
                }
            }).animate({
                opacity: 0.5,
                backgroundColor: '#000000'
            }, {
                duration: 400
            });
        }
        function loadNext(path){
            var img = new Image();
            img.src = path;
            $(img).css({
                opacity: 0
            }).appendTo($('#bg-img')[0]);
            if (img.complete) {
                showNext();
            }
            else {
                $(img).load(showNext);
            }
            
        }
        
        return {
            init: function(){
                staticImg = $('#bg-img img:first');
                if (staticImg && staticImg[0]) {
                    if (staticImg[0].complete) {
                        calcImgSize(staticImg);
                        adjustBgImg(staticImg);
                        $(window).bind('resize', adjustBgImg);
                    }
                    else {
                        staticImg.bind('load', function(){
                            calcImgSize(staticImg);
                            adjustBgImg(staticImg);
                        });
                        $(window).bind('resize', adjustBgImg);
                    }
                }
                
            },
            loadNext: loadNext
        
        };
    })();
    (function($){
        $.imgPreLoad = (function(){
            var srcList = [], ready = false, started = false;
            function loadImg(){
                if (srcList.length) {
                    started = true;
                    var src = srcList.shift(), img = new Image();
                    img.src = src;
                    if (img.complete) {
                        loadImg();
                    }
                    else {
                        $(img).load(loadImg);
                    }
                }
                else {
                    started = false;
                }
            }
            return {
                add: function(src){
                    srcList.push(src);
                    if (ready && !started) {
                        loadImg();
                    }
                },
                ready: function(){
                    ready = true;
                    loadImg();
                }
            };
        })();
        $(window).bind('load', $.imgPreLoad.ready);
    })(jQuery);
    
    
    function callGlobal(){
        $.triumpgBgImg.init();
        
        /* IhatE U */
        if ($.browser.msie) {
            var overlay = $('#overlay'), opac = (overlay.is('.mod')) ? 0 : 0.5;
            overlay.css({
                'opacity': opac
            });
        }
    }
    $(callGlobal);
})(jQuery);


function setC(name, value, expires, path, domain, secure){
  // set time, it's in milliseconds
  var today = new Date();
  today.setTime( today.getTime() );
  
  if(expires){
    expires = expires * 1000 * 60 * 60 * 24;
  }
  var expires_date = new Date(today.getTime() + (expires));
  
  document.cookie = name + "=" +escape( value ) +
  ((expires) ? ";expires=" + expires_date.toGMTString() : "") +
  ((path) ? ";path=" + path : "") +
  ((domain) ? ";domain=" + domain : "") +
  ((secure) ? ";secure" : "");
}

function getC(check_name){
  // first we'll split this cookie up into name/value pairs
  // note: document.cookie only returns name=value, not the other components
  var a_all_cookies = document.cookie.split( ';' );
  var a_temp_cookie = '';
  var cookie_name = '';
  var cookie_value = '';
  var b_cookie_found = false; // set boolean t/f default f

  for ( i = 0; i < a_all_cookies.length; i++ )
  {
    // now we'll split apart each name=value pair
    a_temp_cookie = a_all_cookies[i].split( '=' );


    // and trim left/right whitespace while we're at it
    cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

    // if the extracted name matches passed check_name
    if ( cookie_name == check_name )
    {
      b_cookie_found = true;
      // we need to handle case where cookie has no value but exists (no = sign, that is):
      if ( a_temp_cookie.length > 1 )
      {
        cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
      }
      // note that in cases where cookie is initialized but no value, null is returned
      return cookie_value;
      break;
    }
    a_temp_cookie = null;
    cookie_name = '';
  }
  if ( !b_cookie_found )
  {
    return null;
  }
}

function delC( name, path, domain ) {
  if ( getC( name ) ) document.cookie = name + "=" +
  ( ( path ) ? ";path=" + path : "") +
  ( ( domain ) ? ";domain=" + domain : "" ) +
  ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

function setBacklinkCookie(url, flash){
  setC('triumph_bcklnk',url,'','/','','');
}


$(document).ready(function(){
//delete dadLinks from MainNavi
  if(cmsflash == false){
     var $mainmenu=$("#slidemenu ul li:not(#slidemenu ul li ul li)");
     $mainmenu.each(function (index, domEle) {
      if($('ul',domEle).length > 0){
        $(this).children("a:eq(0)").attr('href','#');
      }
    })
    $('.navPO a').attr('href','#');
  }
//get Cookie and set backlink on pc if true
  if($('#catalogue_back').length==1){
    if (getC('triumph_bcklnk')){
      backURL = getC('triumph_bcklnk');
      $('#catalogue_back a').attr('href',backURL);
      return false;
    }else{
      $('#catalogue_back').remove();
    }
  }
//search about title="backlink" to set cookie and parse the url
  allTitleLinks = $('a[name="backlink"]');
  allTitleLinks.each(function(){
    $(this).click(function(){
      go2URL = $(this).attr('href');
      $(this).attr('href','#');
      back2URL = window.location.href;
      setBacklinkCookie(back2URL,false);
      window.location.href = go2URL;
    }
    );
  }
  );
});

