$(function(){
	$('a.external_link').attr("target", "_blank");
});
Cufon.replace('.slogan', { fontFamily: 'anivers', hover: true});
Cufon.replace('h1', { fontFamily: 'anivers', hover: true});
Cufon.replace('#hpColumns h1', { fontFamily: 'anivers', hover: true, textShadow: '#162b45 1px 1px'});

$(function(){
	bgImageTotal=4;
	randomNumber = Math.round(Math.random()*(bgImageTotal-1))+1;
	imgPath=('/web/papercraft/images/header'+randomNumber+'.jpg');
	$('#header').css("background",'url("'+imgPath+'")');
});

/*sucker fish menu init*/
jQuery(function(){
	jQuery('#topMenu ul').superfish({
		autoArrows:  false,
		animation:   {height:'show'},
		speed:       'fast',
		delay:        200,
		dropShadows:  false 
	}).find('ul').bgIframe({opacity:0});
});


$(document).ready(function(){
		
	//Remove outline from links
	$("a").click(function(){
		$(this).blur();
	});
	
	//When mouse rolls over
	$("#flyout ul li").hover(function(){
		$(this).find(".flyCont").stop().animate({height:'100px'},{queue:false, duration:600, easing: 'easeOutQuart'})
		$(this).find("a.menuLink").addClass("opened")
		if ($(this).index() == 0){
			$('#loginLink').addClass("shade")
		} else {
			$('#cartLink').addClass("shade")
		}
	}, function(){
		$(this).find(".flyCont").stop().animate({height:'0px'},{queue:false, duration:200, easing: 'easeOutCirc'})
		$(this).find("a.menuLink").removeClass("opened")
		if ($(this).index() == 0){
			$('#loginLink').removeClass("shade")
		} else {
			$('#cartLink').removeClass("shade")
		}
	});
	
});

/*replacing tag <hr> with stylable div*/
$(function(){
	$(".weditor").find("hr").wrap('<div class="fakeHr"></div>')
});
/*add alt text under image in weditor*/
$(function(){
	var imgAlt
	var imgWidth
	$('.weditor img.fleft').wrap('<div class="img-border-left"></div>')
	$('.weditor img.fright').wrap('<div class="img-border-right"></div>')
	
	$('.weditor img.fleft, .weditor img.fright').each(function (i){
		i = i+1;
		$(this).addClass("imgAlts"+i);
		imgAlt = $('.weditor .imgAlts'+i).attr("alt")
		imgWidth = $(this).width() + 8;
		$(this).parent().css('width', imgWidth + "px");
		if ($(this).attr("alt").length > 0){
			$(".weditor .imgAlts"+i).after("<p>" +imgAlt+ "</p>");
		}
	});
});

$(function() {
    $('div.widget a.pollAnswerButton').click(function() {
        var pollWidget = $(this).parents('div.widget:eq(0)');
        var pollAnswerId = $(this).attr('rel');

        formVars = {};
        formVars['random'] = new Date().getTime();
        formVars['ajax'] = 1;
        formVars['method'] = 'answerVote';
        formVars['answerId'] = pollAnswerId;
        ajaxUrl = '/ajax/polls/';
        $.ajax({
            type: 'GET',
            timeout: 30,
            url: ajaxUrl,
            data: formVars,
            async: false,
            dataType: 'xml',
            success: function(xmlResponse){
                if ($(xmlResponse).find('response').length > 0) {
                    if (($(xmlResponse).find('result').length > 0) && ($(xmlResponse).find('result').text() == 1)) {
                        var pollAnswerCount = pollWidget.find('#pollAnswerCount' + pollAnswerId);
                        var pollAnswersCount = pollWidget.find('.pollAnswersCount');
                        pollAnswerCount.text(parseInt(pollAnswerCount.text()) + 1);
                        pollAnswersCount.text(parseInt(pollAnswersCount.text()) + 1);

                        pollWidget.find('div.answer').each(function (i) {
                            try {
                                var thisAnswerCount = parseInt($(this).find('span.count').text());
                                var thisAnswerPercentage = (thisAnswerCount / pollAnswersCount.text()) * 100;
                                $(this).find('span.pollBar span').css('width', thisAnswerPercentage + '%');
                            } catch (exception) {

                            }
                        });
                    }
                    if ($(xmlResponse).find('message').length > 0) {
                        pollWidget.find('.pollMessage').text($(xmlResponse).find('message').text());
                    }
                }
            },
            error: function(err){
            	if ((err.status != 200) && (typeof(err.statusText) != 'undefined') && (err.statusText != '')) {
                    pollWidget.find('.pollMessage').text(err.statusText);
            	}
            }
        });

        return false;
    });
});
