// ~~~>>> 23/09/2009 - JRO => ATTENTION, POUR NOS BESOINS NOUS AVONS MODIFIE CE FICHIER, ATTENTION AU MERGE !!!
// ~~~>>> 23/09/2009 - JRO => BE CAREFUL, FOR OUR NEEDS WE HAVE MODIFIED THIS FILE, PAY ATTENTION ON THE MERGE !!!
// noConflict mode
jQuery.noConflict();

var ih; // bacgrkound image height
var iw; // bacgrkound image width
var img; // bacgrkound image element
var fh; // footer height

// when the page is fully loaded
jQuery(document).ready(function() {

      // scroll Hack for IE6
      if (navigator.userAgent.match('MSIE'))
        jQuery(document.body).prepend(jQuery('<div id="mouseScrollHack"></div>'));

    // menu preparation
    jQuery('#navigation ul').each(function() {
          var lis = jQuery(this).children('li');
        lis.each(function() {
              if ((lis.index(jQuery(this))+1) == lis.size())
              jQuery(this).addClass('last');
          });
    });

    // menu handler
    jQuery('#navigation li:not(#navigation ul li)').each(function() {
          if (jQuery(this).children('ul').size()) {
              jQuery(this).mouseenter(function() {
                 jQuery(this).addClass('active');
            });

              jQuery(this).mouseleave(function() {
                  jQuery(this).removeClass('active');
              });
         }
    });

    img = jQuery('#background img:first');
    iw = jQuery(img).width();			// image width
    ih = img.height();			// image height
    var pw = jQuery(window).width();	// page width
    var ph = jQuery(window).height();	// page height

    fh = jQuery('.footer-bg').height();
    //resizeFooter(pw, ph, fh);

    // cart handler
    jQuery('#cart-link').click(function() {
         jQuery('#cart').toggleClass('invisible').toggleClass('visible');
         jQuery('#cart-link').toggleClass('open');
    });
});

// resize on window resize
jQuery(window).resize(function() {
  refreshPage();
});

// resize footer on window load
jQuery(window).load(function() {
    var pw = jQuery(window).width();  // page width
    var ph = jQuery(window).height(); // page height
    //resizeFooter(pw, ph, fh);
  img = jQuery('#background img:first');
  iw = jQuery(img).width();			// image width
  ih = img.height();			// image height
  resizeBg(iw, ih, pw, ph);
  // ~~~>>> 2009/11/27 - JRO => MANTIS-703 : resize Bg on timeout
  //setTimeout("resizeBg(iw, ih, pw, ph)", 2000);
  // <<<~~~ 2009/11/27 - JRO <= MANTIS-703;
  setTimeout(function(){
      resizeBg(iw, ih, pw, ph);
  },2000);
});


// function executed on country change from the menu .destination
function filterPartner(version) {
    showSelectbox();
    var currentNation;

    if(version == 0) {
        currentNation = jQuery('div.shippingDestination .selectbox').text();
    }
    if(version == 1) {
        currentNation = jQuery('div.destination .selectbox').text();
    }

    if (currentNation == 'Choose country') {
        jQuery('.singlePartner').css('display','block');
        jQuery('.partnersHeader h2').text('Choose shipping destination to view appropriate Online Partners');
        jQuery('.partnersHeader h3').text('Online Partners');
        jQuery('.partnersHeader fieldset a').css('display','none');
        refreshPage();
        return;
    }

    var flag;
    partnersLists = new Array();
    partnerCountries = new Array();

    partnersLists = jQuery('.listOfCountries');

    for (i = 0; i < partnersLists.length; i++) {
        flag = false;
        partnerCountries = jQuery(partnersLists[i]).text().split(' - ');
        for (j = 0; j < partnerCountries.length; j++) {
            if (partnerCountries[j] == currentNation) {
                flag = true;
                break;
            }
        }
        if (flag == false) {
            jQuery(partnersLists[i]).parent().parent().css('display','none');
        } else {
            jQuery(partnersLists[i]).parent().parent().css('display','block');
        }
    }

    jQuery('.partnersHeader h2').text('Results');
    // ~~~>>> 2009/11/12 - FVE => MANTIS-746 : "Choose country" should appear by default
    jQuery('.partnersHeader h3').html('These Online Partners stock your item');
    // <<<~~~ 2009/11/12 - FVE <= MANTIS-746
    jQuery('.partnersHeader fieldset a').css('display','block');
    if (version == 1) {
        jQuery('.partnersHeader').css('width','502px');
    }
    refreshPage();
}


