// AUTHOR : Peter Czachorowski
// DATE : 11 Mar 2009

// DESCRIPTION
// -----------
// Code to set a cookie for a product
// Read a cookie to check if vote is already cast if so then set to active
// Send vote information to DB Table and increment vote count

// FUNCTIONS 
// ---------
// checkIfVoted(ProductID)
// vote(productID)
// createCookie(name, value, days)
// readCookie(name)

// PROCESS
// -------
// onload check for cookie
// if exists for product set ACTIVE
// on vote submit ajax call to DB to add vote. increment vote. displays thanks msg
// TODO : create view in DB counting all votes 
// TODO : create ajax page
// TODO : get images




    var msgThanks = 'Already voted!';
    var msgIntro = '';
    var cookieProductDealID =''; //this var is set on page load by the checkIfVoted function
	
	
	function FadeEffect(element){
       new Effect.Fade(element, 
       { duration:1});
   }
   function ShowEffect(element){
       new Effect.Appear(element, 
       {duration:1, from:1.0, to:1.0});
   }
	
    function vote(varVoteProductID,varVoteDealID,varVoteType,voteTotal) {
		cookieProductDealID = readCookie('productID_'+varVoteProductID+'_dealID_'+varVoteDealID);
		if(cookieProductDealID!=varVoteProductID+'_'+varVoteDealID)
		{
		
				
		new Effect.Fade('voteTotalID', {afterFinish: function (obj) { 
		
			new Effect.Appear('voteTotalID'); 
		
			var url = '/tnsw/utils/voteCommon.aspx';
			var pars = 'action=addvote&productID='+ varVoteProductID+'&dealID='+varVoteDealID+'&voteType='+varVoteType+'';
			
			var myAjax = new Ajax.Request(
				url, 
				{
					method: 'get', 
					parameters: pars, 
					onFailure: reportError,
					onSuccess: processVote(varVoteProductID,varVoteDealID,varVoteType,voteTotal)			
				});
				
			}})
			
		}
		else
		{
			alert('Already voted')
		}
    }
        
    function processVote(varVoteProductID,varVoteDealID,varVoteType,voteTotal)
    {
		createCookie('productID_' + varVoteProductID+'_dealID_'+varVoteDealID, varVoteProductID+'_'+varVoteDealID, 365);
		voteTotal=parseFloat(voteTotal)+1;
		
		
		
		if(voteTotal==1)
		{
			$('voteTotalID').innerHTML = '1 person liked this deal';
			$('voteImgID').innerHTML='<img src=/tnsw/templates/images/custom/'+varVoteType+'_on.gif width=20 height=20  />'
		}
		else
		{
			$('voteTotalID').innerHTML = voteTotal+' people liked this deal';
		}
    }
        
    function reportError(request)
	{
		alert('Sorry. There was an error.');
	}
    
    function checkIfVoted(varProductID,varDealID,varVoteTyp) {
        cookieProductDealID = readCookie('productID_'+varProductID+'_dealID_'+varDealID);
    }

    function createCookie(name, value, days) {
        if (days) {
            var date = new Date();
            date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
            var expires = "; expires=" + date.toGMTString();
        }
        else var expires = "";
        document.cookie = name + "=" + value + expires + "; path=/";
    }

    function readCookie(name) {
        var nameEQ = name + "=";
        var ca = document.cookie.split(';');
        for (var i = 0; i < ca.length; i++) {
            var c = ca[i];
            while (c.charAt(0) == ' ') c = c.substring(1, c.length);
            if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
        }
        return null;
        }

