/**
 *
 * @param {Object} local_conf
 */
jQuery.fn.fan = function (local_conf) {
	//
    var conf = {};

    conf.states = {yes: true, no: true};
    conf.defaultState = null;

    conf.ajaxCheckUrl = null;
    conf.ajaxTokenUrl = null;
    conf.ajaxAddUrl = null;

    conf.cookieEnabled = false;
    conf.cookiePrefix = 'fan';
    conf.cookieDays = 7;
    conf.cookieDomain = '';
    conf.cookiePath = '/';
    
    conf.linkSelector = '#iWatch a';

    conf.stateChangeBefore = function (previousState, state) {};
    conf.stateChangeAfter = function (previousState, state) {};

    conf.voteSubmitBefore = function () {};
    conf.voteSubmitAfter = function () {};
    
	//
    var thisJQ = jQuery(this);
	
    var currentState = conf.defaultState;

	/**
	 * 
	 * @param {Object} local_conf
	 */
    var init = function (local_conf) {
        conf = jQuery.extend(conf, local_conf);
        
        linkVoteJQ = thisJQ.find(conf.linkSelector);

        linkVoteJQ.click(function () {
			voteSubmit();
			return false;
		});
        
        stateCheck();
    }

	/**
	 * 
	 */
    var stateCheck = function () {
        //
		if(conf.cookieEnabled) {
	        var cookieState = jQuery.cookie(conf.cookiePrefix + '_state');
	        
	        for(state in conf.states) {
	            if(cookieState === state) {
	                stateChange(cookieState);
	                return;
	            }
	        }
		}

        //
        jQuery.ajax({
            type: 'GET',
            url: conf.ajaxCheckUrl,
            dataType: 'json',
            success: function (data, textStatus) {
                if(data.response === true) {
                    stateChange('yes')
                }
				else {
                    stateChange('no')
				}
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
            }});
    }

    /**
     * 
     * @param {String} state
     */
    var stateChange = function (state) {
		//
		var previousState = currentState;

        //
        conf.stateChangeBefore(previousState, state);

        //
        currentState = state;

		//
		if(conf.cookieEnabled) {
	        jQuery.cookie(conf.cookiePrefix + '_state', state, {expires: conf.cookieDays, domain: conf.cookieDomain, path: conf.cookiePath});
		}

        //
        conf.stateChangeAfter(previousState, state);
    }
    
	/**
	 * 
	 */
	var voteSubmit = function () {
		if(linkVoteJQ.queue().length > 0) {
			return;
		}

		//
		conf.voteSubmitBefore();
		
		//
		linkVoteJQ.queue(function () {
		    jQuery.ajax({
	            type: 'GET',
	            url: conf.ajaxTokenUrl,
	            dataType: 'json',
	            success: function (data, textStatus) {
					var token = data.response;
					
			        jQuery.ajax({
			            type: 'POST',
			            url: conf.ajaxAddUrl,
			            data: {token: token},
			            dataType: 'json',
			            success: function (data, textStatus)
			            {
							stateChange(data.response == 1 ? 'yes' : 'no');
							linkVoteJQ.dequeue();

							conf.voteSubmitAfter();
			            },
			            error: function (XMLHttpRequest, textStatus, errorThrown) {
							stateChange('no');
							linkVoteJQ.dequeue();

							conf.voteSubmitAfter();
						}});
	            },
	            error: function (XMLHttpRequest, textStatus, errorThrown) {
					stateChange('no');
					linkVoteJQ.dequeue();
				}});
		});
	}
    
    jQuery(document).ready(
        function () {
            init(local_conf);
        });
};
