// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

function log( msg, force ) {
	if( console != undefined ) {
		console.log( msg );
	} else if( force ) {
		alert( msg );
	}
}

function addEmptyClassToLayoutContainers() {
	$$('.LayoutContainer').each(function(container){
		container.box_children = 0
		Selector.findChildElements(container, ['.Box']).each(function(box){
			container.box_children++;
		});
		if( container.box_children == 0 ) {
			container.addClassName( 'Empty' );
		}
	});
}

function getInnerDimensions(dim) {
	var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }

  //window.alert( 'Width = ' + myWidth );
  //window.alert( 'Height = ' + myHeight );

	if( dim == undefined ) {
		return [ myWidth, myHeight ];
	} else if( dim == "w" ) {
		myWidth;
	}	else if( dim == "h" ) {
		myHeight;
	}
}
function getScrollXY(dim) {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
	if( dim == undefined ) {
		return [ scrOfX, scrOfY ];
	} else if( dim == "x" ) {
		scrOfX;
	}	else if( dim == "y" ) {
		scrOfY;
	}
}

function showCentered(element, just_center) {
	var d = getInnerDimensions(); 
	var s = getScrollXY(); 
	setTimeout( function() { 
		e = [$(element).getDimensions().width, $(element).getDimensions().height]; 
		x = (d[0]/2)-(e[0]/2) + s[0]; 
		y = ((d[1]/2)-(e[1]/2) + s[1])-20;
		
		if( y < 46 ) { y = 46; }
		if( x < 0 ) {	x = 0; }
		
		/*console.log( 'windows is: ' + d[0] + 'x' + d[1] ); 
		console.log( 'element is: ' + e[0] + 'x' + e[1] ); 
		console.log( 'scroll offset is: ' + s[0] + ',' + s[1] ); 
		console.log( 'centering at: ' + x + ', ' + y );*/
		
		$(element).setStyle({left:x + 'px'});
		$(element).setStyle({top:y + 'px'});
		if( just_center == undefined || just_center == false ) {
			Effect.toggle(element,'appear');
			//$(element).appear(); 
		}
	}, 100);
}

function clearContainers() {
	$$('.DropArea').each(function(container){
		container.innerHTML = "";
	});
}

function makeAjaxRequest( url, http_method, auth_token ) {
	new Ajax.Request(
		url, 
		{
			asynchronous:true, 
			evalScripts:true, 
			method:http_method, 
			onComplete:function(request){$('global_loading_container').hide()}, 
			onFailure:function(request){alert('There was an error, please try again.')}, 
			parameters:'authenticity_token=' + encodeURIComponent(auth_token)
		}
	);
}

function findPos(obj) {
	var curleft = curtop = 0;
	
	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);		
		return [curleft,curtop];
	}
}

function regFind( str, pattern ) {
	
	log( "str: " + str + ", pattern " + pattern );
	
	var re = new RegExp(pattern);
	return re.test(str);
}
