/**
* 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 ($('.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;
        }
    });

    validateForm = function (e) {
        var inputs = $(e).find(".required"),
        errors = inputs.filter(function() {
            return $(this).val().replace(/\s*/g, '') == '';
        });
        $(e).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;
        }
        return true;
    }

    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 ($('div.scrollbars').length > 0) {
        $('div.scrollbars').jScrollPane({
            showArrows:true,
            scrollbarWidth:15,
            scrollbarMargin:20,
            dragMinHeight:15,
            dragMaxHeight:15,
            topCapHeight: 30,
            bottomCapHeight: 30,
            reinitialiseOnImageLoad: true
        });
    }

    $('a.overlay-trigger').overlay({
        target: '.overlay',
        fixed: false,
        top: 100,
        mask: {
            color: '#000',
            loadSpeed: 200,
            opacity: 0.5
        },
        onBeforeLoad: function() {
            var wrap = this.getOverlay().find(".wrap");
            var url = this.getTrigger().attr("href");
            wrap.load(url + ' #text');
        },
        onLoad: function() {
            var wrap = this.getOverlay().find(".wrap");
            silentSubmit(wrap);
        },
        onBeforeClose: function() {
            this.getOverlay().find(".wrap").empty();
        }
    });
});

function silentSubmit(wrap) {
    wrap.find('form').submit(function() {
        $.post($(this).attr('action'), $(this).serialize(), function (data) {
            wrap.empty();
            wrap.append($(data).find('#text'));
            silentSubmit(wrap);
        })
        return false;
    });
}

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);
	});
}
