$(function(){
	$('a.external_link').attr("target", "_blank");
});

/*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});
});

/*random header images*/
/*$(function(){
	bgImageTotal=2;
	randomNumber = Math.round(Math.random()*(bgImageTotal-1))+1;
	$('#header').addClass("headerImage" + randomNumber);
});*/

$(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*/
var weditorImagesIndex = 1;
function initWeditorImagesAlts(){
	$('.weditor img.fleft, .weditor img.fright').each(function(i){
		if (!$(this).parent().is('.img-border-left') && !$(this).parent().is('.img-border-right') && (parseInt($(this).width()) > 0)) {
			if ($(this).is('.weditor img.fleft')) {
				$(this).wrap('<span class="img-border-left"></span>');
			} else {
				$(this).wrap('<span class="img-border-right"></span>');
			}
			$(this).addClass("imgAlts"+weditorImagesIndex);
			imgAlt = $('.weditor .imgAlts'+weditorImagesIndex).attr("alt");
			imgWidth = parseInt($(this).width()) + parseInt($(this).css('paddingLeft')) + parseInt($(this).css('paddingRight'));
			$(this).parent().css('width', imgWidth + "px");
			if ($(this).attr("alt").length > 0){
				$(".weditor .imgAlts"+weditorImagesIndex).after("<p>" +imgAlt+ "</p>");
			}
			weditorImagesIndex++;
		}
	});
}
$(function(){
	initWeditorImagesAlts();
});
$(window).load(function(){
	initWeditorImagesAlts();
});

$(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;
    });
});
