var pageDropBoxes = Array();
var openBox = null;
function registerDropBox(rootName) {
	pageDropBoxes[pageDropBoxes.length] = rootName;
}


function createRequestObject(){
	var requestObject; 
	var browser = navigator.appName; 
	if(browser == "Microsoft Internet Explorer"){
		requestObject = new ActiveXObject("Microsoft.XMLHTTP");
	}else{
		requestObject = new XMLHttpRequest();
	}
	return requestObject; 
}

var timerId = null;
var currentId = null;
var currentParent = null;
insideAccount = false;
insideAccountButton = false;

function flipDownload(downloadId) {
	testDisplay = document.getElementById(downloadId + "_desc").style.display;
	
	if (testDisplay == "block") {
		document.getElementById(downloadId + "_desc").style.display = 'none';
		document.getElementById(downloadId + "_exp").className = 'button_expand';		
	}
	else {
		document.getElementById(downloadId + "_desc").style.display = 'block';
		document.getElementById(downloadId + "_exp").className = 'button_expand button_expand_off';
	}
	
	
}

function enableAdult() {
	localHttp= createRequestObject();

	localHttp.open('get', '/remoteHandler.php?action=enableAdult');
	localHttp.onreadystatechange = function(foo) {
		if (localHttp.readyState == 4) {	
				rez = localHttp.responseText;				
				location.href = rez;
		}
	}
	localHttp.send(null);
}



function dropHoverDynamic(rootName, selectedNumber, classOff, classOn) {
	totalNumber = document.getElementById(rootName + 'Total').value;

	for (i = 0 ; i<totalNumber; i++) {

		
		
		workingElement = rootName + i;

		if (i == selectedNumber) {
			document.getElementById(workingElement).className = classOn;
		}
		else {
			document.getElementById(workingElement).className = classOff;
		}
	}
	
}


function dropHover(targetId) {
	if (targetId == 'cnt_video') {
		document.getElementById('cnt_video').className = 'dropOptionOn';
	}
	else {
		document.getElementById('cnt_video').className = 'dropOptionOff';
	}
	if (targetId == 'cnt_all') {
		document.getElementById('cnt_all').className = 'dropOptionOn';
	}
	else {
		document.getElementById('cnt_all').className = 'dropOptionOff';
	}
	
	if (targetId == 'cnt_music') {
		document.getElementById('cnt_music').className = 'dropOptionOn';
	}
	else {
		document.getElementById('cnt_music').className = 'dropOptionOff';
	}
	if (targetId == 'cnt_images') {
		document.getElementById('cnt_images').className = 'dropOptionOn';
	}
	else {
		document.getElementById('cnt_images').className = 'dropOptionOff';
	}
	if (targetId == 'cnt_music') {
		document.getElementById('cnt_music').className = 'dropOptionOn';
	}
	else {
		document.getElementById('cnt_music').className = 'dropOptionOff';
	}
	
	if (document.getElementById('cnt_adult')) {
		if (targetId == 'cnt_adult') {
			document.getElementById('cnt_adult').className = 'dropOptionOn';
		}
		else {
			document.getElementById('cnt_adult').className = 'dropOptionOff';
		}
	
	}
	
	
}

