/*
>--------------------------------------------------------------------------------<
| CREDITS INFORMATION															 |
>--------------------------------------------------------------------------------<
| Created By: Benny McLennan													 |
| File Name: mainJavascript.js													 |
| File Location: javascripts													 |
| Generic Description: All main JavaScript for the BMF Generic Framework		 |
| Date Created: May 20th 2009													 |
| Date Edited: N/A																 |
| Copyright: © Army Roster 2008 - 2009											 |
>--------------------------------------------------------------------------------<
| Website: http://www.armyroster.com											 |
| Website Restriction: Level 0 													 |
| Important Notes: Required on every page of the website. Website runs off AJAX  |
| Date Installed: May 27th 2009	 												 |
| Date Modified: N/A															 |
|																				 |
| Notes: If you wish to use this script, please feel free. No permission is 	 |
|        required. We would, however, like a brief email notifying us of your use|
| 		 of our script(s). 														 |
| 						 														 |
|		 Thank you,																 |
|		 Benny																	 |
>--------------------------------------------------------------------------------<
*/

function pretend_submit(){
}  
function GetXmlHttpObject() {
	var objXMLHttp=null
	if (window.XMLHttpRequest) {
		objXMLHttp=new XMLHttpRequest()
	}
	else if (window.ActiveXObject) {
		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
	}
	return objXMLHttp
}

function resizeImages () {
	var all_images = document.getElementsByTagName("img");
	
	for(count=0; count < all_images.length; count++) {
	  var img = all_images[count];
	
	  if(img.className === "bbcode_img") {
		var imgH = img.clientHeight;
		var imgW = img.clientWidth;
		var scaleW = (470 / imgW);
		if(imgW > 470) {
		  img.style.width = "470px";
		  img.style.height = Math.round(imgH * scaleW) + "px";
		}
	  }
	}
}  
function resizeHomeImages () {
	var all_images = document.getElementsByTagName("img");
	
	for(count=0; count < all_images.length; count++) {
	  var img = all_images[count];
	
	  if(img.className === "bbcode_img") {
		var imgH = img.clientHeight;
		var imgW = img.clientWidth;
		var scaleW = (595 / imgW);
		if(imgW > 595) {
		  img.style.width = "595px";
		  img.style.height = Math.round(imgH * scaleW) + "px";
		}
	  }
	}
}  

function loadingScript(){
	document.getElementById("TheLoader").style.background = "url(images/staticbox/Loading.gif) no-repeat 0 -20px";
}
function loadedScript(){
	document.getElementById("TheLoader").style.background = "url(images/staticbox/Loading.gif) no-repeat 0 0";
}
function newhash (NewURL) {
	var URLcheck = "#" + NewURL;
	if (window.location.hash === URLcheck) {
		AR_getter();
	} else {
		loadingScript();	
	}
	window.location.hash = NewURL;
	var curHashVal = window.location.hash;
	$('#dialog').dialog('close');
	$('#dialogprofile').dialog('close');
	$('#newthread').dialog('close');
}

function comer (NewURL) {
	if (NewURL == '') {
	window.location.hash = "action=home";
	} else {
	window.location.hash = "#";
	window.location.hash = NewURL;
	AR_getter();
	}
}

setInterval(function(){	
	if(curHashVal!=window.location.hash){ 
		curHashVal = window.location.hash;
		AR_getter()
	}
	if (window.location.hash == "#action=home") {
	resizeHomeImages();
	} else {
	resizeImages();
	}
}, 500);

var curHashVal = window.location.hash;

function dummy_message () {
	$('#dialog').dialog('open');
	$('#dialog').dialog({
		bgiframe: false,
		closeOnEscape: true,
		draggable: true,
		resizable: false,
		width:632,
		modal: true,
	});
}


var ddsitelogo={
	
	setting: {orientation:4, visibleduration:0, fadeduration:[1000, 500]}, //orientation=1|2|3|4, duration=millisec or 0, fadedurations=millisecs
	offsets: {x:10, y:10},//offset of logo relative to window corner
	logoHTML: '<div id="staticBox"><div id="TheLoader" style="background: url(images/staticbox/Loading.gif) no-repeat; height: 20px; width: 175px; border: 1px solid #000"></div></div>', //HTML for logo, which is auto wrapped in DIV w/ ID="mysitelogo"

	coord: {},

	keepfixed:function(){
		if (!this.cssfixedsupport){
			var $window=jQuery(window)
			var is1or3=/^[13]$/.test(this.setting.orientation)
			var is1or2=/^[12]$/.test(this.setting.orientation)
			var x=$window.scrollLeft() + (is1or3? this.offsets.x : $window.width()-this.$control.width()-this.offsets.x)
			var y=$window.scrollTop() + (is1or2? this.offsets.y : $window.height()-this.$control.height()-this.offsets.y)
			this.$control.css({left:x+'px', top:y+'px'})
		}
	},

	showlogo:function(){
		var mainobj=ddsitelogo
		this.$control.animate({opacity:1}, this.setting.fadeduration[0])
		if (this.setting.visibleduration>0){
			setTimeout(function(){
				mainobj.$control.stop().animate({opacity:0}, mainobj.setting.fadeduration[1], function(){
					jQuery(window).unbind('scroll.fixed resize.fixed')
				})
			}, this.setting.visibleduration+this.setting.fadeduration[0])
		}
	},
	
	init:function(){
		jQuery(document).ready(function($){
			var mainobj=ddsitelogo
			var iebrws=document.all
			mainobj.cssfixedsupport=!iebrws || iebrws && document.compatMode=="CSS1Compat" && window.XMLHttpRequest //not IE or IE7+ browsers in standards mode
			if (mainobj.cssfixedsupport){
				mainobj.coord[(/^[13]$/.test(mainobj.setting.orientation))? 'left' : 'right']=mainobj.offsets.x
				mainobj.coord[(/^[12]$/.test(mainobj.setting.orientation))? 'top' : 'bottom']=mainobj.offsets.y
			}
			mainobj.$control=$('<div id="mysitelogo">'+mainobj.logoHTML+'</div>')
				.css({position:mainobj.cssfixedsupport? 'fixed' : 'absolute', opacity:0})
				.css(mainobj.coord)
				.appendTo('body')
			if (document.all && !window.XMLHttpRequest && mainobj.$control.text()!='') //loose check for IE6 and below, plus whether control contains any text
				mainobj.$control.css({width:mainobj.$control.width()}) //IE6- seems to require an explicit width on a DIV containing text
			mainobj.keepfixed()
			mainobj.showlogo()
			$(window).bind('scroll.fixed resize.fixed', function(){mainobj.keepfixed()})
		})
	}
}

