
jQuery.noConflict();

var ih; // background imatge height
var iw; // background imatge width
var img; // background imatge element
var fh; // base height

// quan la pàgina és carregada completament
jQuery(document).ready(function() {

      // scroll Hack for IE6
      if (navigator.userAgent.match('MSIE'))
        jQuery(document.body).prepend(jQuery('<div id="mouseScrollHack"></div>'));

    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');
          });
    });

    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();			// imatge width
    ih = img.height();			// imatge height
    var pw = jQuery(window).width();	// pagina width
    var ph = jQuery(window).height();	// pagina height

    fh = jQuery('.footer-bg').height();

    jQuery('#cart-link').click(function() {
         jQuery('#cart').toggleClass('invisible').toggleClass('visible');
         jQuery('#cart-link').toggleClass('open');
    });
});

// redimensiona al redimensionar navegador
jQuery(window).resize(function() {
  refreshPage();
});

jQuery(window).load(function() {
    var pw = jQuery(window).width();  // pagina width
    var ph = jQuery(window).height(); // pagina height
  img = jQuery('#background img:first');
  iw = jQuery(img).width();			// imatge width
  ih = img.height();			// imatge height
  resizeBg(iw, ih, pw, ph);

  setTimeout(function(){
      resizeBg(iw, ih, pw, ph);
  },2000);
});


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');
    jQuery('.partnersHeader h3').html('These Online Partners stock your item');
    jQuery('.partnersHeader fieldset a').css('display','block');
    if (version == 1) {
        jQuery('.partnersHeader').css('width','502px');
    }
    refreshPage();
}



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;
}


//redimensiona imatge fons
function resizeBg(iw, ih, pw, ph) { // imatge width, imatge height, pagina width, pagina 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();  // pagina width
    var ph = jQuery(window).height(); // pagina height
    resizeBg(iw, ih, pw, ph);
    //resizeFooter(pw, ph, fh);
}
