/* (c)2011 DSJ Productions */

var s24_host_protocol = (("https:" == document.location.protocol) ? "https://" : "http://");
var s24_host = "footprinttravel.nl";
var s24_path = "";
var PATH = s24_host_protocol + s24_host + s24_path;

function doAddToBasket(tour_id, page_identifier)
{
    $.ajax({
        url: PATH+'/ajax_handler.php?action=add_quote_line&tour_id='+tour_id+'&page_identifier='+page_identifier, 
        type: 'get',
        success:function(t)
        {
            if(page_identifier != 'quote' || t.substring(0,10)=="CONFIRM###")
                alrt(t);
            if((page_identifier == 'quote' && t.substring(0,10)!="CONFIRM###") || t.substring(0,10)=="REFRESH###")
            {
                document.location.href = document.location;
            }
        }
    });    
    $('.quoteCount .quoteCountContent').text(parseInt($('.quoteCount .quoteCountContent').text())+1);
    $('.quoteCount').show();
}

function removeQuoteLine(quote_line_id, page_identifier)
{
    $.ajax({
        url: PATH+'/ajax_handler.php?action=remove_quote_line&quote_line_id='+quote_line_id, 
        type: 'get',
        success:function(t)
        {
            //alrt(t);
        }
    });    
}

function saveBookingForm()
{
    $('#booking_form').ajaxSubmit({
        success:function(data,status,xhr)
        {
            showAlrt(data);
        }
    });
}

function addFlightToBooking(booking_id, checksum)
{
    $.ajax({
        url: PATH+'/ajax_handler.php?action=add_flight&booking_id='+booking_id+'&checksum='+checksum, 
        type: 'get',
        success:function(t)
        {
            document.location = document.location;
        }
    });    
}

function submitTourComment() 
{ 
    $('#tour_comment').ajaxSubmit(
    {
        success:function(t,s,x)
        {
            showAlrt(t);
        }
    }); 
    return false; 
}

function doSubscribeNewsletter()
{
    $('#newsletter').ajaxSubmit({
        success: function(t,s,x)
        {
            alrt(t);
            if(x.getResponseHeader('Store24-ResponseCode') == '1')
            {
                window.setTimeout(function(){
                    document.location = PATH + '/';
                }, 5000);
            }
        }
    });
}


function showAlrt(identifier)
{
    $.ajax({url:PATH+'/ajax_handler.php?action=get_alert&alert_identifier='+identifier, success:function(t){alrt(t); }});    
}

function alrt(c)
{
    var confirm = false;
    if(c.substring(0,10)=="CONFIRM###")
    {
        confirm = true;
        c = c.substring(10, c.length);
    }
    else if(c.substring(0,10)=="REFRESH###")
    {
        c = c.substring(10, c.length);
        document.location.href = document.location;
    }
    $('object').hide();
    $('#full_bg').fadeIn(800);
    $('#alertWrapper').show();
    $('#alertContent').html(c);
    
    if(confirm == false)
    {
        $('#alert').animate({top:"-200px"});
        $('#alert').delay(3000).animate({top:"-340px"});
        $('#full_bg').delay(2500).fadeOut(800);
        window.setTimeout(function()
        {
            $('object').show();
        },2800);
    }
    else
    {
        $('#alert').animate({top:"-100px"});
    }
}

function closeAlrt()
{
    $('#alert').animate({top:"-200px"});
    $('#alert').animate({top:"-340px"});
    $('#full_bg').fadeOut(800);
    $('object').show();
}

function getInnerWidth(win) {
  var winWidth;
  if (win.innerWidth) {
    winWidth = win.innerWidth;
  }
  else if (win.document.documentElement && win.document.documentElement.clientWidth) {
    winWidth = win.document.documentElement.clientWidth;
  }
  else if (document.body) {
    winWidth = win.document.body.clientWidth;
  }
  return winWidth;
}

function positionAlert()
{
    if($('#alert').length>0)
    {
        documentWidth = getInnerWidth(self);
        document.getElementById('alert').style.left = ((documentWidth / 2) - 200)+'px';
        if(document.getElementById('contactOverlay'))
            document.getElementById('contactOverlay').style.left = ((documentWidth / 2) - 200)+'px';
        windowLoaded = true; 
    }
}

function recordOutboundLink(linkHref, category, action) {
  try {
    var pageTracker=_gat._getTracker("UA-XXXXX-X");
    pageTracker._trackEvent(category, action);
    setTimeout('document.location = "' + linkHref + '"', 100)
  }catch(err){}
}

function strstr( haystack, needle, bool ) {
    var pos = 0;
 
    haystack += '';
    pos = haystack.indexOf( needle );
    if( pos == -1 ){
        return false;
    } else{
        if( bool ){
            return haystack.substr( 0, pos );
        } else{
            return haystack.slice( pos );
        }
    }
}
function get_hostname_from_url(url) 
{
    return url.match(/:\/\/(.[^/]+)/)[1];
}

$(document).ready(function() 
{
    positionAlert();
    
    $(".quoteTableParent").click(function() {
        $(this).next().toggle(500);
    });
    
    $('a').click(function()
    {
        var linkHref = $(this).attr('href');
        if(strstr(linkHref, 'http') && !strstr(linkHref, 'footprinttravel.nl'))
        {
            if($(this).attr('target') == '_blank')
            {
                _gaq.push(['_trackEvent', 'Uitgaande links', 'klik', linkHref]);
            }
            else
            {
                recordOutboundLink(linkHref, "Uitgaande links", "klik");
                return false;
            }
        }
    });
    
    $('img').css('height', 'auto');
    
});

