/**************************************************

Filename:		prop.js
Date Modified:	August 31, 2011
Modified By:	Josh Giles

Dependencies:	http.js

Description:	javascript file for dealing with
				props

**************************************************/

// Create namespace and specify some meta-information
Prop = {};
Prop.NAME = "Prop";
Prop.VERSION = 1.0;

/*************************************************

Constants

**************************************************/

Prop.url = './ajax/propAjax.php';

/*************************************************

Prop up an idea

**************************************************/

Prop.propUp = function(ideaID)
{
    Prop.prop(ideaID, true);
}

/*************************************************

Prop down an idea

**************************************************/

Prop.propDown = function(ideaID)
{
    Prop.prop(ideaID, false);
}

/*****************************************************

Process all props

******************************************************/

Prop.prop = function(ideaID, isUp)
{
    var propsElement = $('#prop' + ideaID);
    // What action to POST
    var action = '';
 /*    // Find which way, if any, user has propped this idea before
    var clickedElement = propsElement.find('.clicked');
    // No prior prop, need add one, add 'clicked' class appropriately
    if (!clickedElement.length)
    { */
        action = isUp ? 'propUp' : 'propDown';
  /*       var setClass = function() 
        {
            var arrowElement = propsElement.find(isUp ? '.upArrow' : '.downArrow');
            arrowElement.addClass('clicked');
			if (isUp)
				arrowElement.addClass('upArrowClicked'); // for formatting
			else
				arrowElement.addClass('downArrowClicked');
	
        }
    }
    // Prior prop, need to remove it
    else
    {
        // Can't double-prop something
        var arrow = clickedElement.attr('class').split(' ')[0];
        if ((isUp && arrow == 'upArrow') || (!isUp && arrow == 'downArrow'))
            return;

        action = isUp ? 'removePropDown' : 'removePropUp';
		isUp ? clickedElement.removeClass('downArrowClicked') : clickedElement.removeClass('upArrowClicked');
        // Remove '.clicked' class as appropriate
        var setClass = function() 
        {
            clickedElement.removeClass('clicked');
        }
    } */
            
    var callback = function(x)
    {
      /*   $worked = parseInt(x);
        if ($worked)
        {
            var valElement = propsElement.find('.propValue');
            var props = parseInt(valElement.html());
            valElement.html(isUp ? props + 1 : props -1);
            setClass();
        }
		else
		{
			console.log(x);
		} */
		
		//console.log(x);	// test
		
		$obj = JSON.parse(x); 
        $worked = parseInt($obj[0]);
         $isUp = parseInt($obj[1]);  
            var valElement = propsElement.find('.propValue');
            var props = parseInt(valElement.html());
            valElement.html($worked);
            
			var arrowUp = propsElement.find( '.upArrow');
			var arrowDown = propsElement.find( '.downArrow'); 
            arrowUp.removeClass('upArrowClicked').removeClass('clicked');
            arrowDown.removeClass('downArrowClicked').removeClass('clicked'); 
			if ($isUp == 1) { arrowUp.addClass('upArrowClicked').addClass('clicked'); } // for formatting
			else if ($isUp ==  -1 ){ arrowDown.addClass('downArrowClicked').addClass('clicked');}
			else { } // for formatting
			
    }
    
    var options = {};
    options.parameters = {};
    options.parameters.action = action;
    options.parameters.ideaID = ideaID;
    HTTP.post(Prop.url, callback, options);
}
    
    
