/*
Plugin Name: DatingGold WebCamGirls
Plugin URI: http://ads.webcamclub.com/wp-widget/
Description: Displays random hot models waiting for you now! ...
Author: <a href="http://www.datinggold.com/">Datinggold.com</a>
Version: 0.1
License: GPL v2 - http://www.gnu.org/licenses/old-licenses/gpl-2.0.html

Copyright © 2009 DatingGold.com (support AT datinggold DOT com)
http://datinggold.com/info.php?show=contact

Requires WordPress 2.1 or later. Not for use with WPMU.
*/

/**
 *   Class to interact with the WordPress Widget
 *
 *   @package DG_WEBCAMGIRLS
 *
 */
var oDgWebCamGirls = {

    svc_domain : null,
    request_url : null,
    active : false,
    models : null,
    ainfo : null,
    atcc : null,
    nats : null,
    limit : null,
    photo_type : null,

    /**
     *   Installs and intializes the class variables
     *
     *   @param string svc_domain: The domain name of the site servicing the widget
     *   @param string request_url: The url where the ajax call will be sent
     *   @param string ainfo: The affiliate information
     *   @param string atcc: The optional atcc or campaign number
     *   @param string nats: The NATS id
     *   @param string limit: The limit; number of models that will be displayed
     *   @param string photo_type: will either be "photo" or "animated"
     *   @return void
     *
     */
    install : function( svc_domain, request_url, ainfo, atcc, nats, limit, photo_type )
    {
        this.active = true;
        this.svc_domain = svc_domain;
        this.request_url = request_url + "/dg_webcamgirls.php";
        this.ainfo = ainfo;
        this.atcc = atcc;
        this.nats = nats;
        this.limit = limit;
        this.photo_type = photo_type;

        if ( this.photo_type == "animated" )
        {
            this.addEvent( window, "load", oDgWebCamGirls.displayModels );
        }
    },

    /**
     *   Adds an event handler to the DOM ...
     *
     *   @param object elem: The DOM element  which is getting the the event handler
     *   @param string event: the name of the event to schedule
     *   @param string func: the function to execute when the event is fired
     *   @return bool
     *
     */
    addEvent : function( elem, event, func )
    {
		if (elem.addEventListener)
        {
			return elem.addEventListener(event, func, false)
		}
        else if (elem.attachEvent)
        {
			return elem.attachEvent('on'+event, func)
		}
        else
        {
			return false
		}
	},

    /**
     *   Displays the models by iterating through the .models array
     *
     *   @param void :
     *   @return void
     *
     */
    displayModels : function()
    {
        for ( var id = 0; id < oDgWebCamGirls.models.length; id++ )
        {
            model = oDgWebCamGirls.models[ id ];

            if ( oDgWebCamGirls.photo_type == "animated" )
            {
                oDgWebCamGirls.loadVideo( model, id );
            }
            else
            {
                oDgWebCamGirls.loadPhoto( model, id );
            }
        }
    },

    /**
     *   Loads a video using swfobject
     *
     *   @param array model: An array of the model data
     *   @param int id: pointer to the .models array; used to identify which element to update
     *   @return void
     *
     */
    loadVideo : function( model, id )
    {
        if ( model.video_sid.length != 0 && document.getElementById( "dg_model_cell_"+id ) )
        {
            try
            {
                if ( model.freechat_url.length == 0 )
                {
                    var linkPath = model.profile_url;
                }
                else
                {
                    var linkPath = model.freechat_url;
                }

                var flashvars = {};
                flashvars.modelID = model.model_id;
                flashvars.modelName = model.model_name;
                flashvars.sid = model.video_sid;
                flashvars.imgPath = model.small_image;
                flashvars.publicMntDomain = model.publicMntDomain;
                flashvars.linkPath = linkPath;

                var params = {};
                params.menu = "false";
                params.wmode = "transparent";
                params.width = "104";
                params.height = "81";
                params.autoplay = "true";
                params.scale = "noborder";
                params.play = "true";
                params.allowScriptAccess = "always";
                params.allowFullScreen = "false";
                params.quality = "high";
                params.swfLiveConnect = "true";
                params.movie = "http://"+this.svc_domain+"/im/images/im_thumb.swf?modelID="+model.model_id+"&modelName="+model.model_name+"&sid="+model.video_sid+"&imgPath="+model.small_image+"&linkPath="+linkPath;
                params.src = "http://"+this.svc_domain+"/im/images/im_thumb.swf?modelID="+model.model_id+"&modelName="+model.model_name+"&sid="+model.video_sid+"&imgPath="+model.small_image+"&linkPath="+linkPath;

                var attributes = {};
                attributes.id = "dg_model_cell_" + id;
                attributes.name = "dg_model_cell_" + id;

                var swf_file = "http://"+this.svc_domain+"/im/images/im_thumb.swf?modelID="+model.model_id+"&modelName="+model.model_name+"&sid="+model.video_sid+"&imgPath="+model.small_image+"&linkPath="+linkPath;
                var expressInstall = "http://"+this.svc_domain+"/im/images/expressInstall.swf?modelID="+model.model_id+"&modelName="+model.model_name+"&sid="+model.video_sid+"&imgPath="+model.small_image+"&linkPath="+linkPath;
                swfobject.embedSWF( swf_file, "dg_model_cell_" + id, "104", "81", "9.0.0", expressInstall, flashvars, params, attributes);

                oModelName = document.getElementById( "dg_model_name_" + id );

                if ( oModelName )
                {
                    oModelName.href = linkPath;
                    oModelName.innerHTML = model.model_name;
                }
            }
            catch(e)
            {
                oDgWebCamGirls.loadPhoto( model, id );
            }
        }
        else
        {
            oDgWebCamGirls.loadPhoto( model, id );
        }
    },

    /**
     *   Loads a models photo and her model name
     *
     *   @param array model: An array of the model data
     *   @param int id: pointer to the .models array; used to identify which element to update
     *   @return void
     *
     */
    loadPhoto : function( model, id )
    {
        if ( model.small_image.length > 0 && document.getElementById( "dg_model_photo_"+id ) )
        {
            try
            {
                oPhoto = document.getElementById( "dg_model_photo_" + id );
                oPhoto.src = model.small_image;

                oModelName = document.getElementById( "dg_model_name_" + id );

                if ( model.freechat_url.length == 0 )
                {
                    oModelName.href = model.profile_url;
                }
                else
                {
                    oModelName.href = model.freechat_url;
                }

                oModelName.innerHTML = model.model_name;
            }
            catch(e)
            {}
        }
    },

    /**
     *   Used in the niche/category dropdown onchange event to request a new set of models based on the selected niche/category
     *
     *   @param object o: The object that sent it here ... in this case an <select></select> element
     *   @return void
     *
     */
    requestUpdate : function( o )
    {
        var dgRequest = new DgServiceRequest( oDgWebCamGirls.request_url, oDgWebCamGirls.receiveModels );
        var params = "dg_niche_request=1";
        params += "&atcc="+oDgWebCamGirls.atcc;
        params += "&nats="+oDgWebCamGirls.nats;
        params += "&limit="+oDgWebCamGirls.limit;
        params += "&niche_id="+o.selectedIndex;
        params += "&niche_name="+o.options[ o.selectedIndex ].text;
        params += "&ainfo="+oDgWebCamGirls.ainfo;

        dgRequest.update( params, "GET" );
    },

    /**
     *   Callback function for the ajax call
     *
     *   @param string data: Contains the return data from the server
     *   @param int status: Contains the return status from the server
     *   @return void
     *
     */
    receiveModels : function( data, status )
    {
        if ( status == 200 && data != "" )
        {
            try
            {
                oDgWebCamGirls.models = new Array();
                oDgWebCamGirls.models = eval( data );
                oDgWebCamGirls.displayModels();
            }
            catch(e)
            {}
        }
    }

}