//function executed on country change from the menu .destination (version for the "about us" section)
function filterPartnerAboutUs() {
    showSelectbox();

    var currentNation = jQuery('div.shippingDestination .selectbox').text();

    if (currentNation == 'Choose country') {
        jQuery('.innerContent').children('ul').children('li').css('display','block');
        jQuery('.partnersHeader h2').text('Choose shipping destination to view appropriate Online Partners');
        jQuery('.partnersHeader fieldset a').css('display','none');
        refreshPage();
        return;
    }

    var flag;
    partnersLists = new Array();
    partnerCountries = new Array();

    partnersLists = jQuery('.countryList');

    for (i = 0; i < partnersLists.length; i++) {
        flag = false;
        partnerCountries = jQuery('li',partnersLists[i]);
        for (j = 0; j < partnerCountries.length; j++) {
            if (jQuery.trim(jQuery(partnerCountries[j]).text()) == jQuery.trim(currentNation)) {
                flag = true;
                break;
            }
        }
        if (flag == false) {
            jQuery(partnersLists[i]).parent().parent().attr('style','display: none');
        } else {
            jQuery(partnersLists[i]).parent().parent().attr('style','display: block');
        }
    }

    jQuery('.partnersHeader h2').text('Results');
    jQuery('.partnersHeader fieldset a').css('display','block');
    refreshPage();
}

// function to show selectbox of sexyCombo that disappears
function showSelectbox() {
    jQuery('div.selectbox').removeClass('invisible');
    return;
}


// resize footer adapting to short pages
/*function resizeFooter(pw, ph, fh) {
  //determino altezza del corpo pagina calcolata sugli elementi flottati
  var ch = jQuery('.floated-cont').height() + parseInt(jQuery('.content').css('padding-top')) + parseInt(jQuery('.content').css('padding-bottom'));
  jQuery('.content-container').css('height',ch);  //assegno a content container l'altezza che assumerebbe se fosse flottato

  //console.log(ch);

  var nfh = 0;
  var dh = jQuery('.header-container').height()+ch+fh;	// document height: the height of the real html page
  if (ph > dh) {
    nfh = (ph-jQuery('.header-container').height()-ch)+'px';
    jQuery('.footer-bg').css('min-height', nfh);
    jQuery('.footer-bg').css('height', nfh);
    jQuery('.footer').css('margin-top', '-'+nfh);
  } else {
    jQuery('.footer-bg').css('height', fh);
    jQuery('.footer-bg').css('min-height', fh);
    jQuery('.footer').css('margin-top', '-'+fh);
  }

}*/

//resize background image
function resizeBg(iw, ih, pw, ph) { //args: image width, image height, page width, page height */
  if (ih > ph && iw < pw) {
    img.css('width', pw+'px');
    img.css('height', ((ih*pw)/iw)+'px');
  } else if (ih < ph && iw > pw) {
    img.css('height', ph+'px');
    img.css('width', ((ph*iw)/ih)+'px');
  } else if (ih > ph && iw > pw) {
    if (((ih*pw)/iw) >= ph) {
      img.css('width', pw+'px');
      img.css('height', ((ih*pw)/iw)+'px');
    } else {
      img.css('height', ph+'px');
      img.css('width', ((ph*iw)/ih)+'px');
    }
  } else if (ih < ph && iw < pw) {
    if (((ih*pw)/iw) >= ph) {
      img.css('width', pw+'px');
      img.css('height', ((ih*pw)/iw)+'px');
    } else {
      img.css('height', ph+'px');
      img.css('width', ((ph*iw)/ih)+'px');
    }
  }
}


function refreshPage(){
    var pw = jQuery(window).width();  // page width
    var ph = jQuery(window).height(); // page height
    resizeBg(iw, ih, pw, ph);
    //resizeFooter(pw, ph, fh);
}

// <<<~~~ 23/09/2009 - JRO <= ATTENTION, POUR NOS BESOINS NOUS AVONS MODIFIE CE FICHIER, ATTENTION AU MERGE !!!
// <<<~~~ 23/09/2009 - JRO <= BE CAREFUL, FOR OUR NEEDS WE HAVE MODIFIED THIS FILE, PAY ATTENTION ON THE MERGE !!!

