/////////////////////////////////////////////////////////
//
//  File: voting script for people's choice awards
//  Author: Craig Nelson, Classic Labs Development
//

    function turnOffVoting() {
        // disable voting after predetermined date and hour
        var endDate = new Date("January 24, 2012 12:00:00").getTime(); // end time for this event is noon.
        var today = new Date().getTime();
    
        return (today > endDate) ? true : false;
    }

    function disableVoteImage() {
        if (turnOffVoting() || readCookie('voted-ctabc')) {
            $$('.vote').each(function (item) {    
                item.src = "http://assets.ascribehq.com/integrations/clients/abc_connecticut/images/voting-disabled.gif";
            });
        }
        
        return;
    }
    
    function showEmailRequest(el) {
        if (readCookie('voted-ctabc')) {
            alert('You have already voted.');
            return;
        }
    
        if (turnOffVoting()) {
            alert('Voting has ended.');
            return;
        }
    
        var allDropDownEls = $$('.portfolio .email-toggle');
        var dropDownEL = $(el).next(".email-toggle");
    
        allDropDownEls.each(function (e) {
            if (e.visible()) {
                Effect.toggle(e, 'appear', { duration: .5 });
            }
        });
    
        Effect.toggle(dropDownEL, 'appear', { duration: .5 });
    }

    function vote(title, image, emailAddress, el) {
        emailAddress = emailAddress.value;
        
        if (navigator.cookieEnabled) {
            if(confirm('Do you want to vote for ' + title + '? Please note: one vote per person.')) {
                if (emailAddress) {
                    image.src = "http://assets.ascribehq.com/integrations/clients/abc_connecticut/php/vote-process.php?vote=" + escape(title) + "&email=" + escape(emailAddress);
                    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);
                    createCookie('voted-ctabc', 1, new_years_date);
                
                    Effect.toggle(el, 'appear', {
                        duration: .5,
                        afterFinish: function () {
                            alert('Thank you for voting.');
                            disableVoteImage();
                        }
                    });
                }
                else {
                    alert('You must enter an email address.');
                }
            }
        }
        else {
            alert('You must have cookies enabled to vote.\nPlease enable cookies, then refresh this page.');
        }
    }

    // cookies
    function createCookie(name,value,days) {
    	if (days) {
    		var expires = "; expires=" + days.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;
    }

    function eraseCookie(name) {
    	createCookie(name, "", -1);
    }
    
    document.observe("dom:loaded", function () {
        disableVoteImage();
    });
