
// Mapa
function setWoj(id) {
	document.getElementById("wojSel").selectedIndex = id;
	getMiasta(id);
}

function getMiasta(id) {
	$.get('/pl-miasta', { miasto: id }, function(data) {
		mia = $('#miastoSel'); 
		mia.html(data);
		if(data)
		{
  			mia.removeAttr('disabled');
  			document.getElementById("miastoSel").selectedIndex = 0;
  		} else mia.attr('disabled', 'disabled');
	});
}
function mapOver(val) {
	//alert(val);
	$("#mp").attr("src","files/aptus/images/mapa_pl_"+val+".jpg");

}
function mapOut() {
	$('#mp').attr("src", "/files/aptus/images/mapa_pl.jpg");
}




// Frontpage languages

var aptusLanguages = new Object({

  'fi_FI' : new Object({
    flag  : 'flag_fi.png',
    text  : 'hyvinvointia_lemmikillesi.gif',
    alt   : 'suomeksi',
    cats  : 'kissoille',
    dogs  : 'koirille',
    url   : '/etusivu',
    info  : 'Orion Pharma Eläinlääkkeet, PL 425, 20101 Turku, puh. 010 4261, faksi 010 426 7771',
    show  : true
  }),

  'sv_SE' : new Object({
    flag  : 'flag_se.png',
    text  : 'products_for_your_se.png',
    alt   : '',
    cats  : 'katt',
    dogs  : 'hund',
    url   : '/frontpage_sv',
    info  : 'Orion Pharma Animal Health Djupdalsvägen 7 Box 520 192 05 Sollentuna Tel: 08 623 64 40',
    show  : true
  }),

  'da_DK' : new Object({
    flag  : 'flag_da.png',
    text  : 'products_for_your_dk.png',
    alt   : '',
    cats  : 'til katte',
    dogs  : 'til hunde',
    url   : '/frontpage_da',
    info  : 'Orion Pharma Animal Health Møllevej 9 A  2990 Nivå Tel: 49 12 67 65',
    show  : true
  }),

  'no_NO' : new Object({
    flag  : 'flag_no.png',
    text  : 'products_for_your_no.png',
    alt   : '',
    cats  : 'for katter',
    dogs  : 'for hunder',
    url   : '/frontpage_no',
    info  : 'Orion Pharma AS Animal Health Gjerdrums vei 8 0402 Oslo Tel: 4000 4190',
    show  : true
  }),

  'pl_PL' : new Object({
    flag  : 'flag_pl.png',
    text  : 'products_for_your_pl.gif',
    alt   : '',
    cats  : 'dla kotów',
    dogs  : 'dla psów',
    url   : '/frontpage_pl',
    info  : 'Orion Oyj S.A. Przedstawicielstowo w Polsce 01-699 Warszawa, ul. Parandowskiego 19  Tel./fax 22 833 31 77, 832 10 36, 832 10 37',
    show  : true
  }),

  'hu_HU' : new Object({
    flag  : 'flag_hu.png',
    text  : 'products_for_your_hu.gif',
    alt   : '',
    cats  : 'macskáknak',
    dogs  : 'kutyáknak',
    url   : '/frontpage_hu',
    info  : 'Orion Pharma Kft. 1139 Budapest, Pap Károly u. 4-6 Tel.: +36 1 239 9095, fax +36 1 237 0603',
    show  : true
  }),

  'sk_SK' : new Object({
    flag  : 'flag_sk.png',
    text  : 'products_for_your_pets_well_being.gif',
    alt   : '',
    cats  : 'pre mačky',
    dogs  : 'pre psov',
    url   : '/frontpage_sk',
    info  : '',
    show  : true
  }),

  'cs_CZ' : new Object({
    flag  : 'flag_cs.png',
    text  : 'products_for_your_pets_well_being.gif',
    alt   : '',
    cats  : 'pro kočky',
    dogs  : 'pro psy',
    url   : '/frontpage_cz',
    info  : 'Orion Oyj Zelený pruh 95/97, 140 00, Praha, ČR Tel.: +420 227 027 263',
    show  : true
  }),

  'en_GB' : new Object({
    flag  : 'in_english2.png',
    text  : 'products_for_your_pets_well_being.gif',
    alt   : 'in english',
    cats  : 'for cats',
    dogs  : 'for dogs',
    url   : '/frontpage',
    info  : '',
    show  : true
  })

});


