var cookieName = 'homepageVote';
var votes = new Object();
$(document).ready(function(){

    //jquery carousel setup
    var curr = '';
    $('#carousel').cycle({
        speed: 1000,
        timeout: 0,
        activePagerClass: 'active',
        pager: 'ul#carousel-nav',
        pagerAnchorBuilder: function(idx, slide) {
            var currentReturn = 'ul#carousel-nav li a:eq(' + idx + ')';
            return currentReturn;
        }
    });

    var carouselNavi = $('ul#carousel-nav li');
    var curr = '';

    carouselNavi.bind('click', function(){
        curr = $(this);
    })
    if(carouselNavi.length)    {
        carouselNavi.mouseenter(function(){

            if(!$(this).attr('class').match('active')){
                carouselNavi.each(function(){
                    $(this).removeClass('active');
                });
            }
        });

        carouselNavi.mouseout(function(){
            if(curr.length > 0){
                curr.addClass('active');
            }
        });
    }

    var pollButtons = $('#pollFeed .poleBtn a');
    votes = getVotes();

    var currentPoll = parseInt(pollButtons.filter(':first').attr('rel'));

    if(undefined == votes[currentPoll]){
        pollButtons.filter(':first').click(function(){
            $('img',$(this)).attr('src','/images/relate-pole-1-clicked.gif');
            postVote(1);
            return false;
        });
        pollButtons.filter(':eq(1)').click(function(){
            $('img',$(this)).attr('src','/images/relate-pole-0-clicked.gif');
            postVote(0);
            return false;
        });
    } else {
        $('img', pollButtons.filter(':eq(' + (1 - votes[currentPoll]) + ')')).attr('src', '/images/relate-pole-' + votes[currentPoll] + '-clicked.gif');
        postVote('a');
    }

    function postVote(vote) {
        var callback = function(data){
            setVotes(vote);
            pollButtons.each(function(){
                $(this).unbind('click');
            });
            var results = $('#pollResults');
            $('span',results).html(data['results']);
            results.show('slow');
        };
        $.post(
            '/raw.php?page=vote',
            {
              vote: vote,
              question: $('#pollFeed p:first').html(),
              asset_id: $('#pollAssetId').html()
            },
            callback,
            'json'
        );
    }

    function getVotes(){
        var cookieResponse = $cookie(cookieName);
        var temp_votes = new Object();
        if(undefined != cookieResponse &&
           '' != cookieResponse) {
            cookieResponse = cookieResponse.split('¦');
            for(var i in cookieResponse){
                var tempVote = cookieResponse[i].split('~');
                if(undefined != tempVote[0] &&
                   undefined != tempVote[1]){
                    temp_votes[parseInt(tempVote[0])] = parseInt(tempVote[1]);
                }
            }
        }
        return temp_votes;
    }

    function setVotes(vote){
        var out = new Array();
        if(0 === vote || 1 === vote){
            votes[currentPoll] = vote;
            for(var i in votes){
                if(undefined != i &&
                   undefined != votes[i]){
                    out.push(i + '~' + votes[i]);
                }
            }
            out = out.join('¦');
            $cookie(cookieName, out);
        }
    }

    //common problems.
    var commonProblems = $('.common-problem');
    if(commonProblems.length > 0){
        commonProblems.each(function(){
            var commonProblemType = $(this).attr('id');
            var commonProblemList = $('#common-problem-list-'+commonProblemType);
            var commonProblemLink = $('#common-problem-open-link-'+commonProblemType);
            if(!$(this).attr('class').match('do-not-collapse')){
                commonProblemList.hide();
            }
            commonProblemLink.click(function(){
                var commonProblemOpenLinkSpan = $('span',commonProblemLink);
                if('view' == commonProblemOpenLinkSpan.html()){
                    commonProblemOpenLinkSpan.html('hide');
                    commonProblemList.show();
                }else{
                    commonProblemOpenLinkSpan.html('view');
                    commonProblemList.hide();
                }
                return false;
            });
        });
    }

	// telephone counselling expander
	$('.telephone-counselling-expanded').hide();
		$('.telephone-counselling').click(function(){
			$('.online-counselling-expanded, .telephone-counselling').hide();
			$('.telephone-counselling-expanded, .online-counselling').show();
		});

	// online counselling expander
	$('.online-counselling-expanded').hide();
		$('.online-counselling').click(function(){
			$('.telephone-counselling-expanded, .online-counselling').hide();
			$('.online-counselling-expanded, .telephone-counselling').show();
		});
});
