var theme_url = '';
var img_url = theme_url + '';
var swf_url = theme_url + '/media';
var express_install = swf_url + '/expressInstall.swf';

jQuery(document).ready(function(){
    if (jQuery('#main.frontpage').get(0)) {
        Bpoe.frontpage.init();
    }
    else if (jQuery('#main.media-gallery').get(0)) {
        Bpoe.mediagallery.init();
    }
    else if (jQuery('#main.premiers-page').get(0)) {
        Bpoe.premierspage.init();
    }

    jQuery('a.play-video').click(Bpoe.media_popup_link);

    Bpoe.swf_video();

    Bpoe.sifr.init();
  /*
  $('#cf3_field_1').keyup(function(){
//    limitChars ('cf3_field_1', 250, 'charlimitinfo');
    limitWords ('cf3_field_1', 250, 'charlimitinfo');
  });
  $('#cf7_field_1').keyup(function(){
//    limitChars ('cf7_field_1', 250, 'charlimitinfo');
    limitWords ('cf7_field_1', 250, 'charlimitinfo');
  });

*/
  
/*
    jQuery('#header a.increase').click(Bpoe.textsize.increase);
    jQuery('#header a.decrease').click(Bpoe.textsize.decrease);

    if (jQuery.cookie('textsize')) {
        Bpoe.textsize.update(jQuery.cookie('textsize'));
    }
*/
    if (jQuery('#main.whats-happening').get(0)) {
        Bpoe.map.init();
    }

    if (jQuery('#main.upload').get(0)) {
        Bpoe.pride_form.init();
    }

    if (jQuery('#main.show-your-bc-pride').get(0)) {
        Bpoe.pride_list.init();
    }
});