// Product navi links
var productNavi = new Array();

// Frontpage timeouts
var timeouts = new Object;

var selectedCountry = '';

// Init
$(document).ready( function () {

  //
  // Frontpage
  //

  // Country set
  var countryRegexp = new RegExp('country=([^&]*)');
  var country = countryRegexp.exec(document.location.href);
  if (!country) {
    timeouts.frontpage     = 1500;
    timeouts.flags         = 3000;
    timeouts.flagFade      = 1000;
    timeouts.flagDelay     = 500;
    timeouts.chooseCountry = 4500;
  }

  // Yes
  else {
    selectedCountry = country[1];
    timeouts.frontpage     = 50;
    timeouts.flags         = 50;
    timeouts.flagFade      = 50
    timeouts.flagDelay     = 50;
    timeouts.chooseCountry = 50;
  }

  // Set Aptus logo opacity
  $("#frontpage_logo img").css({"opacity": 0});

  // Add flags
  for (countryId in aptusLanguages) {
    var country = aptusLanguages[countryId];

    var flagOpacity = 1;
    // if (selectedCountry && countryId != selectedCountry)
    //   flagOpacity = 0.25;

    $("#frontpage_flags").append(
      $(document.createElement("a"))
        .attr("id", countryId)
        .css("cursor", "pointer")
        .css("opacity", flagOpacity)
        .click( function () {

          var country = aptusLanguages[ $(this).attr("id") ];
          // $("#frontpage_flags a").css("opacity", 1).not(this).css("opacity", 0.25)

          $("#frontpage_animalSelect").hide();
          $("#frontpage_underConstruction").hide();
          $("#frontpage_contactInfo").html("").hide();

          if (country.show) {
            hideChooseCountry();
            if (country.text) {
              $("#frontpage_text img").attr({"src": "/files/aptus/frontpage/"+ country.text});
            }

            // Url
            $("#frontpage_animalSelect a").attr("href", country.url);

            // Localized cat/dog texts & frontpage url
            $("#frontpage_animalSelect a.cats span").html( country.cats );
            $("#frontpage_animalSelect a.dogs span").html( country.dogs );
    
            $("#frontpage_animalSelect").fadeIn(750);
          }
          else {
            $("#frontpage_underConstruction").fadeIn(750);
          }

          if (country.info) {
            $("#frontpage_contactInfo").html(country.info).fadeIn(750);
          }
        })
        
        .append(
          $(document.createElement("img"))
            .attr("src", "/files/aptus/frontpage/"+ country.flag)
            .attr("alt", country.alt)
            .attr("title", country.alt)
        )
        
      /*
      '<a id="'+ countryId +'" href="#">'+
      '<img src="/files/aptus/frontpage/'+ country.flag +'" alt="'+ country.alt +'" title="'+ country.alt +'" />'+
      '</a>'
      */
    );
  }

  // Frontpage delay
  setTimeout(showFrontpage, timeouts.frontpage);

  // Flag delay
  setTimeout(showFlags, timeouts.flags);

  if (!selectedCountry) {
    setTimeout(showChooseCountry, timeouts.chooseCountry);
  }

  // Animal cookie
  $("#frontpage_animalSelect a.cats").click( function () { setCookie("aptus_animal", "Cat"); });
  $("#frontpage_animalSelect a.dogs").click( function () { setCookie("aptus_animal", "Dog"); });

  // Animal hovers
  $("#frontpage_animalSelect a.cats, #frontpage_animalSelect a.dogs, #animal_select a").hover(
    function () { $(this).find("img").each ( function () { this.src = this.src.replace("_off", "_on"); }) },
    function () { $(this).find("img").each ( function () { this.src = this.src.replace("_on", "_off"); }) }
  );

  $("#frontpage_animalSelect a.cats").hover(
    function () { $("div#frontpage_animalSelect").css("background-image", "url('/files/aptus/frontpage/frontpage_animal_background_left.jpg')") },
    function () { $("div#frontpage_animalSelect").css("background-image", "url('/files/aptus/frontpage/frontpage_animal_background_off.jpg')") }
  );

  $("#frontpage_animalSelect a.dogs").hover(
    function () { $("div#frontpage_animalSelect").css("background-image", "url('/files/aptus/frontpage/frontpage_animal_background_right.jpg')") },
    function () { $("div#frontpage_animalSelect").css("background-image", "url('/files/aptus/frontpage/frontpage_animal_background_off.jpg')") }
  );


  //
  // Other pages
  //

  // Navigation image hover
  $("#page_navi a.off").hover(
    function () { $(this).find("img").each ( function () { this.src = this.src.replace("_off", "_on"); }) },
    function () { $(this).find("img").each ( function () { this.src = this.src.replace("_on", "_off"); }) }
  );


  // First navi item (add productNavi)
  $("#page_navi a:first").after(
    '<div class="subnavi">'+ productNavi.join("") +'</div>'
  );


  // Subnavi hovers
  $("#page_navi a.navi").each( function () {
    var subnaviLevel = 1;
    $(this).next(".subnavi").find("a").each( function () {
      var classOff = "subnavi_off_"+ subnaviLevel;
      var classOn  = "subnavi_on_"+ subnaviLevel;
      $(this)
        .addClass(classOff)
        .hover(
          function () { $(this).toggleClass(classOff).toggleClass(classOn) },
          function () { $(this).toggleClass(classOn).toggleClass(classOff) }
        );
      if (subnaviLevel < 4) subnaviLevel++
    });
  });


  // Last navi item
  $("#page_navi a.navi:last").addClass("last");


  // Navigation hover
  $("#page_navi a.navi").hover(
    function () {
      var pcPos = $(".page_container").offset();
      var pos = $(this).offset();
      var width = $(this).width();

      $(this).next(".subnavi").css({
        top: pos.top + $(this).height() - 20,
        left: pos.left - 10 - pcPos.left,
        width: width
      });
      $("#page_navi .subnavi").hide();
      $(this).next(".subnavi").show();
    },
    function () {}
  );

  // Subnavi hover timout
  var subnaviTimeout = '';
  $("#page_navi .subnavi").hover(
    function () { clearTimeout(subnaviTimeout) },
    function () { subnaviTimeout = setTimeout(hideSubnavi, 1000) }
  );

  function hideSubnavi () {
    $("#page_navi .subnavi").hide();
  }


  // Page images
  if (! $("#page_image").find("img") ) {
  }


  // Competition image hover
  $("a.competition").hover(
    function () { $(this).find("img").each ( function () { this.src = this.src.replace("_off", "_on"); }) },
    function () { $(this).find("img").each ( function () { this.src = this.src.replace("_on", "_off"); }) }
  );


});


