var PopupWindow;
var user_list = '';

function initKFM(){
	var els=document.getElementsByTagName('*');
	var reg=/(^| )kfm($| )/;
	
	for(i in els){
		
		var el=els[i];
		
		if(reg.test(el.className)){
			el.onclick=function(){
				window.SetUrl=(function(id){
					return function(value){
						value=value.replace(/[a-z]*:\/\/[^\/]*/,'');
						document.getElementById(id).value=value;
					}
				})(this.id);
				
				var kfm_url='/global/include/kfm/';
				window.open(kfm_url,'kfm','modal,width=600,height=400');
			}
		}
	}
}

function getHTTPData(url){
	var myRequest = null;

	try {
		if(window.XMLHttpRequest) {
			// Gecko (Firefox, Moz), KHTML (Konqueror, Safari), Opera, Internet Explorer 7
			myRequest = new XMLHttpRequest();
		} else if(window.ActiveXObject) {  
			myRequest = new ActiveXObject("Microsoft.XMLHTTP"); // Internet Explorer 4,5,5.5,6,7
		} 
	} catch(e) {
		return 'Error in detecting browser capabilities.';
	}
	
	try {
		myRequest.open("GET", url, false);
		myRequest.send(null);
	}
	catch(e) {
		return 'Error in XMLHttpRequest.';
	}
	return myRequest.responseText;
}

function selectImage(imageID, popup){

	if(imageID && (el = document.getElementById('image_checkbox_' + imageID))){
		myURL = '/global/xmlrequest/select_image.php?imageID='+imageID+'&selected='+el.checked;
		myResponse = getHTTPData(myURL);
		
		if(popup){
			if(window.opener && !window.opener.closed && (el = window.opener.document.getElementById('image_checkbox_' + imageID))){
				
				selectedDiv = window.opener.document.getElementById('imageDiv_' + imageID);
				
				if(myResponse ==1){
					el.checked= true;
					
					if(selectedDiv){
						selectedDiv.className= "thumbnailDivSelected";
					}
				}
				else{
					el.checked= false;
					
					if(selectedDiv){
						selectedDiv.className= "thumbnailDiv";
					}

				}
			}
		}
		else{
			if(selectedDiv = document.getElementById('imageDiv_' + imageID)){
				if(myResponse ==1){
					selectedDiv.className= "thumbnailDivSelected";
				}
				else{
					selectedDiv.className= "thumbnailDiv";
				}
			}

			if(PopupWindow && (el = PopupWindow.document.getElementById('image_checkbox_' + imageID))){
				if(myResponse ==1){
					el.checked= true;
				}
				else{
					el.checked= false;
				}
			}
		}	
	}

}

function updateCounts(galleryID, popup){

	myURL = '/global/xmlrequest/count_selections.php?galleryID='+galleryID;
	myCount = getHTTPData(myURL);
	
	selectionArray = myCount.split(":");
	if(selectionArray.length==2){

		if(popup){
			if(window.opener && !window.opener.closed){
				if(el = window.opener.document.getElementById('mySelectionCount')){
					el.innerHTML = parseInt(selectionArray[0]);
				}

				if(el = window.opener.document.getElementById('myCommentCount')){
					el.innerHTML = parseInt(selectionArray[1]);
				}
				
				if(el = window.opener.document.getElementById('summaryLink')){
					if(parseInt(selectionArray[0]) >0){
						el.style.display="inline";	
						window.opener.$("div#gallerySubmit").show();
					}
					else{
						el.style.display="none";	
						window.opener.$("div#gallerySubmit").hide();
					}
				}
			}
		}
		else{
			if(el = document.getElementById('mySelectionCount')){
				el.innerHTML = parseInt(selectionArray[0]);
			}

			if(el = document.getElementById('myCommentCount')){
				el.innerHTML = parseInt(selectionArray[1]);
			}
	
			if(el = document.getElementById('summaryLink')){
				if(parseInt(selectionArray[0]) >0){
					el.style.display="inline";	
					$("div#gallerySubmit").show();
				}
				else{
					el.style.display="none";	
					$("div#gallerySubmit").hide();
				}
			}

		}
	}

}
$(function(){

	$("#usernameAutocomplete").autocomplete(user_list, {
		minChars: 0,
		width: 310,
		max: 200,
		cacheLength: 100,
		matchContains: true,
		autoFill: false,
		formatItem: function(row, i, max) {
			return row.name.replace(new RegExp("(^, )"), "");
		},
		formatMatch: function(row, i, max) {
			return row.name.replace(new RegExp("(^, )"), "");
		},
		formatResult: function(row) {
			return row.name.replace(new RegExp("(^, )"), "");
		}

	}).result(function(event, data, formatted) {
		if($('#clientContainer input[name="newClientArray['+data.login_id+']"]').length==0){
	 		$("#clientContainer").append('<div><input type="button" class="removeUser" value="x" /> '+formatted+'<input type="hidden" value="'+data.login_id+'" name="newClientArray['+data.login_id+']" /></div>');
		}
		$("#usernameAutocomplete").val('');
	});

	$('.removeUser').livequery('click', function(){
		$(this).parent().slideUp('normal', function() { $(this).remove(); });
	});
	$("#userSearchAutocomplete").autocomplete(user_list, {
		minChars: 0,
		width: 310,
		max: 200,
		cacheLength: 100,
		matchContains: true,
		autoFill: false,
		formatItem: function(row, i, max) {
			return row.name.replace(new RegExp("(^, )"), "");
		},
		formatMatch: function(row, i, max) {
			return row.name.replace(new RegExp("(^, )"), "");
		},
		formatResult: function(row) {
			return row.name.replace(new RegExp("(^, )"), "");
		}

	}).result(function(event, data, formatted) {
		$('#userSearchLoginID').val(data.login_id);
	});

	$('a.rating_link').click(function(){
		rating = parseInt($(this).attr('ref'));
		imageID = parseInt($(this).parent().attr('ref'));
		$(this).parent().removeClass().addClass('thumbnailRating_'+rating).addClass('ratingContainer');
		if(window.opener && !window.opener.closed){
			window.opener.$('div.ratingContainer[ref='+imageID+']').removeClass().addClass('thumbnailRating_'+rating).addClass('ratingContainer'); 
		}
		else if(PopupWindow){
			PopupWindow.$('div.ratingContainer[ref='+imageID+']').removeClass().addClass('thumbnailRating_'+rating).addClass('ratingContainer'); 
		}
		
		$.post("/global/xmlrequest/rate_image.php", {'rating' : rating, 'imageID': imageID});
		return false;
	});

	$('input#filterSubmit').click(function(){
		if(PopupWindow){
			PopupWindow.close();
		}
	});

	$('div#passwordShow').click(function(){
		$('div#hiddenPassword').toggle();
		return false;
	});

	$('a#galleryHelpToggle').click(function(){
		$('div#galleryHelpContents').toggle('fast');
		return false;
	});
});
	

