/*
 * Author: Karl O'Leary, Emagine Media
 * Updated:
 * - 2009-01-12, Karl, created
 */



/*
 * Script variables.
 * - regualar variables
 */
var version = "0.1";
var description = "Brucebetting Poker JavaScript";
var this_url = "";

    /*
     * On load code.
     */
    $(document).ready(function() {
        this_url = jQuery.url.attr("path");
        equalHeight($(".col"));

        $("#entryform").submit(function() {
            var type = $("#form").attr("value");            

            if (type=="player") {
                var ip_name = $("input[name='field_id_8']").attr("value");
                var ip_mobile = $("input[name='field_id_9']").attr("value");
                var ip_email = $("input[name='field_id_10']").attr("value");

                if (ip_name!="") {
                    $("#title").attr("value", $("#title").attr("value")+ip_name);                    
                    return true;
                }
                else {
                    alert("Please fill in your name to complete the form.");
                    return false;
                }
            }
            else if (type=="tournament") {
                var ip_total = $("#field_id_21").attr("value");
                var ip_date = $("#field_id_22").attr("value");

                if (ip_total!="" && ip_date!="") {
                    $("#title").attr("value", $("#title").attr("value")+" "+ip_date);
                    return true;
                }
                else {
                    alert("Please fill in all fields and\nenter the tournament date in the format YYYY-MM-DD HH:MM.");
                    return false;
                }
            }
        });
    });


    /*
     * On close code.
     */
    $(window).unload(function () {
        //GUnload();
    });



    function debug(str) {
        try {
            console.log(new Date()+": "+str);
        }
        catch(err) {}
    }

    // Useability FUNCTIONS =============================================================================================================================================================

    function equalHeight(group) {
        tallest = 0;
        group.each(function() {
            thisHeight = $(this).height();
            if(thisHeight > tallest) {
                tallest = thisHeight;
            }
        });
        group.height(tallest);
    }




    // Validation FUNCTIONS =============================================================================================================================================================


    function validateEmail(eee) {
        var email  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
        return email.test(eee);
    }

    function validateZipcode(ccc, sss) {
        var us_zip  = /(^\d{5}$)|(^\d{5}-\d{4}$)/;
        var ca_zip = /(^[A-Z][0-9][A-Z][0-9][A-Z][0-9]$)|(^[A-Z][0-9][A-Z].[0-9][A-Z][0-9]$)/;
        if (ccc=="US")
          return us_zip.test(sss);
        else if (ccc=="CA")
          return ca_zip.test(sss);
        else
          return false;
    }



// Google Maps FUNCTIONS =============================================================================================================================================================

function googleMapsLoad() {
    if (GBrowserIsCompatible()) {
    var map = new GMap2(document.getElementById("map"));
        map.addControl(new GLargeMapControl());
        map.addControl(new GMapTypeControl());
        map.setCenter(new GLatLng(53.409532, -7.844238), 6);

        GDownloadUrl("/shop_locations.xml", function(data) {
            var xml = GXml.parse(data);
            var markers = xml.documentElement.getElementsByTagName("marker");
            for (var i = 0; i < markers.length; i++) {
                var name = markers[i].getAttribute("name");
                var address = markers[i].getAttribute("address");
                var type = markers[i].getAttribute("type");
                var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")), parseFloat(markers[i].getAttribute("lng")));
                var marker = googleMapsCreateMarker(point, name, address, type);
                map.addOverlay(marker);
            }
        });
    }
}

function googleMapsCreateMarker(point, name, address, type) {
    var gIconBlue = new GIcon();
    gIconBlue.image = 'http://www.brucebetting.com/graphics/mm_20_red.png';
    gIconBlue.shadow = 'http://labs.google.com/ridefinder/images/mm_20_shadow.png';
    gIconBlue.iconSize = new GSize(12, 20);
    gIconBlue.shadowSize = new GSize(22, 20);
    gIconBlue.iconAnchor = new GPoint(6, 20);
    gIconBlue.infoWindowAnchor = new GPoint(5, 1);

    var gIconRed = new GIcon();
    gIconRed.image = 'http://www.brucebetting.com/graphics/mm_20_red.png';
    gIconRed.shadow = 'http://labs.google.com/ridefinder/images/mm_20_shadow.png';
    gIconRed.iconSize = new GSize(12, 20);
    gIconRed.shadowSize = new GSize(22, 20);
    gIconRed.iconAnchor = new GPoint(6, 20);
    gIconRed.infoWindowAnchor = new GPoint(5, 1);

    var gCustomIcons = [];
    gCustomIcons["shop x"] = gIconBlue;
    gCustomIcons["shop"] = gIconRed;

    var marker = new GMarker(point, gCustomIcons[type]);
    var html = "<b>" + name + "</b> <br/>" + address;
    GEvent.addListener(marker, 'click', function() {
        marker.openInfoWindowHtml(html);
    });
    return marker;
}