// Show frontpage
function showFrontpage () {

  // Animate logo
  $("#frontpage_logo img")
    .animate({
      top: 0,
      left: 0,
      width: 250,
      height: 250,
      opacity: 1.0
    }, 250);

  // Text
  $("#frontpage_text img").fadeIn(2500);
}


// Show flags
function showFlags () {
  var flagDelay = 0;
  // var selectedCountryElement = '';

  $("#frontpage_flags a").each( function () {

    var countryId = this.id;
    var country = aptusLanguages[countryId];

    // Fade non-published flag
    if (!country.show) {
      $(this).find("img").css({opacity: 0.25, cursor: "default"});
    }

    var flagFade = timeouts.flagFade + (flagDelay * timeouts.flagDelay);
    $(this).fadeIn(flagFade).css({"visibility": "visible"});
    flagDelay++;
  });

  if (selectedCountry)
  {
    $("#frontpage_flags a#"+ selectedCountry).trigger("click");
  }
}


function showChooseCountry () {
  $("#frontpage_flags img.choose_country").fadeIn(500).css({"visibility": "visible"});
}

function hideChooseCountry () {
  $("#frontpage_flags img.choose_country").css({"visibility": "hidden"});
}


// Sets cookie
function setCookie (name, value) {
  var expires = new Date();
  expires.setTime( expires.getTime() + (3600 * 24 * 31 * 3) );
  document.cookie = name +"="+ value +"; expires="+ expires +";";
}
