jQuery.noConflict();

jQuery(window).ready(function(){
  jQuery.fx.off = jQuery.browser.msie && jQuery.browser.version.match(/^6\./);
  var loadCount = 0;
  jQuery('[include]').each(function () {
    ++loadCount;
    var self = jQuery(this);
    var url = self.attr('include');
    jQuery.get(url,function (data) {
      self.replaceWith(data);
      if (--loadCount == 0) go();
    });
  });
  if (!loadCount) go();
});

function nop() {}

function fixBkPng() {
  this.runtimeStyle.behavior="none";
  var url=this.currentStyle.backgroundImage;
  if (url.match(/\.png/i)) {
    url=url.match(/\(['"]?(.*?)['"]?\)/)[1];
    this.runtimeStyle.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+url+"',sizingMethod='crop')";
    this.runtimeStyle.backgroundImage="none";
  }
}

function go() {
  jQuery('#search').keypress(function (e) {
    if (e.which == 13) search();
  });
  jQuery('#subscribeField').keypress(function (e) {
    if (e.which == 13) subscribe();
  });
  var area = window.location.search.match(/\?\/(\w+)/);
  area = !area ? 'index' : area[1];
  var f = window['area_' + area];
  if (f) f();
  else if (window['console'])
    console.error('function area_'+area+'() not found');
}

var oldTab;

function initTabs() {
  oldTab = 0;
  jQuery('#tab_slider > div').hide();
  jQuery('#tab1').show();
  jQuery('#content_nav li:first a').addClass('activeSlide');
  jQuery('#main_content_slider li').click(function () {
    var idx = jQuery(this).parent().children().index(this);
    jQuery('#main_content_slider li a').removeClass('activeSlide');
    jQuery('#main_content_slider li:eq(' + idx + ') a').addClass('activeSlide');
    //if (jQuery.browser.msie) {
      jQuery('#tab' + (oldTab + 1)).hide();
      jQuery('#tab' + (idx + 1)).show();
    /*}
    else jQuery('#tab' + (oldTab + 1)).fadeOut('fast',function() {
      jQuery('#tab' + (idx + 1)).fadeIn();
    });*/
    oldTab = idx;
  });
}

function expandText(e) {
  jQuery(e).closest('.newsItem').find('.shortText').hide().next().show();
  jQuery(e).hide();
}

function area_index() {
  initTabs();
  jQuery('#slideshow').cycle({
      fx:     'fade',
      speed:  'slow',
      timeout: 3000,
      pager:  '#slider_nav',
      pagerAnchorBuilder: function(idx, slide) {
          // return sel string for existing anchor
          return '#slider_nav li:eq(' + (idx) + ') a';
      }
  });
}

function area_sobre() {
  initTabs();
}

function area_noticias() {
  /*var id = location.hash;
  if (id) {
    id = id.substr(1); //articleId
    var e = jQuery('a[name=' + id + ']');
    id = e.parent('.content').parent('div').attr('id'); //tabId
    var idx = parseInt(id.substr(3)) - 1;
    jQuery('#main_content_slider li:eq(' + idx + ')').click();
  }*/
  var subkey = parseInt(location.search.match(/noticias\/(\d)/)[1]);
  jQuery('#main_content_slider li a').removeClass('activeSlide');
  jQuery('#main_content_slider li:eq(' + (subkey - 1) + ') a').addClass('activeSlide');
  /*jQuery('#main_content_slider li').click(function () {
    var idx = jQuery(this).parent().children().index(this) + 1;
    location.href = '?/noticias/' + idx;
  });*/
}

function area_noticia() {
  initTabs();
}

function area_eventos() {
  initTabs();
}

function area_evento() {
  initTabs();
}

function area_membros() {
  initTabs();
}

function area_links() {
  initTabs();
}

function area_contactos() {
  initTabs();
}

function area_pesquisa() {
  initTabs();
}

function search() {
  var query = encodeURIComponent(jQuery('#searchField').val());
  if (query == '' || query == 'Pesquisar') return;
  location = '?/pesquisa&q=' + query;
}

function subscribe() {
  var email = jQuery('#subscribeField').val();
  if (email.indexOf('@') > 0) {
    jQuery('#subscribeField').val('Email').blur();
    jQuery.get('var/modules/aicep/newsletters/services/subscribe.php',{email:email},function (data) {
      if (jQuery.trim(data) == 'ok=1')
        alert('Obrigado por subscrever a nossa newsletter!');
      else alert('Lamentamos, mas não foi possível subscrever a newsletter neste momento.');
    });
  }
  else alert('Por favor introduza um endereço de email.');
}

function prevPage(actual) {
  if (actual > 1)
    location.href = location.href.replace(/&p=\d+/,'&p=' + (actual - 1));
}

function nextPage(actual,max) {
  if (actual < max)
    location.href = location.href.replace(/#.*/,'').replace(/&p=\d+|$/,'&p=' + (actual + 1));
}

