/**
* 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;}

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

    if (jQuery('.motto').length > 0) {
        jQuery('.motto').flashembed({src: "http://www.austinelderlycare.com/assets/flash/text.swf", wmode:'transparent'});
    }

    if (jQuery('span.careathome-large').length > 0) {
        jQuery('span.careathome-large').flashembed({src: "http://www.austinelderlycare.com/assets/flash/ad.swf", wmode:'opaque'});
    }

    if (jQuery('span.careathome-small').length > 0) {
        jQuery('span.careathome-small').flashembed({src: "http://www.austinelderlycare.com/assets/flash/ad-small.swf", wmode:'opaque'});
    }

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

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

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

    if (jQuery('div.error, div.notice, div.success').length > 0) {
        jQuery.each(jQuery('div.error, div.notice, div.success'), function(i, e) {
            notification = jQuery(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);
            });
        })
    }
    jQuery('input[name="option[]"]').change(function() {
        if (jQuery(this).attr('checked') == true) {
            customProgramAdd(jQuery(this).parent().siblings('li.blue').eq(0).html(), jQuery(this).val());
        } else {
            customProgramRmv(jQuery(this).parent().siblings('li.blue').eq(0).html(), jQuery(this).val());
        }
    });
});

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) {
	jQuery(document).ready(function() {
		initializeGoogleMaps();
		showAddressGoogleMaps(address);
		getGoogleCenter(address, 15);
	});
}

function customProgramAdd(p, v) {
    jQuery('.t2 .indent').append('<span class="block arrow1">' + p + ': ' + v + '</span>');
    jQuery('#options').val(jQuery('#options').val() +  p + ': ' + v + '<br />');
    jQuery('.t2 div.form').show();
}
function customProgramRmv(p, v) {
    jQuery('#options').val('');
    jQuery.each(jQuery('.t2 .indent span.block'), function (i, e) {
        if (jQuery(e).html() == (p + ': ' + v)) {
            jQuery(e).remove();
        } else {
            jQuery('#options').val(jQuery('#options').val() +  p + ': ' + v + '<br />');
        }
    });
    if (jQuery('.t2 .indent span.block').length == 0) {
        jQuery('.t2 div.form').hide();
    }
}