var Bpoe = {
/*
    textsize: {
        current: function() {
            var fontsize = jQuery('body').css('font-size');
            fontsize = parseFloat(fontsize, 10);
            return fontsize;
        },

        update: function(value) {
            var size = parseFloat(value);
            Bpoe.textsize.save(size);
            jQuery('body').css('font-size', size + 'px');
        },

        increase: function() {
            if (Bpoe.textsize.current() < 19) {
                Bpoe.textsize.update(Bpoe.textsize.current()+2);
            }
            return false;
        },

        decrease: function() {
            if (Bpoe.textsize.current() > 9) {
                Bpoe.textsize.update(Bpoe.textsize.current()-2);
            }
            return false;
        },

        save: function(value) {
            jQuery.cookie('textsize', value);
        }
    },
*/
    swf_video: function() {
        jQuery('.swf-video').not('.swf-video-replaced').each(function() {
            var vidname = jQuery(this).attr('id');
            swfobject.embedSWF(swf_url + '/bpoe_player.swf', vidname, "740", "420", "8.0.0", express_install, { vidpath : swf_url + '/' + vidname + '.flv' });
            jQuery(this).addClass('swf-video-replaced');
        });
        jQuery('.swf-video-740x501').not('.swf-video-replaced').each(function() {
            var vidname = jQuery(this).attr('id');
            swfobject.embedSWF(swf_url + '/bpoe_player_740x501.swf', vidname, "740", "501", "8.0.0", express_install, { vidpath : swf_url + '/' + vidname + '.flv' });
            jQuery(this).addClass('swf-video-replaced');
        });
    },

    frontpage: {
        init: function() {
            swfobject.embedSWF(swf_url + '/Carousel.swf', 'slideshow', "960", "540", "8.0.0", express_install, { /*swfpath : swf_url + '/frontpage_images.swf'*/ }, {wmode:'transparent'} );
        }
    },

    mediagallery: {
        init: function() {
            swfobject.embedSWF(swf_url + '/view_submissions.swf', 'view-submissions', "960", "540", "8.0.0", express_install, { /*swfpath : swf_url + '/frontpage_images.swf'*/ }, {wmode:'transparent'} );
        }
    },

    premierspage: {
        init: function() {
            swfobject.embedSWF(swf_url + '/premier_gallery.swf', 'premier-flash', "960", "540", "8.0.0", express_install, { }, {wmode:'transparent'} );
        }
    },
  
    map: {
        init: function() {
            var lcId = new Date().getTime();

            swfobject.embedSWF(swf_url + '/map_preloader.swf', 'interactive-map', "933", "553", "8.0.0", express_install, { swfpath : swf_url + '/map.swf', lcId : lcId });

            Bpoe.map.proxy = new FlashProxy(lcId, swf_url + '/JavaScriptFlashGateway.swf');

            jQuery('li.project a').click(function() {
                var id = jQuery(this).attr('id');
                id = id.replace('project-', '');
                Bpoe.map.proxy.call('focus_on', id);
                return false;
            });
        }
    },

    pride_form: {
        init: function() {
            var obj = jQuery('form div.radio-list input:checked');

            Bpoe.pride_form.show_hide(obj.get(0));

            jQuery('form div.radio-list input').click(function() {
                Bpoe.pride_form.show_hide(this);
            });

            $('.youtube-help').hide();
            var help = $('<a href="#">Help</a>').toggle(function() {
                $('.youtube-help').show();
            },
            function() {
                $('.youtube-help').hide();
            });

            $('div.field-video label').append(' (').append(help).append(')');

            jQuery('div.field-photo p.current-file').each(function() {
                jQuery('div.field-photo input').hide();
                jQuery(this).append(' <a href="#" class="change-link">Change</a>');
                jQuery('div.field-photo a.change-link').click(function() {
                    jQuery('div.field-photo input').show();
                    jQuery('div.field-photo p.current-file').remove();
                    return false;
                });
            })
        },

        show_hide: function(obj) {
            var fields = jQuery('fieldset.types div.field');
            fields.hide();
            if (obj) {
                jQuery('form p.please-choose').hide();
            }
            else {
                jQuery('fieldset.types').after('<p class="please-choose">Please choose a content type above to continue...</p>');
            }
            switch(parseInt(jQuery(obj).val())) {
                case 22:
                    fields.filter('.field-story').show();
                    break;
                case 23:
                    fields.filter('.field-photo, .field-description').show();
                    break;
                case 24:
                    fields.filter('.field-video, .field-description').show();
                    break;
            }
        }
    },

    pride_list: {
        init: function() {
            jQuery('li.article h3 a, li.article a.preview').not('.story-preview, .story').click(Bpoe.media_popup_link);

            jQuery('form.controls input[type=radio]').click(Bpoe.pride_list.submit_filter);
            jQuery('form.controls select').change(Bpoe.pride_list.submit_filter);
            jQuery('form.controls input[type=submit]').remove();
        },

        submit_filter: function() {
            this.form.submit();
            return true;
        }
    },

    media_popup_link: function() {
        if (!jQuery('#popup-media').get(0)) {
            jQuery('body').addpopup('popup-media', 'popup-media');
        }

        jQuery('#popup-media').hide();
        jQuery('#popup-loading').center();
        jQuery('#popup-loading, #overlay').show();

        jQuery('#popup-media').load(jQuery(this).attr('href') + '?ajax=1', {}, function() {
            jQuery('#slideshow').hide();

            if (jQuery.browser.msie && jQuery.browser.version < 7) {
                jQuery('select').hide();
            }

            jQuery(this).center().append('<a href="#" class="close">Close</a>');
            jQuery('#popup-media a.close, #overlay').click(function() {
                jQuery('#popup-media, #overlay').html('').hide();
                jQuery('#slideshow').show();

                if (jQuery.browser.msie && jQuery.browser.version < 7) {
                    jQuery('select').show();
                }
                return false;
            });
            jQuery('#popup-loading').hide();
            jQuery('#popup-media, #overlay').show();

            Bpoe.swf_video();
            Bpoe.sifr.init();
        });
        return false;
    },

    sifr: {
        init: function() {
            if(typeof sIFR == "function"){
                sIFR.replaceElement(".sifr", named({
                    sFlashSrc: swf_url + '/myriadpro-semibold.swf',
                    sColor: "#febb25",
                    sWmode: "transparent"
                }));
                sIFR.replaceElement(".sifr-white", named({
                    sFlashSrc: swf_url + '/myriadpro-semibold.swf',
                    sColor: "#ffffff", //"#082457",
                    sWmode: "transparent"
                }));
        
                sIFR.replaceElement(".sifr-blue", named({
                    sFlashSrc: swf_url + '/myriadpro-semibold.swf',
                    sColor: "#1a4c97",
                    sWmode: "transparent"
                }));
            }
        }
    }
}

jQuery.fn.addpopup = function(id, classes) {
    this.append('<div id="overlay" style="height: ' + jQuery('body').height() + 'px;"></div>');
    this.append('<div class="popup loading" id="popup-loading"><img src="' + img_url + '/loading.gif" /> Loading...</div>');
    this.append('<div class="popup ' + classes + '" id="' + id + '"></div>');
};

