var gID = function(el){ return document.getElementById(el); }
if(!stage) var stage='';
CARE2.grabWidget={
    borderColor:null,buttonColor:null,widgetSize:null,widgetCode:null,widgetColors:null,clip:null,widgetImage:'green_orange',

    updateClipboard:function(sw,na_feedURL){
        if(sw=='na') this.feedURL = na_feedURL;
        this.widgetCode='<script type="text/JavaScript">var Care2P_Parameters=["'+this.feedURL+'", "'+this.widgetSize+'", "single", "'+this.publisherID+'", "0", "#'+this.borderColor+'", "#'+this.buttonColor+'", "#c739e"];</';
        this.widgetCode+='script><script type="text/JavaScript" src="http://dingo.care2.com/petitions/widget/common/petition_embed_br.js"></script>';

        var safeCode=this.widgetCode.replace(/\</g,'&lt;').replace(/\>/g,'&gt;');

        gID('embedCodeContent').innerHTML=safeCode;
    },

    init:function(d,t,f){
        var _self = this;
        if(t=="single" && f!="na"){
            f=f.slice(0,3)+'/'+f.slice(3,6)+'/'+f.slice(6,9);
            this.feedURL='http://'+CARE2.stage+'www.thepetitionsite.com/xml/petitions/'+f+'/feed.rss';
        } else if(f=="na"){
            get_na_petid=function(){
                if(petitionsFormObj.petitionID) {
                    clearInterval(check_na_petid);
                    var na_petid=petitionsFormObj.petitionID.slice(0,3)+'/'+petitionsFormObj.petitionID.slice(3,6)+'/'+petitionsFormObj.petitionID.slice(6,9);
                    var na_feedURL='http://'+CARE2.stage+'www.thepetitionsite.com/xml/petitions/'+na_petid+'/feed.rss';
                    _self.updateCode('na',na_feedURL);
                }
            }
            $(function(){ check_na_petid = setInterval(get_na_petid,250); });
        }else{
            f=f||"animal-welfare";
            f=f.replace("non-profit", "npo");
            if(f.search('browse-petitions')!=-1){
                f=(f.search('/')!=-1)?f.split('/')[1]:f.replace('browse-petitions','all');
            } else if(f.search('all')==-1) {
                if((f.search('/')==-1) || (f.search('activist')==-1 && f.search('hottest')==-1 && f.search('npo')==-1)) { f=f+'/all'; }
            }
            this.feedURL='http://'+CARE2.stage+'www.thepetitionsite.com/feeds/category/'+f+'/feed.rss';
        }

        this.publisherID=(CARE2.loggedIn() && CARE2.loggedIn()!=false)?CARE2.loggedIn().passportID:'1007';

        if(CARE2.library=="jQuery"){
            $(d).bind('click',{obj:this},this.showPopup);
        }else{
            CARE2.addListener(d,"click",this.showPopup,this);
        }
    },

    floaterCallback:function(){
        this.widgetSize="small";
        this.borderColor="7cbb42";
        this.buttonColor="eb6924";
        this.updateClipboard();

        if(gID('widget_preview_data')){
            gID(this.widgetImage).innerHTML = gID('widget_preview_data').innerHTML;
        }
        gID(this.widgetImage).style.display="block";

        this.clip.setText(this.widgetCode);
        this.clip.setHandCursor(true);
        this.clip.glue('grab_wdgt_copy_code');
        this.clip.addEventListener( 'onComplete', function(){
            gID('grab_wdgt_copy_code').style.display="none";
            gID('copied_code').style.display="block";
        });

        var obj=this;
        setTimeout(function(){obj.resizeFloater()},5);
    },

    updatePreview:function(){
        gID(this.widgetImage).style.display="none";
        this.widgetColors=gID("widgetColorValue").value;
        this.widgetImage=this.widgetColors.split('|')[2];
        if(gID('widget_preview_data')){
            gID(this.widgetImage).innerHTML = gID('widget_preview_data').innerHTML;
        }
        gID(this.widgetImage).style.display="block";

        this.updateCode();
    },

    updateCode:function(sw,na_feedURL){
        this.widgetSize = gID("widgetSizeValue").value;
        this.widgetColors = gID("widgetColorValue").value;
        this.borderColor = this.widgetColors.split('|')[0];
        this.buttonColor = this.widgetColors.split('|')[1];

        this.updateClipboard(sw,na_feedURL);
        this.clip.setText(this.widgetCode);
        gID('grab_wdgt_copy_code').style.display="block";
        gID('copied_code').style.display="none";

        var obj=this;
        setTimeout(function(){obj.resizeFloater()},5);
    },

    resizeFloater:function(){
        if(this.floater){
            this.floater.resizeBox();
        }else{
            $.colorbox.resize();
        }
    },

    showPopup:function(e,obj){
        if(CARE2.library=="jQuery"){
            obj=e.data.obj;
            $.colorbox({inline:true, href:"#petitionWidget",transition:"none",initialWidth:10,initialHeight:10},function(){obj.floaterCallback();});
            e.preventDefault();
        }else{
            obj.floater=CARE2.floater("petitionWidget",{branding:"care2",_onshow:function(){obj.floaterCallback();}});
            YAHOO.util.Event.preventDefault(e);
        }
    }
}