function expandPreview(previewId, parentId) {
	if (currentId != null) {
		document.getElementById(currentId).style.display = 'none';	
		breakChildren = currentId.split("-");
		selectedImage = parseInt(breakChildren[1]);
		
		
	 	for (j =0 ; j < 4; j++) {
			
	 		if (j == selectedImage) {
				//document.getElementById(previewId).style.display = 'block';
			}
			else {
				workingId = "static" + breakChildren[0].replace("exc", "") + "-" + j;
				document.getElementById(workingId).style.opacity = 1;
				document.getElementById(workingId).style.filter = 'alpha(opacity=100)';
			}
		}
	}
	breakChildren = previewId.split("-");
	selectedImage = parseInt(breakChildren[1]);
	
 	for (i =0 ; i < 4; i++) {
 		if (i == selectedImage) {
			document.getElementById(previewId).style.display = 'block';
		}
		else {
			workingId = "static" + parentId + "-" + i;
			document.getElementById(workingId).style.opacity = .2;
			document.getElementById(workingId).style.filter = 'alpha(opacity=40)';
		}
	}
	document.getElementById(parentId).style.zIndex = 20;
	currentId = previewId;
}
function hidePreview(previewId, parentId) {
	
		document.getElementById(currentId).style.display = 'none';	
		breakChildren = currentId.split("-");
		selectedImage = parseInt(breakChildren[1]);
		
		
	 	for (j =0 ; j < 4; j++) {
			
	 		if (j == selectedImage) {
				//document.getElementById(previewId).style.display = 'block';
			}
			else {
				workingId = "static" + breakChildren[0].replace("exc", "") + "-" + j;
				document.getElementById(workingId).style.opacity = 1;
				document.getElementById(workingId).style.filter = 'alpha(opacity=100)';
			}
		}
	
	currentId = null;
	document.getElementById(previewId).style.display = 'none';
	document.getElementById(parentId).style.zIndex = 0;
}

function changeFromName() {
	workingName = document.getElementById('fromName').value;
	document.getElementById('userNameBody').innerHTML = workingName;
	document.getElementById('userNameSubject').innerHTML = workingName;
}

function changeToName() {
	workingName = document.getElementById('toName').value;
	document.getElementById('friendNameBody').innerHTML = workingName;
	
}

function checkMsg() {


	if (document.getElementById('personalMessage').innerHTML == "Type a personal message") {
		document.getElementById('personalMessage').innerHTML = "";
		document.getElementById('personalMessage').style.color = "black";
	}
	
}

function flipButton(buttonId) {
	
	

}

function clearHide() {
	if (timerId) {
		clearTimeout(timerId);
	}
}

	
function is_child_of(parent, child) {
			if( child != null ) {			
				try {
					while( child.parentNode ) {
						if( (child = child.parentNode) == parent ) {
							return true;
						}
					}
				}
				catch (error) {
					return false;
				}
				
					
			}
			return false;
		}
		
		function fixOnMouseOut(element, event, rootName) {
			var current_mouse_target = null;
			if( event.toElement ) {				
				current_mouse_target 			 = event.toElement;
			} else if( event.relatedTarget ) {				
				current_mouse_target 			 = event.relatedTarget;
			}
			if( !is_child_of(element, current_mouse_target) && element != current_mouse_target ) {

				//hideMediaDropBox();

				timerId = setTimeout('hideDynamicDropBox(\''+rootName+'\')', 500);
				//timerId = setTimeout(timeoutFunction, 500);
			}
		}

function fixOnMouseOutStroke(element, event, rootName) {
			var current_mouse_target = null;
			if( event.toElement ) {				
				current_mouse_target 			 = event.toElement;
			} else if( event.relatedTarget ) {				
				current_mouse_target 			 = event.relatedTarget;
			}
			if( !is_child_of(element, current_mouse_target) && element != current_mouse_target ) {

				//hideMediaDropBox();

				timerId = setTimeout('hideDynamicDropBoxStroke(\''+rootName+'\')', 500);
				//timerId = setTimeout(timeoutFunction, 500);
			}
		}
function dropExit(mEvent) {
	targetId = null;
  // Internet Explorer
  if (mEvent.srcElement)
  {
    targetId = mEvent.srcElement.id;
  }
  // Netscape and Firefox
  else if (mEvent.target)
  {
    targetId = mEvent.target.id;
  }
  alert(targetId);
  hide = false;
	if (targetId == null) {
		hide = true;
	}
	else if (targetId.indexOf('drop') == -1 && targetId.indexOf('cnt') == -1) {
		hide = true;
	}
	if (hide) {
		hideMediaDropBox();
	}
	

}

