 var $j = jQuery.noConflict();
$j(document).ready(function(){

$j('.contact_mp .state-issues .state-issue a').click(function(e){
	e.preventDefault();
	var template = $j(this).siblings("p").html();
	$j('#mpForm textarea').text(template);
});
$j('form#signupForm #support_pack').change(function(event){
	thisValue = $j(this).val();

	if (thisValue == "No") {

		$j('form#signupForm li.delivery_details').slideUp('fast');
	}
	if (thisValue == "Yes") {

		$j('form#signupForm li.delivery_details').slideDown('fast');
	}
	
});
});


function setExternalLinks() {
    var el_list = document.getElementsByTagName('A');
    for (i=0; i<el_list.length; i++) {
        if (el_list[i].getAttribute('rel') == 'external') {
            el_list[i].setAttribute('target', '_blank');
        }
    }
}

function removeDialog() {
		
	$j('#splashContent').dialog('destroy');

}

function detectBrowser() {
    var browser=navigator.appName;
    var b_version=navigator.appVersion;
    var version=parseFloat(b_version);
    if ((browser=="Microsoft Internet Explorer") && (b_version.search("MSIE 6.0"))) {
        return true;
    }
}


function checkCookie() {
    showSplash=getCookie('RSPCAPASplash');
    if (showSplash==""||showSplash==null) {
	$j('#splashContent')
        .dialog({
            autoOpen: false,
            title: 'Basic Dialog',
            modal: true,
            width: 800,
            height: 500,
            position: 'center'
            
        }).dialog('open');
        setCookie('RSPCAPASplash','true',365);
      
    }

    if (showSplash=="true") {
	$j('#splashContent')
        .dialog({
            autoOpen: false,
            title: 'Basic Dialog',
            modal: true,
            width: 800,
            height: 500,
            position: 'center'
            
        }).dialog('open');
    }
}

