var documentReady = false;

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

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 blinkSelected(id) {
	$('#'+id).animate({ backgroundColor: "#ffffeb" }, '1000');
	$('#'+id).animate({ backgroundColor: "white" }, '2000');
}

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

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

function makeBookmarkUrl(tabName){
	location.hash = tabName.toLowerCase() + 'tab';	
}

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 = document.documentElement.clientHeight - 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);
	}
});

function getHeatClick(){
	var section;
	var pageName;
	var path = (window.location.pathname).split("/");

	for (var i=path.length-1; i >= 0; i--) {
		if ( (path[i] != "") && (pageName == undefined) ) {
			pageName = path[i];
			continue;
		}
	
		if ( (path[i] != "") && (section == undefined) ) {
			section = path[i];
			break;
		}
	}
	
	if (section == undefined) {
		section = pageName;
		pageName = 'index'
	}
	if (pageName == undefined){
		pageName = 'index';
	}
	if ( (section == 'home') || (section == undefined) ) {
		section = 'MAIN';
	}
	var result = {};
	result.section = section;
	result.pageName = pageName;
	return result;
}
function initUpgradesClick(){
	$('#upgrades-link').live('click', function(){
		blinkSelected ('discounts');
	});
}

//color animation
(function(d){d.each(['backgroundColor','borderBottomColor','borderLeftColor','borderRightColor','borderTopColor','color','outlineColor'],function(b,c){d.fx.step[c]=function(a){if(a.state==0){a.start=f(a.elem,c);a.end=e(a.end)}a.elem.style[c]="rgb("+[Math.max(Math.min(parseInt((a.pos*(a.end[0]-a.start[0]))+a.start[0]),255),0),Math.max(Math.min(parseInt((a.pos*(a.end[1]-a.start[1]))+a.start[1]),255),0),Math.max(Math.min(parseInt((a.pos*(a.end[2]-a.start[2]))+a.start[2]),255),0)].join(",")+")"}});function e(a){var b;if(a&&a.constructor==Array&&a.length==3)return a;if(b=/rgb\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*\)/.exec(a))return[parseInt(b[1]),parseInt(b[2]),parseInt(b[3])];if(b=/rgb\(\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*\)/.exec(a))return[parseFloat(b[1])*2.55,parseFloat(b[2])*2.55,parseFloat(b[3])*2.55];if(b=/#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})/.exec(a))return[parseInt(b[1],16),parseInt(b[2],16),parseInt(b[3],16)];if(b=/#([a-fA-F0-9])([a-fA-F0-9])([a-fA-F0-9])/.exec(a))return[parseInt(b[1]+b[1],16),parseInt(b[2]+b[2],16),parseInt(b[3]+b[3],16)];return g[d.trim(a).toLowerCase()]}function f(a,b){var c;do{c=d.curCSS(a,b);if(c!=''&&c!='transparent'||d.nodeName(a,"body"))break;b="backgroundColor"}while(a=a.parentNode);return e(c)};var g={aqua:[0,255,255],azure:[240,255,255],beige:[245,245,220],black:[0,0,0],blue:[0,0,255],brown:[165,42,42],cyan:[0,255,255],darkblue:[0,0,139],darkcyan:[0,139,139],darkgrey:[169,169,169],darkgreen:[0,100,0],darkkhaki:[189,183,107],darkmagenta:[139,0,139],darkolivegreen:[85,107,47],darkorange:[255,140,0],darkorchid:[153,50,204],darkred:[139,0,0],darksalmon:[233,150,122],darkviolet:[148,0,211],fuchsia:[255,0,255],gold:[255,215,0],green:[0,128,0],indigo:[75,0,130],khaki:[240,230,140],lightblue:[173,216,230],lightcyan:[224,255,255],lightgreen:[144,238,144],lightgrey:[211,211,211],lightpink:[255,182,193],lightyellow:[255,255,224],lime:[0,255,0],magenta:[255,0,255],maroon:[128,0,0],navy:[0,0,128],olive:[128,128,0],orange:[255,165,0],pink:[255,192,203],purple:[128,0,128],violet:[128,0,128],red:[255,0,0],silver:[192,192,192],white:[255,255,255],yellow:[255,255,0]}})(jQuery);

