var documentReady = false;

jQuery(document).ready( function($) {
	documentReady = true;
});

function searchField(fieldId) {
	var searchField = document.getElementById(fieldId);
	if (!searchField)
		return false;
	var inputPlaceholder = searchField.getAttribute("placeholder");

	if (searchField.value.length == 0 || searchField.value == inputPlaceholder) {
		searchField.className = "empty";
		searchField.value = inputPlaceholder;
	}

	searchField.onfocus = function() {
		if (this.className == "empty") {
			this.className = "";
			this.value = "";
		}
	};
	searchField.onblur = function() {
		if (this.value.length == 0) {
			this.className = "empty";
			this.value = inputPlaceholder;
		}
	};

}
function sSearch() {
	searchField("search_field");
	searchField("support_search");

}
window.onload = sSearch;

function clearSelection(parentId) {
	var allChildren = document.getElementById(parentId).childNodes;
	var count = 0;
	for ( var i = 0; i < allChildren.length; i++) {
		if (allChildren[i].tagName != null
				&& allChildren[i].className != "title") {
			allChildren[i].className = "";
		}
	}
}

function setSelected(id) {
	document.getElementById(id).className = "active";
	document.getElementById(id).focus;
}

function changeSelection(parentId, id) {
	clearSelection(parentId);
	setSelected(id);

}

function clickButton(id) {
	document.getElementById(id).click();
}

function changeVisibility(parentId, id) {
	setAllInvisible(parentId);
	setVisible(id);
}

function setVisible(id) {
	document.getElementById(id).className = "visible";
}

function setAllInvisible(parentId) {
	var allChildren = document.getElementById(parentId).childNodes;
	var count = 0;
	for ( var i = 0; i < allChildren.length; i++) {
		if (allChildren[i].tagName != null) {
			allChildren[i].className = "invisible";
		}
	}
}

function setInputTextAndClick(inputId, value, buttonId) {
	document.getElementById(inputId).value = value;
	document.getElementById(buttonId).click();
}

function setInputText(inputId, value) {
	document.getElementById(inputId).value = value;
}

function changeDescriptionTab(tabId) {
	changeSelection('description_menu', tabId + '_li');
	changeVisibility('description_content', tabId);
}

function show(id) {
	document.getElementById(id).style.display = 'block';
}

function hideElement(element) {
	element.style.display = 'none';
}

function getXcenterCoord() {
	var winW;
	if (window.opera && window.getSelection) {
		return document.body.clientWidth / 2 - 263;
	}
	if (parseInt(navigator.appVersion) > 3) {
		if (navigator.appName == "Netscape") {
			winW = window.innerWidth - 16;
		}
		if (navigator.appName.indexOf("Microsoft") != -1) {
			if (window.XMLHttpRequest) {
				winW = document.body.offsetWidth - 20;
			} else {
				winW = document.documentElement.clientWidth;
			}

		}
	}
	return (winW / 2) - 265;
}

function getYcenterCoord() {
	var winH;
	if (window.opera && window.getSelection) {
		return screen.height / 2 - 300 + window.pageYOffset;
	}
	if (parseInt(navigator.appVersion) > 3) {
		if (navigator.appName == "Netscape") {
			winH = window.innerHeight - 16;
		}
		if (navigator.appName.indexOf("Microsoft") != -1) {
			if (window.XMLHttpRequest) {
				winH = document.body.offsetHeight - 20;
			} else {
				winH = document.documentElement.clientHeight;
			}
		}
	}
	return (winH / 2) - 300 + window.pageYOffset;
}

function keyPressHandlerOffer(e) {
	var kC = (window.event) ? event.keyCode : e.keyCode;
	var Esc = (window.event) ? 27 : e.DOM_VK_ESCAPE
	if (kC == Esc) {
		e.returnValue = false;
		document.getElementById('sourceCodeLicense:licensePopup').hide();
		return false;
	}
	return true;
}

/*
 * This script is listening if user clicks link with rel='facebox' attribute and
 * opens facebox popup layer
 */
jQuery(document).ready( function($) {
	initPopup();
})

function initPopup() {
	$('a[rel=facebox]').facebox( {
		loading_image :'/images/facebox/loading.gif',
		close_image :'/images/facebox/closelabel.gif'
	});
}

/*
 * Function which finds link with cirrent url in href attribute and clicks on it
 */
function findAndClickLink(url) {
	if (url != '' && window.location.hash.length > 1) {
		var obj = $('a[href=' + url + ']');
		if (obj != null)
			obj.click();
	}
}

/*
 * Hash listener script. It checks hash and if it contains '#print' script opens
 * print dialog for the page
 */
jQuery(document).ready( function($) {
	if (window.location.hash == '#print') {
		window.print();
	}
})



function removeClassName(objElement, strClass) {
	if (objElement.className) {
		var arrList = objElement.className.split(' ');
		var strClassUpper = strClass.toUpperCase();
		for ( var i = 0; i < arrList.length; i++) {
			if (arrList[i].toUpperCase() == strClassUpper) {
				arrList.splice(i, 1);
				i--;
			}
		}
		objElement.className = arrList.join(' ');
	}
}

var MIN_HEIGHT = 100;
var VERTICAL_PADDING = 75;
function openInPopup(id) {
	var height = $(window).height() - 2 * VERTICAL_PADDING;
	if (height < MIN_HEIGHT) {
		height = MIN_HEIGHT;
	}
	openInPopupWithParams(id, 900, height, false, true)
}

function openInPopupWithParams(id, width, height, showScrollX, showScrollY) {
	var content = document.getElementById(id).innerHTML;
	var overflowX = showScrollX ? 'scroll' : 'auto';
	var overflowY = showScrollY ? 'scroll' : 'auto';
	var widthText = width ? "width:" + width + "px;" : '';
	var heightText = height ? "height:" + height + "px;" : '';
	content = "<div style='" + heightText + widthText + "overflow-x:"
			+ overflowX + ";" + "overflow-y:" + overflowY + ";" + "'><div>"
			+ content + "</div></div>";

	jQuery.facebox(content);
}

$(document).ready(function () {

	$(document).keypress(function(event) {
		if ((event.keyCode == 13 || event.keyCode == 10) && event.ctrlKey) {
			var text = getSelectedText();
			if (text == '') {
				return;
			}
			
			var message = "Send the report? \n You will remain on the same page. No page reload needed.";
		    
			if (confirm(message)) {
		    	sendBugReport(text);
		    }
		}
	});
});

function getSelectedText() {
	var txt = '';
    
	if (window.getSelection){
       return window.getSelection();
    } else if (document.getSelection) {
    	return document.getSelection();
    } else if (document.selection) {
       return document.selection.createRange().text;
    }
	
   else return;
}

function sendBugReport(text) {
	document.getElementById("footerSubview:errorReport:errorText").value = text;
	document.getElementById("footerSubview:errorReport:pageId").value = document.location.href;
	
	var sendRequest = document.getElementById('footerSubview:errorReport:sendBugReport');	        	    	
	sendRequest.run();
}

$(document).ready(function(){
	if(window.location.hash!="" && window.location.hash!="#" && $(window.location.hash).length>0){
			window.scroll(0, $(window.location.hash).offset().top);
	}
});