function getCookie(c_name) {
    if (document.cookie.length>0)
    {
        c_start=document.cookie.indexOf(c_name + "=");
        if (c_start!=-1)
        {
            c_start=c_start + c_name.length+1;
            c_end=document.cookie.indexOf(";",c_start);
            if (c_end==-1) c_end=document.cookie.length;
            return unescape(document.cookie.substring(c_start,c_end));
        }
    }
    return "";
}
function setCookie(c_name, value,expiredays) {
    var exdate=new Date();
    exdate.setDate(exdate.getDate()+expiredays);
    document.cookie=c_name+ "=" +escape(value)+
    ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function showSplashCheck() {
    if (document.getElementById('splashCheck').checked) {
        setCookie('RSPCAPASplash','false',365);
    }
}


var poll_displayResults = function(response, id){
    if(response == false){
        $(id + "submit").disabled = true;
    } else if (response.indexOf('error') != -1) {
        alert('Please select an option from the poll before voting.');
        $(id + "submit").disabled = false;
    } else {
        $j('#' + id + ' li.submit, #' + id + ' ul li.radio span').fadeOut('slow', function() {
            $j('#' + id + ' ul').html(response.replace(/easypoll_bar/gi, "easypoll_bar hidden"));

            $j('.easypoll_bar, .result').fadeIn('slow');
        });
		$j('.poll .jaggedBoxBottom:visible').prepend('<div class="poll_link"><a href="/forum">Have your say</a></div>');
    }
	


};



function setupYT(ytlinks) {

    $j(document).ready( function () {

        $j('.ytlink').click( function () {
            index = $j('.ytlink').index(this);

            $j('#ytPlayer').html('<object width="352" height="222" type="application/x-shockwave-flash" data="http://www.youtube.com/v/'+ ytLinks[index] +'&hl=en&fs=1" id="bannerPlayer" style="visibility: visible;"><param name="movie" value="http://www.youtube.com/v/'+ ytLinks[index] +'&hl=en&fs=1"></param><param name="flashvars" value="pathToSWF=/sites/all/themes/monitor/swf/"/><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><param name="wmode" value="transparent" /></object>');
        });

        //set player to first video
        $j('#ytPlayer').html('<object width="352" height="222" type="application/x-shockwave-flash" data="http://www.youtube.com/v/'+ ytLinks[0] +'&hl=en&fs=1" id="bannerPlayer" style="visibility: visible;"><param name="movie" value="http://www.youtube.com/v/'+ ytLinks[0] +'&hl=en&fs=1"></param><param name="flashvars" value="pathToSWF=/sites/all/themes/monitor/swf/"/><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><param name="wmode" value="transparent" /></object>');

        $j('#ytWrapper ul').jcarousel();

    });
}

var adTabs;
var currentAd;

function homepageCarousel(page) {
$j('.poll .poll1 h3, .poll .poll1 div.easypoll').fadeIn("slow");
    adTabs = $j('.bannerNav li');
    currentAd = $j('.bannerNav li').index($j('.bannerNav li.active'));
    function fadeBanner() {
        if (currentAd < adTabs.length - 1) {
            currentAd++;
        } else {

            currentAd = 0;
            $j('.poll .poll5 h3').hide();
            $j('.poll .poll5 div.easypoll').hide();
        }

        goToBanner(currentAd);
        $j('.poll .poll'+(currentAd) + ' h3').hide();
        $j('.poll .poll'+(currentAd+1) + ' h3').fadeIn("slow");
        $j('.poll .poll'+(currentAd) + ' div.easypoll').hide();
        $j('.poll .poll'+(currentAd+1) + ' div.easypoll').fadeIn("slow");

    }

    function colorPick(bannerNum) {
        switch (bannerNum) {
            case 0:
                colorChange = '#B5C718';
                break;
            case 1:
                colorChange = '#33AFE8';
                break;
            case 2:
                colorChange = '#FEA000';
                break;
            case 3:
                colorChange = '#FF2E98';
                break;
            default:
                colorChange = '#B5C718';
        }
        return colorChange;
    }
	
    function goToBanner(bannerNum) {
		var firstFired = 1;
        colorChange = colorPick(bannerNum);

        if (page == 'home') {
		
            $j('.mainBgImg:visible, #banner h3:visible, #banner p:visible, .poll  h3:visible, .poll  div.easypoll:visible').fadeOut( 'slow', function () {
                if (firstFired) {
				
                    $j('#banner').attr('class', 'issue'+(bannerNum+1))
                    //fade in and animate background color
                    $j('.mainBgImg:eq('+(bannerNum+1)+'), #banner h3:eq('+bannerNum+'), #banner p:eq('+bannerNum+')').fadeIn('slow');
                    $j('.poll .poll'+(currentAd+1) + ' h3, .poll .poll'+(currentAd+1) + ' div.easypoll').fadeIn("slow");

                    /* Disabled while heading is off
                        $j('#banner .text h2').animate({
                        'backgroundColor':colorChange
                    });*/
                    $j('.bannerNav li.active a').animate({
                        'paddingBottom':'10px'
                    });
                    $j('.bannerNav li.active').animate({
                        'marginTop':'48px'
                    }, function() {
                        $(this).removeClass('active');
                        $j('.bannerNav li').eq(bannerNum).animate({
                            'marginTop':'18px'
                        }, function() {
                            $(this).addClass('active');
                        });
                        $j('.bannerNav li a').eq(bannerNum).animate({
                            'paddingBottom':'40px'
                        });
                    });
                    firstFired = 0;
                }
            });
			}
        else{
            if (page == 'states') {
                $j('.mainBgImg:visible, #banner h2:visible, #banner p:visible, .poll  h3:visible, .poll  div.easypoll:visible').fadeOut( 'slow', function () {
                    if (firstFired) {

                        $j('#banner').attr('class', 'issue'+(bannerNum+1))
                        //fade in and animate background color
                        $j('.mainBgImg:eq('+bannerNum+'), #banner h2:eq('+bannerNum+'), #banner p:eq('+bannerNum+')').fadeIn('slow');
                        $j('.poll .poll'+(currentAd+1) + ' h3, .poll .poll'+(currentAd+1) + ' div.easypoll').fadeIn("slow");

                        $j('.mainBgImg:eq('+bannerNum+'), #banner h2:eq('+bannerNum+'), #banner p:eq('+bannerNum+')').fadeIn('slow');
                        $j('#banner .text h1').animate({
                            'backgroundColor':colorChange
                        });

                        $j('.bannerNav li.active').removeClass('active');
                        $j('.bannerNav li').eq(bannerNum).addClass('active');
                        firstFired = 0;
                    }
                });
            }
            else {
                $j('.mainBgImg:visible, #banner h2:visible, #banner p:visible, #banner .readmore:visible').fadeOut( 'slow', function () {
                    if (firstFired) {
                        $j('#banner').attr('class', 'issue'+(bannerNum+1))
                        //fade in and animate background color
                        $j('.mainBgImg:eq('+bannerNum+'), #banner h2:eq('+bannerNum+'), #banner p:eq('+bannerNum+')').fadeIn('slow');
                        $j('#banner .text h1').animate({
                            'backgroundColor':colorChange
                        });

                        $j('.bannerNav li.active').removeClass('active');
                        $j('.bannerNav li').eq(bannerNum).addClass('active');
                        firstFired = 0;
                    }
                });
                firstFired = 1;
            }
        }
    }
    $j('#banner .hidden').removeClass('hidden').hide();
    if ($j('h1').attr('id') == 'logo')
        //$j('#banner h2').css('background', colorPick(currentAd)); //disabled while heading off also in another place above
        $j('#banner h2').css('background', '#fffff'); //disabled while heading off
    else
        $j('#banner h1').css('background', colorPick(currentAd));
    //timeOut = setInterval(fadeBanner, 30000000);

    $j('.bannerNav a').click(function(event) {
		$j('.poll_link').remove();
        event.preventDefault();
		
        if (!($j(this).parent('li').hasClass('active'))) {

            //clearTimeout(timeOut);

            currentAd = $j('.bannerNav a').index($j(this));

            goToBanner(currentAd);

        //timeOut = setInterval(fadeBanner, 8000);
        }
		else 
		{
		if ($j("p.initial").is(":visible")){
		
            currentAd = $j('.bannerNav a').index($j(this));

            goToBanner(currentAd);
		}
		}
  $j('p.initial').addClass("hidden").hide();
    });
}




