var baseDir = 'http://www.tom-muller.com/admin/';

//MYSQL************************************************************************************************************************
//************************************************************************************************************************
function getContent(theUrl, target)
{
	var requestUrl = baseDir + theUrl;
	//alert(requestUrl);
	$('#ajax_status').css('display', 'block');
	$.ajax({
		url: requestUrl,
		context: document.body,
		success: function(content){
			if (content.substr(0,6) != 'failed') {	
				//first, seperate the javascript
				var content = content.split("END THE JAVASCRIPT BIT");
			}else{
				alert(content);
			}
			$("#"+target).html(content[1]);
			//$("#"+target).hide();
			$("#"+target).slideDown("fast");
			//run JS from content
			eval(content[0]);
			$('#ajax_status').css('display', 'none');
		},
		statusCode: {
			404: function() {
				alert('page not found');
			}
		}
	});
}

//SAVE A DROPDOWN SELECTION TO A TABLE***************
function saveSelect(theId, theTable, theColumn, theSelect){
	if(theTable == 'carts'){
		//then we on the cart page and need to disable the proceed button
		document.getElementById('proceed_to_pp').disabled = true;
	}
	var theSelectIndex = document.getElementById(theSelect).options.selectedIndex;
	var theValue = document.getElementById(theSelect).options[theSelectIndex].value;
	var requestUrl = baseDir + "req/set_val.php?table=" + theTable + "&column=" + theColumn + "&row_id=" + theId + "&value=" + escape(theValue);
	$.ajax({
		url: requestUrl,
		context: document.body,
		success: function(content){
			if (content.substr(0,6) != 'failed') {	
				//first, seperate the javascript
				var content = content.split("END THE JAVASCRIPT BIT");
			}else{
				alert(content);
			}
			//run JS from content
			eval(content[0]);
		},
		statusCode: {
			404: function() {
				alert('page not found');
			}
		}
	});
}

function toggleCheckbox(theId, theTable, theColumn){
	var requestUrl = baseDir + "req/toggle_checkbox.php?id=" +theId+"&table="+theTable+"&column="+theColumn;
	//alert(requestUrl);
	$.ajax({
		url: requestUrl,
		context: document.body,
		success: function(content){
			if (content.substr(0,6) != 'failed') {	
				//first, seperate the javascript
				var content = content.split("END THE JAVASCRIPT BIT");
			}else{
				alert(content);
			}
		}
	});
}

//INSERT CONTENT INTO A TEXTAREA***************
//************************************************************************************************************************
function insertImage(insertInto){
	imageIndex = document.getElementById('image_select').options.selectedIndex;
	formatIndex = document.getElementById('image_format').options.selectedIndex;
	if(formatIndex == 0){
		alert('Please select an image format before inserting an image.');
	}else{
		if(imageIndex != 0){
		var imageCode = '[' + document.getElementById('image_format').options[formatIndex].value + ' ' +document.getElementById('image_select').options[imageIndex].value;
		//insertAtCaret(insertInto, imageCode);
		insertTag(insertInto, imageCode, '');
		}
	}
	document.getElementById('image_select').options.selectedIndex=0;
}
function insertSelectOption(insertInto, insertFrom){
	fileIndex = document.getElementById(insertFrom).options.selectedIndex;
	if(fileIndex != 0){
		//insertAtCaret(insertInto, document.getElementById(insertFrom).options[fileIndex].value)
		insertTag(insertInto, document.getElementById(insertFrom).options[fileIndex].value, '')
	}
	document.getElementById(insertFrom).options.selectedIndex=0;
}

function insertTag(el,tagStart, tagEnd) {
	el = document.getElementsByClassName(el)[0].elements[0];
  	if (el.setSelectionRange) {
 		var caretPos = (el.value.substring(0,el.selectionStart) + tagStart + el.value.substring(el.selectionStart,el.selectionEnd) + tagEnd).length;
 		el.value = el.value.substring(0,el.selectionStart) + tagStart + el.value.substring(el.selectionStart,el.selectionEnd) + tagEnd + el.value.substring(el.selectionEnd,el.value.length);
	  	setCaretTo(el, caretPos);
  	}
  	else {
  		// IE code here...
  		var selectedText = document.selection.createRange().text; 
         
        if (selectedText != "") { 
            var newText = tagStart + selectedText + tagEnd; 
            document.selection.createRange().text = newText; 
        } 
  	}
}

function setCaretTo(obj, pos) {
	if(obj.createTextRange) {
		var range = obj.createTextRange();
		range.move('character', pos);
		range.select();
	} else if(obj.selectionStart) {
		obj.focus();
		obj.setSelectionRange(pos, pos);
	}
}

//MISCELANY************************************************************************************************************************
//************************************************************************************************************************
//KILLS EVENT BUBBLING
function killBubbles(e)
{
	if (!e) var e = window.event;
	e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();
}

//ANIMATION************************************************************************************************************************
//************************************************************************************************************************
//USE JQUERY To APPEAR/DISAPPEAR AN ELLEMENT
function toggleInOut(target){
	if($('#'+target).css('display') == 'none')
	{
		$("#"+target).slideDown("fast");
	}else{
		$("#"+target).slideUp("fast");
	}
}
//RESIZE DIV********************
//GET COMPUTED STYLE OF AN ELEMENT
function getStyle(oElm, strCssRule)
{
	oElm = document.getElementById(oElm);
	var strValue = '';
	if(document.defaultView && document.defaultView.getComputedStyle){
		strValue = document.defaultView.getComputedStyle(oElm, '').getPropertyValue(strCssRule);
	}
	else if(oElm.currentStyle){
		strCssRule = strCssRule.replace(/\-(\w)/g, function (strMatch, p1){
			return p1.toUpperCase();
		});
		strValue = oElm.currentStyle[strCssRule];
	}
	return strValue;
}

//RETURN THE WINDOW DIMENSIONS
function windowDim()
{
	if (document.body && document.body.offsetWidth) {
		winW = document.body.offsetWidth;
		winH = document.body.offsetHeight;
	}
	if (document.compatMode=='CSS1Compat' &&
		document.documentElement &&
		document.documentElement.offsetWidth ) {
	 winW = document.documentElement.offsetWidth;
	 winH = document.documentElement.offsetHeight;
	}
	if (window.innerWidth && window.innerHeight) {
	 winW = window.innerWidth;
	 winH = window.innerHeight;
	}
	return [winW, winH];
}

//RESIZE DIV********************
function resizeDiv(imgWidth, imgHeight, target, theImg){
	winD = windowDim();
	winW = winD[0];
	winH = winD[1];
	maxImgWidth = winW - (30);
	maxImgHeight = winH - (120);
	
	if(imgWidth > maxImgWidth)
	{
		var ratio = maxImgWidth / imgWidth;
		newImgWidth = maxImgWidth;
		newImgHeight = Math.round(imgHeight * ratio);
	}
	if(imgHeight > maxImgHeight){
		var ratio = maxImgHeight / imgHeight;
		newImgHeight = maxImgHeight;
		newImgWidth = Math.round(imgWidth * ratio);
	}else{
		newImgWidth = imgWidth;
		newImgHeight = imgHeight;
	}
	//resize the img
	imgTarget = document.getElementById(theImg);
	imgTarget.style.width = newImgWidth + 'px';
	imgTarget.style.height = newImgHeight + 'px';

	//resize the div
		$('#' + target).animate({
			width: (newImgWidth) + 'px',
			height: (newImgHeight + 50) + 'px',
		}, 1000 );
}
