/*
 * Primary Scripts
 * I &heart; HTML5 Boilerplate
 * Author       : Ciobanu N. Cristian <ciobanu.n.cristian@gmail.com>
 * Created on   : Dec 22, 2010, 4:00:00 PM
 */
var logos_smf = 0;

$(document).ready(function() {
    // Default AJAX call settings
    $.ajaxSetup({
        cache: false,
        timeout: 5000,
        error:function(x,e){
            if('parsererror'==e) {
                notification('Sorry, we ran into a technical problem (parse error). Please try again...', 'red');
            } else if('timeout'==e) {
                notification('Request timed out. Please try again...', 'red');
            } else if ( "status" in x ) {
                if(0 == x.status) {
                    notification('You are offline! Please check your network.', 'red');
                } else if (404 == x.status) {
                    notification('Sorry, we ran into a technical problem (404). Please try again...', 'red');
                } else if (500 == x.status) {
                    notification('Sorry, we ran into a technical problem (500). Please try again...', 'red');
                }
            } else {
                notification('Sorry, we ran into a technical problem (unknown error). Please try again...', 'red');
            }
        }
    });
    // Notification
    $('.notification').live('click', function() {
        $(this).slideUp(1000);
    });

    if ($('ul.tabs').length > 0) {
        $("ul.tabs").tabs("div.panes > div.pane", {history: true, onBeforeClick: function(e, index){
                $('.team-data').hide();
                $('.team-data').eq(index).show();
        }});
    }
    // Infinite replication
    if ($('#row-template-add').length > 0) {
        $('#row-template-add').click(function() {
            $('#row-template').clone(true).removeAttr('id').val('').append('<a class="row-template-rmv">remove</a>').appendTo($('#row-template-wrapper'));
            return false;
        });
        $('.row-template-rmv').live('click', function() {
            $(this).parent().remove();
            return false;
        });
    }
    if ($('#slidetext-1').length > 0) {
        $('#slidetext-1').flashembed({src: "assets/swf/text-1.swf", wmode:'transparent'});
    }

    if ($('#leftlogos').length > 0 && $('#rightlogos').length > 0) {
        setInterval("fadeInOutLogos()", 3000);
    }
    if ($('#slideshow').length > 0) {
        setInterval("fadeInOut('slideshow')", 5000);
    }
    if ($('#map_canvas').length > 0) {
        showMap("122 W John Carpenter Freeway Suite 525 Irving , TX 75039");
    }
});

function notification(text, status) {
    $('body .notification').remove();
    $('body').prepend('<div class="notification ' + status + '">' + text + '</div>');
}

function fadeInOut(selector) {
    var $active = $('#' + selector + ' img.active');
    if ( $active.length == 0 ) $active = $('#' + selector + ' img:last');
    var $next =  $active.next().length ? $active.next() : $('#' + selector + ' img:first');
    $active.addClass('last-active');
    $next.css({
        opacity: 0.0
    })
    .addClass('active')
    .animate({
        opacity: 1.0
    }, 1000, function() {
        $active.removeClass('active last-active');
    });
}

function fadeInOutLogos() {
	if (logos_smf % 2 == 0) {
		selector = "leftlogos";
	} else {
		selector = "rightlogos";
	}
    var $active = $('#' + selector + ' img.active');
    if ( $active.length == 0 ) $active = $('#' + selector + ' img:last');
    var $next =  $active.next().length ? $active.next() : $('#' + selector + ' img:first');
    $active.addClass('last-active');
    $next.css({
        opacity: 0.0
    })
    .addClass('active')
    .animate({
        opacity: 1.0
    }, 2000, function() {
        $active.removeClass('active last-active');
    });
    logos_smf++;
}
var geocoder;

function initializeGoogleMaps() {
    if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map_canvas"))
        map.setCenter(new GLatLng(38.20365531807151, -92.900390625), 4);
        map.addControl(new GSmallMapControl());
        map.addControl(new GMapTypeControl());
        geocoder = new GClientGeocoder();
    }
}

function showAddressGoogleMaps(address) {
    if (geocoder) {
        geocoder.getLatLng(
            address,
            function(point) {
                if (!point) {
                } else {
                    var marker = new GMarker(point);
                    map.addOverlay(marker);
                }
            }
        );
    }
}

function getGoogleCenter(address, zoom) {
    if (geocoder) {
        geocoder.getLatLng(
            address,
            function(point) {
                if (!point) {
                } else {
                    map.setCenter(point, zoom);
                }
            }
        );
    }
}

function showMap(address) {
	$(document).ready(function() {
		initializeGoogleMaps();
		showAddressGoogleMaps(address);
		getGoogleCenter(address, 11);
	});
}
