var spottago = {};
spottago.event = {};
spottago.DOM = {};
spottago.window = {};

spottago.moreOptsToggle = function() 
{
    $( '#moreopts_toggle' ).click(
        function( e ){
            var container = $( '#moreopts_toggle' );
            var text = $( '#moreopts_text' );
            var adv_opts = $( '#more_opts_wrap' );
            if( text.html() == "More Options" )
            {
                container.attr( 'class', 'moreopts_minus' );
                text.html( 'Less Options' );
                adv_opts.animate( { height: 75 }, 600 );
                
            }
            else
            {
                container.attr( 'class', 'moreopts_plus' );
                text.html( 'More Options' );
                adv_opts.animate( { height: 0 }, 400 );
            }
        } 
    );


}

/* Utilities for events */

spottago.event.kill = function( e )
{
    if( e.preventDefault )
    {
        e.preventDefault();
        e.stopPropagation();
    }
    else
    {
        e.returnValue = false;
        e.cancelBubble = true;
    }
}

/* Utilities for working with window/document size */

spottago.window.getScreenDimensions = function() {
	var width = 0;
	var height = 0;
    if( typeof( window.innerWidth ) == 'number' ) {
      //Non-IE
      width = window.innerWidth;
      height = window.innerHeight;
    } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
      //IE 6+
      width = document.documentElement.clientWidth;
      height = document.documentElement.clientHeight;
    }
	
    return {width: width, height: height};
}

spottago.window.getDocumentDimensions = function() {
	var width = 0;
	var height = 0;
    if( document.documentElement.scrollWidth ) {
      //Non-IE
      width = document.documentElement.scrollWidth;
      height = document.documentElement.scrollHeight;
    } else if( document.body.scrollWidth ) {
      //IE 6+
      width = document.body.scrollWidth;
      height = document.body.scrollWidth;
    }
	
    return {width: width, height: height};
}

spottago.window.getCenterPositionBySize = function( size )
{
    var screen = spottago.window.getScreenDimensions();
    var left = (screen.width > size.width) ? (screen.width-size.width)/2 : 0;
    var top = (screen.height > size.height) ? (screen.height-size.height)/2 : 0;
    return {horizontal: left, vertical: top};
}


spottago.DOM.utils = new Object();
spottago.DOM.utils.var_dump = function( variable, var_name )
{
    var text = var_name + "\n";
    for( attr in variable )
    {
        text += attr + " : " + variable[attr] + "\n";
    }
    alert( text );
}


spottago.utils = {};
spottago.utils.getDistanceByGeocoords = function( loc1, loc2 )
{
    var lng1_in_rad = loc1.lng * ( Math.PI / 180 );
    var lat1_in_rad = loc1.lat * ( Math.PI / 180 );
    var lng2_in_rad = loc2.lng * ( Math.PI / 180 );
    var lat2_in_rad = loc2.lat * ( Math.PI / 180 );
    //Get differences
    var long_diff = lng2_in_rad - lng1_in_rad;
    var lat_diff = lat2_in_rad - lat1_in_rad;

    //Compute distance based on the Haversine Formula
    a = Math.pow( Math.sin( lat_diff / 2 ), 2 ) + Math.cos( lat1_in_rad ) * Math.cos( lat2_in_rad ) * Math.pow( Math.sin( long_diff / 2 ), 2 );
    c = 2 * Math.atan2( Math.sqrt( a ), Math.sqrt( 1 - a ) );
    return c * 3956; //Earth's radius in miles
}

/*
* Processes JSON text and returns the created object
*/
spottago.utils.processJSON = function( text )
{
    if( text && text != "" )
    {
        //Test for JSON syntax (malicious javascript, for example)
        return !( /[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/.test( text.replace( /"(\\.|[^"\\])*"/g, '' ) ) ) && eval( '(' + text + ')' );
    }
    else
    {
        return false;
    }
}

spottago.utils.auth = {};
spottago.utils.auth.onFacebookLogin = function()
{
    window.location = "/facebook/login.php";
}

/* Extensions to native Javascript objects */

String.prototype.truncate = function( limit, suffix )
{
    var length = this.length;
    if( length < limit ) return this;

    var slice_index = ( suffix ) ? limit - suffix.length : limit;
    var new_string = this.substring( 0, slice_index );
    if( suffix ) new_string += suffix;

    return new_string;
}



