/**
* Default Website Scripts
 * @name default.js
 * @package cnc
 * @version 1.0
 * @since 2010-01-01
 * @author Cristian Ciobanu <cristian@pallasweb.com>
 */

// Raise a popup
function openWindow(url,width,height) {day = new Date();id = day.getTime();window.open(url, id, "toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width="+width+",height="+height+",left="+((screen.width - width)/2)+",top="+((screen.height - height)/2));return false;}
// Open links in a new window
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";}}
// Turns a tag into a link
function href(url) {window.location = url;return false;}

$(document).ready(function() {
    browserDetection();
    fixIE6alphaTransparency();
    externalLinks();

    if ($('.home-left').length > 0) {
        $('.home-left').flashembed({src: "assets/flash/home-left.swf", wmode:'transparent'});
    }

    if ($('.home-right').length > 0) {
        $('.home-right').flashembed({src: "assets/flash/home-right.swf", wmode:'transparent'});
    }

    if ($('.date').length > 0) {
        $(".date").dateinput({format: 'dddd, mmmm dd, yyyy', trigger: true, min: -1});
    }

    if ($('.date-range').length > 0) {
        $(".date-range").dateinput({format: 'dddd, mmmm dd, yyyy', trigger: true, min: -1});
        $(".date-range").bind("onShow onHide", function()  {
            $(this).parent().toggleClass("active");
        });
        $(".date-range:first").data("dateinput").change(function() {
            $(".date-range:last").data("dateinput").setMin(this.getValue(), true);
        });
    }

    $('form').submit(function() {
        var inputs = $(this).find(".required"),
        errors = inputs.filter(function() {
            return $(this).val().replace(/\s*/g, '') == '';
        });
        $(this).find("span.error").remove();
        if (errors.length) {
            $.each(errors, function(i, v) {
                $(v).after('<span class="error">Please enter ' + $(v).attr('title') + '</span>');
            });
            return false;
        }
    });

    if ($('div.error, div.notice, div.success').length > 0) {
        $.each($('div.error, div.notice, div.success'), function(i, e) {
            notification = $(this);
            notification.hide();
            notification.prepend('<span class="notification-close">X</span>');
            notification.slideDown(function()  {
                notification.css({
                    "opacity":0.9
                });
                nTimer = setInterval(function() {
                    notification.css({
                        "opacity":0.6
                    });
                    setTimeout(function() {
                        notification.css({
                            "opacity":0.9
                        });
                    }, 500);
                }, 1000);
            });
            notification.find('span.notification-close').click(function() {
                notification.slideUp();
                clearInterval(nTimer);
            });
        })
    }

    if ($('ul.faq').length>0) {
        $('ul.faq div').hide();
        $('ul.faq a.title').click(function() {
            if($(this).siblings('div').eq(0).css('display') == 'none'){
                $(this).find('span').html('-');
            } else {
                $(this).find('span').html('+');
            }
            $(this).siblings('div').toggle();
        });
    }

    $('a.overlay-trigger').overlay({
        target: '.overlay',
        top: 100,
        mask: {
            color: '#000',
            loadSpeed: 200,
            opacity: 0.5
        },
        onLoad: function() {
            showMyMap("8108 Mesa Dr., Suite B100, Austin, TX 78759")
        },
        onClose: function() {
            GUnload();
        }

     });
});

function initializeGoogleMaps() {
    if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map-canvas"))
        map.setCenter(new GLatLng(38.20365531807151, -92.900390625), 12);
        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 blueIcon = new GIcon(G_DEFAULT_ICON);
                    markerOptions = {
                        icon:blueIcon
                    };

                    var marker = new GMarker(point, markerOptions);
                    map.addOverlay(marker);
                }
            }
        );
    }
}

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

function showMyMap(address) {
	$(document).ready(function() {
		initializeGoogleMaps();
		showAddressGoogleMaps(address);
		getGoogleCenter(address, 15);
	});
}