jQuery.fn.reposition = function(x, y) {
    this.css('top', y + 'px');
    this.css('left', x + 'px');
    return this;
}

function limitChars (textid, limit, infodiv) 
{
  var text = $('#'+textid).val(); 
  var textlength = text.length;
  if(textlength > limit)
  {
//    $('#' + infodiv).html('You cannot write more then '+limit+' characters!');
    $('#'+textid).val(text.substr(0,limit));
    return false;
  }
  else
  {
    $('#' + infodiv).html('(' + (limit - textlength) + ')');
    return true;
  }
}

function limitWords (textid, limit, infodiv)
{
  var text = $('#'+textid).val(); 
  var arr = text.split(/s/);
  var wordcount = arr.length - 1;
  if (wordcount > limit)
  {
    var newstr = "";
    for (i = 0; i < limit; i++)
    {
      if (i != 0)
        newstr += " ";
      newstr += arr[i];
    }
    $('#'+textid).val(newstr);
  }
  else
  {
    $('#' + infodiv).html('(' + (limit - wordcount) + ')');
    return true;
  }
}


function ellipsis(str, end){
  if(str.length<=end){return str;}
  
  x=str.substring(0, end);
  y=x.split(" ");
  y.pop();
  z=y.join(" ");
  
  return z;
}


/*do browser stuff*/
dom = (document.getElementById) ? true : false;
ns5 = ((navigator.userAgent.indexOf("Gecko")>-1) && dom) ? true: false;
ie5 = ((navigator.userAgent.indexOf("MSIE")>-1) && dom) ? true : false;
ns4 = (document.layers && !dom) ? true : false;
ie4 = (document.all && !dom) ? true : false;
nodyn = (!ns5 && !ns4 && !ie4 && !ie5) ? true : false;

var zIndexXPR = 1;

function openhide(iidee)
{
zIndexXPR++;

var theLyr = (ns4)? document.layers[iidee] : (ie4)? document.all[iidee].style : (ie5||ns5)?   document.getElementById(iidee).style: null;        
theLyr.display= theLyr.display=='none' ? '' : 'none';
theLyr.zIndex = zIndexXPR;

}

function showDiv(iidee)
{

zIndexXPR++;

var theLyr = (ns4)? document.layers[iidee] : (ie4)? document.all[iidee].style : (ie5||ns5)?   document.getElementById(iidee).style: null;        
theLyr.display= '';
theLyr.zIndex = zIndexXPR;
}

function hideDiv(iidee)
{

var theLyr = (ns4)? document.layers[iidee] : (ie4)? document.all[iidee].style : (ie5||ns5)?   document.getElementById(iidee).style: null;        
theLyr.display= 'none';
}




var openPopup = window.open;

function show_msg(msg) { alert(msg); }

function validateForm() {
	missing_required = 0;

	for (i = 0; i < arguments.length; i++) {
		if(arguments[i] == '') {
			missing_required = 1;
		}
	}

	if(missing_required) {
		alert("A required form field is missing.");
		return false;
	} else {
		return true;
	}
}

function expCustomLink(myURL) {
	location.href = myURL;
}

function setUrl(path) {
	document.location.href = path;
}

function expArticleLink(sectionId, articleId) {
	location.href = '/index.php?section_id=' + sectionId + '&section_copy_id=' + articleId;
}

function expPopupWindow(url, widthVal, heightVal, resizableVal, scrollbarsVal, toolbarVal, locationVal, directoriesVal, statusVal, menubarVal, copyHistoryVal) {

	var attributes = "width="  	 	 + widthVal       +
				 	 ",height=" 	 + heightVal      +
				 	 ",resizable="  + resizableVal  +
				 	 ",scrollbars="  + scrollbarsVal  +
				 	 ",toolbar=" 	 + toolbarVal 	  +
				 	 ",location=" 	 + locationVal 	  +
				 	 ",directories=" + directoriesVal +
				 	 ",status=" 	 + statusVal 	  +
				 	 ",menubar=" 	 + menubarVal 	  +
				 	 ",copyhistory=" + copyHistoryVal;

	window.open(url, 'WindowName', attributes);
}

function xprLog(msg) {
    if (window.console) console.log(msg);
}

if (document.addEventListener) {  
    document.addEventListener("DOMContentLoaded", function () { if (typeof onXprPageLoad == 'function') onXprPageLoad(); }, false);
} else if (document.attachEvent) {  
    document.attachEvent('DOMContentLoaded', function () { if (typeof onXprPageLoad == 'function') onXprPageLoad(); });
}  

