/** JQUERY EXTENSION TO GRAB URL VARIABLES
--------------------------------------------------------------------------------------------------------------------------------------------*/
$.extend({
  getUrlVars: function(){
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
      hash = hashes[i].split('=');
      vars.push(hash[0]);
      vars[hash[0]] = hash[1];
    }
    return vars;
  },
  getUrlVar: function(name){
    return $.getUrlVars()[name];
  }
});



/** HEADER MENU
--------------------------------------------------------------------------------------------------------------------------------------------*/
$(function() {
    $("ul#hd_menu li").hover(
        function() {
            $(this).children("ul").show();
        },
        function() {
            $(this).children("ul").hide();
        }
    );
});




/** CONTENT SLIDER (used at homepage news)
--------------------------------------------------------------------------------------------------------------------------------------------*/
$(function() {

    var currentPosition = 0;

    // Get active module to define the slider width and id's to work with
    var module = $.getUrlVar('mod');
    if ((module=="news") || (module=="projects")) {
        var typologySlider = $.getUrlVar('view');
        if (typologySlider=="houses") {
            var slideWidth = 655;
        } else {
            var slideWidth = 948;
        }
    } else {
        var slideWidth = 630;
    }

    var slides = $('.slide');
    var numberOfSlides = slides.length;

  // Remove scrollbar in JS
  $('#blocksContainer').css('overflow', 'hidden');

  // Wrap all .slides with #slideInner div
  slides
    .wrapAll('<div id="slideInner"></div>')
    // Float left to display horizontally, readjust .slides width
    .css({
      'float' : 'left',
      'width' : slideWidth
    });

  // Set #slideInner width equal to total width of all slides
  $('#slideInner').css('width', slideWidth * numberOfSlides);

  // Insert controls in the DOM
  $('#newsScrollHome')
    .prepend('<span class="control icon arrow_slide_left" id="leftControl">Clique para andar para a esquerda</span>')
    .append('<span class="control icon arrow_slide_right" id="rightControl">Clique para andar para a direita</span>');

  // Hide left arrow control on first load
  manageControls(currentPosition);

  // Create event listeners for .controls clicks
  $('.control')
    .bind('click', function(){
    // Determine new position
    currentPosition = ($(this).attr('id')=='rightControl') ? currentPosition+1 : currentPosition-1;

    // Hide / show controls
    manageControls(currentPosition);
    // Move slideInner using margin-left
    $('#slideInner').animate({
      'marginLeft' : slideWidth*(-currentPosition)
    });
  });

  // manageControls: Hides and Shows controls depending on currentPosition
  function manageControls(position){
    // Hide left arrow if position is first slide
    if(position==0){ $('#leftControl').hide() } else{ $('#leftControl').show() }
    // Hide right arrow if position is last slide
    if(position==numberOfSlides-1){ $('#rightControl').hide() } else{ $('#rightControl').show() }
  }
});



/** HANDLER FOR CLASS "OpenNewWindow"
--------------------------------------------------------------------------------------------------------------------------------------------*/
 $(function() {
    $('a.OpenNewWindow').click(function(){
        window.open($(this).attr('href'));
        return false;
    });
 });


/** Function to check if the link that's being requested exists or returns sone HTTP Status Code
--------------------------------------------------------------------------------------------------------------------------------------------*/
function UrlExists(url, cb){
    jQuery.ajax({
        url:      url,
        dataType: 'text',
        type:     'GET',
        complete:  function(xhr){
            if(typeof cb === 'function')
               cb.apply(this, [xhr.status]);
        }
    });
}

/*
UrlExists('/path/script.pl', function(status){
    if(status === 200){
       // file was found
    }
    else if(status === 404){
       // 404 not found
    }
});
*/

