var $currentSlide = 0;
var $totalSlides = 0;


function contactusComplete() {
    $('.validation-summary-errors').fadeOut(4000);
}

function contactUsSuccess() {
    $('#Close').click(toggleContact);
    if (!$('.input-validation-error').length) {
        toggleContact();
        $('#thankyou').show();
        $('#tbName').val('');
        $('#tbEmail').val('');
        $('#tbQuery').val('');
        setTimeout(function () { $("#thankyou").fadeOut(1500) }, 4000);
    }
} 

function checkBtns() {
    if ($currentSlide == ($totalSlides - 1)) {
        $('#next-btn img').hide();
    } else {
        $('#next-btn img').show();
    }

    if ($currentSlide == 0) {
        $('#prev-btn img').hide();
    } else {
        $('#prev-btn img').show();
    }


    $('#pages SPAN.on').removeClass('on');
    $("#pages SPAN:eq(" + $currentSlide + ")").addClass('on');

}

function showNextSlide() {
    var $active = $('#slideshow DIV.active');

    if ($active.length == 0) $active = $('#slideshow DIV:last');

    // use this to pull the divs in the order they appear in the markup
    var $next = $active.next().length ? $active.next()
        : $('#slideshow DIV:first');


    $active.addClass('last-active');
    $currentSlide++;
    checkBtns();

    $next.css({ opacity: 0.0 })
        .addClass('active')
        .animate({ opacity: 1.0 }, 1000, function () {
            $active.removeClass('active last-active');

        });

}

function showPrevSlide() {
    var $active = $('#slideshow DIV.active');

    if ($active.length == 0) $active = $('#slideshow DIV:first');


    var $next = $active.prev().length ? $active.prev()
        : $('#slideshow DIV:last');

    $active.addClass('last-active');
    $currentSlide--;
    checkBtns();
    $next.css({ opacity: 0.0 })
        .addClass('active')
        .animate({ opacity: 1.0 }, 1000, function () {
            $active.removeClass('active last-active');

        });
}

var mapSlideSpeed = 400;
var mapExpanded = false;

function expandMap() {
    $('#gmap').show();
    $('#overlay').show();
    $('#map').animate({
        height: "500px",
        width: "500px",
        opacity: 0
    }, mapSlideSpeed, function () {
        $('#map').hide();
    });
    $('#gmap').animate({
        opacity: 1
    }, mapSlideSpeed);
    $('#mapholder').animate({
        height: "500px",
        width: "500px"
    }, mapSlideSpeed);
    $('#overlay').animate({
        opacity: 0.6
    }, mapSlideSpeed);
    mapExpanded = true;
}

function shrinkMap() {

    $('#map').animate({
        height: "140px",
        width: "140px",
        opacity: 1
    }, mapSlideSpeed, function () {
        $('#map').show();
        $('#overlay').hide();
    });
    $('#gmap').animate({
        opacity: 0
    }, mapSlideSpeed);
    $('#mapholder').animate({
        height: "140px",
        width: "140px"
    }, mapSlideSpeed);
    $('#overlay').animate({
        opacity: 0
    }, mapSlideSpeed);
    mapExpanded = false;
    $('#gmap').hide();
}

function toggleContact() {
    $('#contact-form').slideToggle(300);
}

function resetButtons() {
    $('#web-btn').show();
    $('#windows-btn').show();
    $('#mobile-btn').show();
    $('#copy-web').hide();
    $('#copy-windows').hide();
    $('#copy-mobile').hide();
    $('#areas').removeClass();
}

function externalLinks() {
    if (!document.getElementsByTagName) return;
    var anchors = document.getElementsByTagName("a");
    for (var i = 0; i < anchors.length; i++) {
        var anchor = anchors[i];
        if (anchor.getAttribute("href") &&
       anchor.getAttribute("rel") == "external")
            anchor.target = "_blank";
    }
}

function objectToIFrame() {
    var o = document.getElementById('gmapobject');
    var i=document.createElement('iframe');
    i.setAttribute('src',o.getAttribute('data'));
    i.setAttribute('width', o.getAttribute('width'));
    i.setAttribute('height', o.getAttribute('height'));
    i.style.margin = o.style.margin;
    i.style.border = o.style.border;
    o.parentNode.replaceChild(i, o);
}


$(function () {
    //setInterval( "slideSwitch()", 5000 );
    $totalSlides = $('#slideshow .featured-project').size();
    var i = 0;

    $("#slideshow-nav #next-btn img").click(showNextSlide);
    $("#slideshow-nav #prev-btn img").click(showPrevSlide);
    checkBtns()
    for (i = 1; i <= $totalSlides; i++) {
        if (i == 1) {
            $("#pages").append("<span class=\"on\">&#8226;</span>");
        } else {
            $("#pages").append("<span>&#8226;</span>");
        }
    }

    $('#map').click(expandMap);
    $('#overlay').click(function () {
        if (mapExpanded)
            shrinkMap();
    });
    $('#contact-btn').click(toggleContact);
    $('#Close').click(toggleContact);
    $('#web-btn').click(function () {
        resetButtons();
        $('#copy-web').show();
        $('#web-btn').hide();
        $('#areas').addClass('web-selected');
    });
    $('#windows-btn').click(function () {
        resetButtons();
        $('#copy-windows').show();
        $('#windows-btn').hide();
        $('#areas').addClass('windows-selected');
    });
    $('#mobile-btn').click(function () {
        resetButtons();
        $('#copy-mobile').show();
        $('#mobile-btn').hide();
        $('#areas').addClass('mobile-selected');
    });

    checkBtns();
    /* DOCTYPE STRICT javascript changes */
    externalLinks();
    objectToIFrame();
});


