/////////////////////////////////////////////////////////////////////
//
//  File: voting
//  Authors: Craig Nelson and Nick Mathis
//
//
  
    var Ascribe = Ascribe || {};
  
    Ascribe.Voting = function (config) {
        // scope correction
        var that = this;
    
        // user defined configs
        this.startDate = config.startDate;
        this.startDateTime = this.startDate.getTime();
        this.endDate = config.endDate;
        this.endDateTime = this.endDate.getTime();
        this.today = new Date().getTime();
        this.voteImageSelector = config.voteImageSelector || ".vote";
        this.emailDropDownSelector = config.emailDropDownSelector || "#portfolio .email-toggle";
    
        // account configs
        this.path = window.location.pathname; // current path
        this.permalink = this.path.match(/^\/[^\/]+\//); // match for permalink portion of path
        this.account = this.permalink.toString().replace(/\//g, ""); // strip forward slashes to get permalink and use it as account name
        this.voteCookie = "vote-" + this.account; // create string for cookie from account name
    
        this.startDateNotice = function () {
            var startYear = this.startDate.getFullYear();
            var startMonth = this.startDate.getMonth() + 1;
            var startDay = this.startDate.getDate();
  		    return startMonth + "/" + startDay + "/" + startYear + ".";
        }; // startDateTimeNotice()
    
        this.voteRequest = function (el) {
            //  Filter all vote requests
            if (this.readCookie(this.voteCookie)) { // set cookie to account name so cookies can be set on multiple accounts
                alert("You have already voted.");
                return;
            }
            // do not allow voting until a predifined date
            if (this.today < this.startDateTime) {
                alert("Voting does not begin until " + this.startDateNotice()); // the date for this alert will change each year
                return;
            }
            // disable voting after predefined date
            if (this.today > this.endDateTime) {
                alert("Voting has ended. Thank you for participating.");
                return;
            }
            this.showEmailRequest(el);
        }; // voteRequest()
    
        this.setVoteImage = function () {
            if (this.readCookie(this.voteCookie) || this.today < this.startDateTime || this.today > this.endDateTime) {
                $$(this.voteImageSelector).each(function(item){
                    item.src = "http://assets.ascribehq.com/integrations/clients/hanley_wood/images/voting-disabled.gif";
                });
            }
            else {
                $$(this.voteImageSelector).each(function(item){
                    item.src = "http://assets.ascribehq.com/integrations/clients/hanley_wood/images/vote.gif";
                });
            }
        }; // setVoteImage()
    
        this.showEmailRequest = function (el) {
            var allDropDownEls = $$(this.emailDropDownSelector);
            var dropDownID = $(el).up().next(3).identify();
            allDropDownEls.each(function(e) {
                if (e.visible()) {
                    Effect.toggle(e, 'appear', {duration: .2});
                }
            });
            Effect.toggle(dropDownID, 'appear', {duration: .2});
        }; // showEmailRequest()
    
        this.vote = function (title, image, emailAddress) {
            if (this.readCookie(this.voteCookie)) {
                alert("You have already voted.");
                return;
            }
            if (navigator.cookieEnabled) {
                if (emailAddress.blank()) {
                    alert("You must enter an email address.");
                }
                else {
                    if (confirm("Are you sure you want to vote for " + title + "? Please note: one vote per person, NOT one vote per category.")) {
                        image.src = "http://assets.ascribehq.com/integrations/clients/hanley_wood/php/vote-process.php?vote=" + escape(title) + "&email=" + escape(emailAddress) + "&account=" + escape(this.account); // for multiple accounts
                        var new_years_date = new Date();
                        new_years_date.setYear(new_years_date.getFullYear() + 1);
                        new_years_date.setMonth(0);
                        new_years_date.setDate(1);
                        this.createCookie(this.voteCookie, 1, new_years_date );
                        Effect.toggle(image.up().next(3).identify(), 'appear', {
                            duration: .2,
                            afterFinish: function() {
                                that.setVoteImage();
                                alert("Thank you for voting.");
                            }
                        });
                    }
                }
            }
            else {
                alert("You must have cookies enabled to vote.\nPlease enable cookies then refresh this page.");
            }
        }; // vote()
    
        this.createCookie = function (name,value,days) {
    	    if (days) {
    		    var expires = "; expires=" + days.toGMTString();
    	    }
    	    else var expires = "";
    	    document.cookie = name + "=" + value + expires + "; path=/";
        }; // createCookie()
    
        this.readCookie = function (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;
        }; // readCookie()
    
        this.eraseCookie = function (name) {
    	    this.createCookie(name, "", -1);
        }; // eraseCookie()
    
        this.setVoteImage();
    }; // constructor