function showMediaDropBox() {
	document.getElementById('search_dropHolder').style.display = 'inline';
	document.getElementById('search_rightMediaDrop').className = 'inputBox_rightDrop_on';
	document.getElementById('search_leftMediaDrop').className = 'inputBox_left_closed';
	
}
function flipMediaDropBox() {
		if (self.innerHeight) // all except Explorer
	{
		scnWid = self.innerWidth;
		scnHei = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
		// Explorer 6 Strict Mode
	{
		scnWid = document.documentElement.clientWidth;
		scnHei = document.documentElement.clientHeight;
	}
	else if (document.body) // other Explorers
	{
		scnWid = document.body.clientWidth;
		scnHei = document.body.clientHeight;
	}
	
	if (document.getElementById('search_dropHolder').style.display == 'none') {
		showMediaDropBox();
	}
	else {
		hideMediaDropBox();
	}
	
}

function showAdultPermissions() {
	scroll(0,0);
	document.getElementById('bodyElement').style.overflow = 'hidden';
	document.getElementById('pageFader').style.height = scnHei;
	document.getElementById('pageFader').style.display = 'block';
	document.getElementById('adultWarningBox').style.display = 'block';
}
function adultDisagree() {
	document.getElementById('adultWarningBox').style.display = 'none';
	document.getElementById('bodyElement').style.overflow = 'scroll';
	document.getElementById('pageFader').style.display = 'none';

}

function hideMediaDropBox() {
	document.getElementById('dropHolder').style.display = 'none';
	document.getElementById('rightMediaDrop').className = 'inputBox_rightDrop_off';
	document.getElementById('leftMediaDrop').className = 'inputBox_left';
}


function hideDynamicDropBox(root) {

	document.getElementById(root + '_dropHolder').style.display = 'none';
	document.getElementById(root + '_rightMediaDrop').className = 'inputBox_rightDrop_off';
	document.getElementById(root + '_leftMediaDrop').className = 'inputBox_left';
}

function showDynamicDropBox(root) {
	document.getElementById(root + '_dropHolder').style.display = 'inline';
	document.getElementById(root + '_rightMediaDrop').className = 'inputBox_rightDrop_on';
	document.getElementById(root + '_leftMediaDrop').className = 'inputBox_left_closed';
	
}

function hideDynamicDropBoxStroke(root) {
	document.getElementById(root + '_dropHolder').style.display = 'none';
	document.getElementById(root + '_rightMediaDrop').className = 'inputBoxStroke_rightDrop_off';
	document.getElementById(root + '_leftMediaDrop').className = 'inputBoxStroke_left';
}

function showDynamicDropBoxStroke(root) {
	document.getElementById(root + '_dropHolder').style.display = 'inline';
	document.getElementById(root + '_rightMediaDrop').className = 'inputBoxStroke_rightDrop_on';
	document.getElementById(root + '_leftMediaDrop').className = 'inputBoxStroke_left_closed';
	
}

function flipDynamicDropBox(root) {
	
		if (self.innerHeight) // all except Explorer
	{
		scnWid = self.innerWidth;
		scnHei = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
		// Explorer 6 Strict Mode
	{
		scnWid = document.documentElement.clientWidth;
		scnHei = document.documentElement.clientHeight;
	}
	else if (document.body) // other Explorers
	{
		scnWid = document.body.clientWidth;
		scnHei = document.body.clientHeight;
	}
	
	if (document.getElementById(root +'_dropHolder').style.display == 'none') {
		for (i = 0; i < pageDropBoxes.length; i++) {
			
			checkRoot = pageDropBoxes[i];

			if (checkRoot != root) {
				hideDynamicDropBox(checkRoot);
			}
		}
		
		showDynamicDropBox(root);
	}
	else {

		hideDynamicDropBox(root);
	}
	
}

function flipDynamicDropBoxStroke(root) {
	
		if (self.innerHeight) // all except Explorer
	{
		scnWid = self.innerWidth;
		scnHei = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
		// Explorer 6 Strict Mode
	{
		scnWid = document.documentElement.clientWidth;
		scnHei = document.documentElement.clientHeight;
	}
	else if (document.body) // other Explorers
	{
		scnWid = document.body.clientWidth;
		scnHei = document.body.clientHeight;
	}
	
	if (document.getElementById(root +'_dropHolder').style.display == 'none') {
		/**
		for (i = 0; i < pageDropBoxes.length; i++) {
			
			checkRoot = pageDropBoxes[i];

			if (checkRoot != root) {
				hideDynamicDropBoxStroke(checkRoot);
			}
		}**/
		if (openBox && openBox != root) {
			hideDynamicDropBoxStroke(openBox);
		}
		
		showDynamicDropBoxStroke(root);
		openBox = root;
	}
	else {

		hideDynamicDropBoxStroke(root);
	}
	
}

function hideAccountMenuInside(picked) {
	insideAccount = false;
	if (!insideAccountButton) {
		document.getElementById('accountMenu').style.display = 'none';
		if (picked == 0) {
			document.getElementById('accountButton').className = 'button_account';
		}
	}
}

function showAccountMenuInside() {
	document.getElementById('accountMenu').style.display = 'block';
	document.getElementById('accountButton').className = 'button_account button_account_on';
	insideAccount = true;
}


function hideAccountMenu(picked) {

	insideAccountButton = false;
	if (!insideAccount) {
		document.getElementById('accountMenu').style.display = 'none';
		if (picked == 0) {
			document.getElementById('accountButton').className = 'button_account';
		}
	}
}

function showAccountMenu() {
	document.getElementById('accountMenu').style.display = 'block';
	document.getElementById('accountButton').className = 'button_account button_account_on';
	insideAccountButton = true;
}


function selectDropOption(labelRoot, selectedId, selectedValue) {
	
	hideDynamicDropBox(labelRoot);
	document.getElementById(labelRoot + '_Id').value = selectedId;
	document.getElementById(labelRoot + '_Label').innerHTML = selectedValue;
	
}
	
function selectDropOptionStroke(labelRoot, selectedId, selectedValue) {
	
	hideDynamicDropBoxStroke(labelRoot);
	document.getElementById(labelRoot + '_Id').value = selectedId;
	document.getElementById(labelRoot + '_Label').innerHTML = selectedValue;
	
}
	
function php_serialize(a) 
	{
    var a_php = "";
    var total = 0;
    for (var key in a)
    {
    	if (a.hasOwnProperty(key)) {

        ++ total;
        a_php = a_php + "s:" +
                String(key).length + ":\"" + String(key) + "\";s:" +
                String(a[key]).length + ":\"" + String(a[key]) + "\";";
              }
    }
    a_php = "a:" + total + ":{" + a_php + "}";
    return a_php;
}

function changeSort(sortType, sortDir) {
	/**
	if (sortType == 'title') {
		document.getElementById('sortTitle').style.display = 'block';
		document.getElementById('sortAge').style.display = 'none';
		document.getElementById('sortSize').style.display = 'none';
	}
	else if (sortType =="age") {
		document.getElementById('sortAge').style.display = 'block';
		document.getElementById('sortTitle').style.display = 'none';
		document.getElementById('sortSize').style.display = 'none';
	}
	else if (sortType == "size") {
		document.getElementById('sortSize').style.display = 'block';
		document.getElementById('sortAge').style.display = 'none';
		document.getElementById('sortTitle').style.display = 'none';
	}
	**/
	localHttp= createRequestObject();

	localHttp.open('get', '/remoteHandler.php?action=doSort&sortType=' + sortType + "&sortDir=" + sortDir);
	localHttp.onreadystatechange = function(foo) {
		if (localHttp.readyState == 4) {	
				rez = localHttp.responseText;				
				window.location.reload();
		}
	}
	localHttp.send(null);
	
}