/*
 * AJax Object found at http://www.hunlock.com/blogs/The_Ultimate_Ajax_Object
 *
 * Creating a new AJax Object:
 * var myRequest = new ajaxObject('http://www.somedomain.com/process.php');
 *
 *
 * Creating a call back funciton
 * var myRequest = new ajaxObject('http://www.somedomain.com/process.php', processData);
 *
 * function processData(responseText, responseStatus)
 * {
 *   if (responseStatus==200)
 *   {
 *     alert(responseText);
 *   }
 *   else
 *   {
 *     alert(responseStatus + ' -- Error Processing Request);
 *   }
 * }
 *
 */

function DgServiceRequest( url, callbackFunction, contentType )
{
	var that = this;
	var urlCall = url;
	this.updating = false;
	this.contentType = ( contentType == null ) ? "application/x-www-form-urlencoded" : contentType;

	this.callback = callbackFunction || function(){};

	this.abort = function()
	{
		if ( that.updating )
		{
			that.updating = false;
			that.AJAX.abort();
			that.AJAX = null;
		}
	}

	this.update = function( passData, postMethod )
	{
		if ( that.updating )
		{
			return false;
		}

		that.AJAX = null;

		if ( window.XMLHttpRequest )
		{
			that.AJAX = new XMLHttpRequest();
		}
		else
		{
			that.AJAX = new ActiveXObject( "Microsoft.XMLHTTP" );
		}

		if ( that.AJAX == null )
		{
			return false;
		}
		else
		{
			that.AJAX.onreadystatechange = function()
			{
				if ( that.AJAX.readyState == 4 )
				{
					that.updating = false;
					that.callback( that.AJAX.responseText, that.AJAX.status );
					that.AJAX = null;
				}
			}

			this.updating = new Date();

			if ( /post/i.test( postMethod ) )
			{
				try
				{
					var uri = urlCall + '?' + that.updating.getTime();
					that.AJAX.open( "POST", urlCall, true );
					that.AJAX.setRequestHeader( "Content-type", this.contentType ); // "application/x-www-form-urlencoded");
					that.AJAX.setRequestHeader( "Content-Length", passData.length );
					that.AJAX.send( passData );
				}
				catch (e)
				{
                    alert( e.description );
				}
			}
			else
			{
				try
				{
					var uri = urlCall + '?' + passData + '&timestamp=' + ( that.updating.getTime() );
					that.AJAX.open( "GET", uri, true );
					that.AJAX.send( null );
				}
				catch (e)
				{
                    alert( e.description );
				}
			}

			return true;
		}
	}
}