//var	_APP_URL = 'http://www.smartbuy.ro';	//moved here from JSInit so we can get rid of an extra call
//var	_APP_URL = 'http://www.smartbuy.ro';	//moved to layout from here so it can be set from Application/Config/config.php


function filterSelectData(trg, counter, keyword) {
 	$('#' + counter).html('Filtering ...');
 	var total = 0;
 	var have_selected = 0;
	keyword = keyword.toLowerCase();
 	var elem = document.getElementById(trg);
 	for (var i = 0; i < elem.options.length; i++) {
 		var val_html = elem.options[i].innerHTML.toLowerCase();
 		if (val_html.indexOf(keyword) >= 0) {
 			elem.options[i].style.display = 'block';
 			if (!have_selected) {
 				elem.options[i].selected = true;
 				have_selected = 1;
 			}
 			total++;
 		} else {
 			elem.options[i].style.display = 'none';
 		}
 	}

 	$('#' + counter).html('(' + total + ')');
}


//processes a table, setting class and hover for normal/odd lines and adding click select/deselect for the rows
function processTable(table_id, allow_multiple) {
	
	var rowLock = false;
	
	$('#'+ table_id).find('tr:odd').addClass('oddLine');
	//$('#'+ table_id).find('input[type=checkbox]').css('z-index', 99);
	
	$('#'+ table_id).find('input[type=checkbox]').each(function(){
		if($(this).attr('checked')) {
			$(this).parent().parent().addClass('selectedRow');
		}
		else {
			$(this).parent().parent().removeClass('selectedRow');
		}
	});
	
	$('#'+ table_id).find('input[type=checkbox]').bind('click', function() {
		rowLock = true;
		if($(this).parent().parent().hasClass('selectedRow')) {
			$(this).parent().parent().removeClass('selectedRow');
			$(this).attr('checked', '');
		}
		else {
			$(this).parent().parent().addClass('selectedRow');
			$(this).attr('checked', 'checked');
		}
	});
	
			
	$('#'+ table_id).find('tr').each(function() {
		
		$(this).bind('mouseover', function() {
			$(this).addClass('hoverLine');
		});
		$(this).bind('mouseout', function() {
			$(this).removeClass('hoverLine');
		});
		
		$(this).bind('click', function() {
			if(!rowLock) {
				if(!$(this).hasClass('selectedRow')) { // select current row
					if(!allow_multiple) {
						$('.selectedRow input[type=checkbox]').attr('checked', '');
						$('.selectedRow').removeClass('selectedRow');
					}
					
					$(this).addClass('selectedRow');
					$(this).find('input[type=checkbox]').attr('checked', 'checked');
					//$('.selectedRow input[type=checkbox]').attr('checked', 'checked');
				}
				else { // deselect current row
					if(!allow_multiple) {
						$('.selectedRow input[type=checkbox]').attr('checked', '');
						$('.selectedRow').removeClass('selectedRow');
					}
					$(this).find('input[type=checkbox]').attr('checked', '');
					$(this).removeClass('selectedRow');
				}
			}
			rowLock = false;
		});
	});
	

}

//toggles the checked/unchecked status for a list of checkboxes sharing the same class
function toggleCheckboxes(box_class, box_checked) {
	$('.' + box_class).each(function() {
		if(box_checked) {
			$(this).parent().parent().addClass('selectedRow');
		}
		else {
			$(this).parent().parent().removeClass('selectedRow');
		}
		this.checked = box_checked;
	})
}

function getRowID(container, elem) {
	var my_counter = 0;
	rowids = [];
	var my_counter = 0;
	$(container).find(elem + '.selectedRow').each(function() {
		rowids[my_counter] = this.id.substring(3);
		my_counter++;
	});
	
	if (my_counter <= 0) {
		return false;
	} else if(my_counter == 1) {
		return rowids[0];
	} else {
		return rowids;
	}
}

function createAjaxLoader() {
	$('body').append('<div id="ajax_loader"><img src="' + _APP_URL + '/lib/images/decorators/ajax_loader_big.gif" border="0" align="absmiddle"></div>');
}

$(document).ready(function () {
	createAjaxLoader();
	if(typeof(ignore_ajax_loader) == 'undefined') {
		$('#ajax_loader').ajaxStart(function(){
			$('#ajax_loader').show();
		});
		$('#ajax_loader').ajaxStop(function(){
			$('#ajax_loader').hide();
		});
	}
	////$.ifixpng(_APP_URL + 'lib/images/admin/interface/pixel.gif');
	
} );

function createFloatingWindow(title, content) {
	$('DIV.ui-dialog-overlay').remove();
	$('DIV.ui-dialog').remove();
	$('body').append('<div id="floatingWindow" title="' + title + '">' + content + '</div>');
	//$('#floatingWindow').dialog({width:'600px', height: 'auto'});	//, 'modal':true, overlay: {opacity: 0.5}
	$('#floatingWindow').dialog({width:'800px', height: '500px'});	//, 'modal':true, overlay: {opacity: 0.5}
}

function removeFloatingWindow(){
	$('DIV.ui-dialog-overlay').remove();
	$('DIV.ui-dialog').remove();
}

function URLEncode(mystr) {
	// The Javascript escape and unescape functions do not correspond
	// with what browsers actually do...
	var SAFECHARS = "0123456789" + // Numeric
	"ABCDEFGHIJKLMNOPQRSTUVWXYZ" + // Alphabetic
	"abcdefghijklmnopqrstuvwxyz" +
	"-_.!~*'()"; // RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";
	
	var plaintext = mystr;
	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
		if (ch == " ") {
			encoded += "+"; // x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
			encoded += ch;
		} else {
			var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for
	return encoded;
};


// feedback form
var fbVisible = false  
$(document).ready(function () {
	var fbVisible = false;
		$('#subiect_feedback').blur(function() {
			if($('#subiect_feedback').val() == '') {
				$('#subiect_feedback').val('Subiect (optional)');
			}
		});
		$('#subiect_feedback').focus(function() {
			if($('#subiect_feedback').val() == 'Subiect (optional)') {
				$('#subiect_feedback').val('');
			}
		});			
		$('#email_feedback').blur(function() {
			if($('#email_feedback').val() == '') {
				$('#email_feedback').val('Adresa de email (optional)');
			}
		});
		$('#email_feedback').focus(function() {
			if($('#email_feedback').val() == 'Adresa de email (optional)') {
				$('#email_feedback').val('');
			}
		});
		
		$('#mesaj_feedback').blur(function() {
			if($('#mesaj_feedback').val() == '') {
				$('#mesaj_feedback').val('Mesaj');
			}
		});
		$('#mesaj_feedback').focus(function() {
			if($('#mesaj_feedback').val() == 'Mesaj') {
				$('#mesaj_feedback').val('');
			}
		});
});
function toggleFeedBack() {
	$("#feedbackdiv").animate({"right": ((fbVisible = !fbVisible)? "+":"-") + "=303px"}, "slow");
}
function validateFeedbackForm() {
	$.ajax({
		type: "POST",
		url: _APP_URL + "/site/frontend/your-feedback",
		data: {
			subject: $('#subiect_feedback').val(),
			email:	$('#email_feedback').val(),
			mesaj:	$('#mesaj_feedback').val()
		},
		success: function(msg){
			toggleFeedBack();
			alert('Mesajul tau a fost trimis! Multumim.');
		}
	});
	return false;
}	
//end feedback form