ddsitelogo.init()


//** jQuery Scroll to Top Control script- (c) Dynamic Drive DHTML code library: http://www.dynamicdrive.com.
//** Available/ usage terms at http://www.dynamicdrive.com (March 30th, 09')
//** v1.1 (April 7th, 09'):
//** 1) Adds ability to scroll to an absolute position (from top of page) or specific element on the page instead.
//** 2) Fixes scroll animation not working in Opera. 


var scrolltotop={
	//startline: Integer. Number of pixels from top of doc scrollbar is scrolled before showing control
	//scrollto: Keyword (Integer, or "Scroll_to_Element_ID"). How far to scroll document up when control is clicked on (0=top).
	setting: {startline:100, scrollto: "", scrollduration:500, fadeduration:[2000, 100]},
	controlHTML: '<img src="up.png" style="width:48px; height:48px" />', //HTML for control, which is auto wrapped in DIV w/ ID="topcontrol"
	controlattrs: {offsetx:5, offsety:5}, //offset of control relative to right/ bottom of window corner
	anchorkeyword: 'scroll', //Enter href value of HTML anchors on the page that should also act as "Scroll Up" links

	state: {isvisible:false, shouldvisible:false},

	scrollup:function(scrollIng){
		this.setting.scrollto = scrollIng;
		if (!this.cssfixedsupport) //if control is positioned using JavaScript
			this.$control.css({opacity:0}) //hide control immediately after clicking it
		var dest=isNaN(this.setting.scrollto)? this.setting.scrollto : parseInt(this.setting.scrollto)
		if (typeof dest=="string" && jQuery('#'+dest).length==1) //check element set by string exists
			dest=jQuery('#'+dest).offset().top
		else
			dest=0
		this.$body.animate({scrollTop: dest}, this.setting.scrollduration);
	},

	keepfixed:function(){
		var $window=jQuery(window)
		var controlx=$window.scrollLeft() + $window.width() - this.$control.width() - this.controlattrs.offsetx
		var controly=$window.scrollTop() + $window.height() - this.$control.height() - this.controlattrs.offsety
		this.$control.css({left:controlx+'px', top:controly+'px'})
	},

	togglecontrol:function(){
		var scrolltop=jQuery(window).scrollTop()
		if (!this.cssfixedsupport)
			this.keepfixed()
		this.state.shouldvisible=(scrolltop>=this.setting.startline)? true : false
		if (this.state.shouldvisible && !this.state.isvisible){
			this.$control.stop().animate({opacity:1}, this.setting.fadeduration[0])
			this.state.isvisible=true
		}
		else if (this.state.shouldvisible==false && this.state.isvisible){
			this.$control.stop().animate({opacity:0}, this.setting.fadeduration[1])
			this.state.isvisible=false
		}
	},
	
	init:function(){
		jQuery(document).ready(function($){
			var mainobj=scrolltotop
			var iebrws=document.all
			mainobj.cssfixedsupport=!iebrws || iebrws && document.compatMode=="CSS1Compat" && window.XMLHttpRequest //not IE or IE7+ browsers in standards mode
			mainobj.$body=(window.opera)? (document.compatMode=="CSS1Compat"? $('html') : $('body')) : $('html,body')
			mainobj.$control=$('<div></div>')
				.css({position:mainobj.cssfixedsupport? 'fixed' : 'absolute', bottom:mainobj.controlattrs.offsety, right:mainobj.controlattrs.offsetx, opacity:0, cursor:'pointer'})
				.attr({title:'Scroll Back to Top'})
				.click(function(){mainobj.scrollup(); return false})
				.appendTo('body')
			if (document.all && !window.XMLHttpRequest && mainobj.$control.text()!='') //loose check for IE6 and below, plus whether control contains any text
				mainobj.$control.css({width:mainobj.$control.width()}) //IE6- seems to require an explicit width on a DIV containing text
			mainobj.togglecontrol()
			$('a[href="' + mainobj.anchorkeyword +'"]').click(function(){
				mainobj.scrollup()
				return false
			})
			$(window).bind('scroll resize', function(e){
				mainobj.togglecontrol()
			})
		})
	}
}

scrolltotop.init()