function noError(){
	return true;
}

window.onerror = noError;

var vurl = window.top.location.toString();


function isEmail(str) {
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	return (filter.test(str) ? true : false);
}


function is_alpha(test, v) {

	if (v == "a") var lists = "abcdefghijklmnoqprstuvwxyzABCDEFGHIJKLMNOQPRSTUVWXYZ_-./ ";
	if (v == "b") var lists = "1234567890abcdefghijklmnoqprstuvwxyzABCDEFGHIJKLMNOQPRSTUVWXYZ_-!,.?()/ ";
	if (v == "c") var lists = "1234567890_-/.\\ ";
	if (v == "d") var lists = "1234567890";
	else var lists = "1234567890abcdefghijklmnoqprstuvwxyzABCDEFGHIJKLMNOQPRSTUVWXYZ_-./ ";

	var match = true;
	var found = false;
	var chars = "";

	for (var i = 0; i < test.length; i++) {
		chars = test.charAt( i );
		found = false;
		for (var j = 0; j < lists.length; j++) {
			if(lists.charAt( j ) == chars) {
				found = true;
				break;
			}
		}
		if (!found) {
			match = false;
			break;
		}
	}
	return match;
}

function preiaObjectDinHTTP() {
	var xhr = false;
	if (window.ActiveXObject) {
		try {
			xhr = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try {
				xhr = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e) {
				xhr = false;
			}
		}
	} else if (window.XMLHttpRequest) {
		try {
			xhr = new XMLHttpRequest();
		} catch(e) {
			xhr = false;
		}
	}
	return xhr;
}


document.getElementsByClassName = function(cl) {
	var retnode = [];
	var myclass = new RegExp('\\b'+cl+'\\b');
	var elem = this.getElementsByTagName('*');
	for (var i = 0; i < elem.length; i++) {
	var classes = elem[i].className;
	if (myclass.test(classes)) retnode.push(elem[i]);
	}
	return retnode;
};


$(document).ready(function() {
	$("ul[@id=navig] li").corner("5px");
	$("ul[@class=picsthumbs] li").corner("5px");
	$("#stiribox").corner("bottom:10px");
	$("#whitebg").corner("bottom:10px");
	$("#AlegeOrasul").corner("10px");

	//if(vurl.indexOf('cauta') == -1 && vurl.indexOf('album-poze') == -1) $('#content').css('height', $('#extra').height()+'px');
	//else $('#content').css('height', $('#wrapper').height()+'px');

	var nHeight=0;
	$('.picsthumbs li').each(function() {
		var myHeight = $(this).height();
		if(myHeight > nHeight) nHeight = myHeight;
	});


	$('.picsthumbs li').each(function() {
		$(this).css('height', nHeight + 'px');
	});

	$('.confirm').click(function() {
		return (confirm('Esti sigur ca vrei sa stergi?') ? true : false);
	});

	$('#cauta').focus(function() {
		if (this.value == 'Cauta in arhiva de poze') {
			this.value = '';
		}
	});

	$('#cauta').blur(function() {
		if ($.trim(this.value) == '') {
			this.value = 'Cauta in arhiva de poze';
		}
	});

	if(document.getElementsByClassName('clubox')) {
		var nHeight = 0;
		var nHeight2 = 0;
		var vcmd = document.getElementsByClassName('clubox');

		for (var i = 0; i < vcmd.length; i++) {
			if(vcmd[i].offsetHeight > nHeight) nHeight = vcmd[i].offsetHeight;
		}

		for (var j = 0; j < vcmd.length; j++) {
			vcmd[j].style.cssText = 'height: '+nHeight+'px;';
		}
	}


	if(document.getElementById('voteaza')) {
		var vvoteaza = document.getElementById('voteaza').getElementsByTagName('input');
		for (var j = 0; j < vvoteaza.length; j++) {
				if (vvoteaza[j].value != '') {
					vvoteaza[j].onclick = function() {
						var vOpt = this.value.split('_');
						if(confirm('Ati ales optiunea '+vOpt[1]+', sunteti sigur?')) {
							var request = preiaObjectDinHTTP();
							if (request) {

								request.onreadystatechange = function() {
									if (request.readyState == 4) {
										if (request.status == 200) {
											if(request.responseText != "") {
												document.getElementById('voterez').innerHTML = request.responseText;
												alert("Iti multumim pentru votul acordat!");
											}
										}
									}
								};

								params = 'optiune='+vOpt[0]+'&action=getVote';
								request.open("POST", "server.php", true);
								request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
								request.send(params);
							}
						}
						return false;
					}
				}
			}
	}


	if(document.getElementById('searchF')) {
		var vcauta = document.getElementById('cauta');

		vcauta.onfocus = function()  { vcauta.style.cssText = 'background:#818181'; }
		vcauta.onblur = function()  { vcauta.style.cssText = ''; }
		vcauta.setAttribute("autocomplete", "off");

		document.getElementById('searchF').onsubmit = function() {
			var er = '';
			var err = '';
			var vccauta = vcauta.value;

			if (vccauta.replace(/^\s+|\s$/g, '').length < 3 || vccauta.replace(/^\s+|\s$/g, '').length > 100 || !is_alpha(vccauta, 'a')) {
				er = 'x';
				vcauta.style.cssText = "color: #000; background: #ECBFC6; border: 1px solid #91020C;";
				vcauta.onfocus = function()  { vcauta.style.cssText = 'color:#000;background:#F5E0E4'; }
				vcauta.onblur = function()  { vcauta.style.cssText = 'color:#000;background:#ECBFC6'; }
			} else {
				vcuvant = vccauta.split(' ');

				if(!is_alpha(vcuvant[vcuvant.length-1], 'd') || vcuvant[vcuvant.length-1].length != 4) {
					er = 'x';
					err = 'Sintaxa pentru sistemul de cautare este: \nCuvant luna (optional) AN - Ex: Crush 2007 sau Crush Ianuarie 2007';
					vcauta.style.cssText = "color: #000; background: #ECBFC6; border: 1px solid #91020C;";
					vcauta.onfocus = function()  { vcauta.style.cssText = 'color:#000;background:#F5E0E4'; }
					vcauta.onblur = function()  { vcauta.style.cssText = 'color:#000;background:#ECBFC6'; }
				} else {
					vcauta.style.cssText = "";
					vcauta.onfocus = function()  { vcauta.style.cssText = 'background:#818181'; }
					vcauta.onblur = function()  { vcauta.style.cssText = ''; }
				}
			}

			if (er != 'x') {
				document.getElementById('searchF').submit();
				return true;
			} else {
				alert(err != '' ? err : 'Va rugam completati corect campul');
			}
			return false;
		}
	}


	if(document.getElementById('autentificare')) {
		var vrluser = document.getElementById('lutilizator');
		var vrlpass = document.getElementById('lparola');

		vrluser.onfocus = function()  { vrluser.style.cssText = 'background:#818181'; }
		vrluser.onblur = function()  { vrluser.style.cssText = ''; }
		vrluser.setAttribute("autocomplete", "on");

		vrlpass.onfocus = function()  { vrlpass.style.cssText = 'background:#818181'; }
		vrlpass.onblur = function()  { vrlpass.style.cssText = ''; }
		vrlpass.setAttribute("autocomplete", "off");

		document.getElementById('autentificare').onsubmit = function() {
			var er = '';
			var err = 'Va rugam completati campurile!';
			var vrluserv = vrluser.value;
			var vrlpassv = vrlpass.value;

			if (!isEmail(vrluserv)) {
				er = 'x';
				vrluser.style.cssText = "color: #000; background: #ECBFC6; border: 1px solid #91020C;";
				vrluser.onfocus = function()  { vrluser.style.cssText = 'color:#000;background:#F5E0E4'; }
				vrluser.onblur = function()  { vrluser.style.cssText = 'color:#000;background:#ECBFC6'; }
			} else {
				vrluser.style.cssText = '';
			}


			if (vrlpassv.replace(/^\s+|\s$/g, '').length < 6 || vrlpassv.replace(/^\s+|\s$/g, '').length > 100) {
				er = 'x';
				vrlpass.style.cssText = "color: #000; background: #ECBFC6; border: 1px solid #91020C;";
				vrlpass.onfocus = function()  { vrlpass.style.cssText = 'color:#000;background:#F5E0E4'; }
				vrlpass.onblur = function()  { vrlpass.style.cssText = 'color:#000;background:#ECBFC6'; }
			} else {
				vrlpass.style.cssText = '';
			}


			if (er != 'x') {
				var request = preiaObjectDinHTTP();
					if (request) {

						request.onreadystatechange = function() {
							if (request.readyState == 4) {
								if (request.status == 200) {
									if(request.responseText != "") {
										er = 'x';
										vrluser.style.cssText = "color: #000; background: #ECBFC6; border: 1px solid #91020C;";
										vrluser.onfocus = function()  { vrluser.style.cssText = 'color:#000;background:#F5E0E4'; }
										vrluser.onblur = function()  { vrluser.style.cssText = 'color:#000;background:#ECBFC6'; }

										vrlpass.style.cssText = "color: #000; background: #ECBFC6; border: 1px solid #91020C;";
										vrlpass.onfocus = function()  { vrlpass.style.cssText = 'color:#000;background:#F5E0E4'; }
										vrlpass.onblur = function()  { vrlpass.style.cssText = 'color:#000;background:#ECBFC6'; }

										if(request.responseText == 'log') alert('Date incorecte');
										else if(request.responseText == 'alog') alert('Va rugam activati contul!');
									} else {
										vrluser.style.cssText = '';
										vrlpass.style.cssText = '';

										document.getElementById('autentificare').submit();
										return true;
									}
								}
							}
						};

						params = 'user='+vrluserv+'&pass='+strL(vrlpassv)+'&remember='+(document.getElementById('rememberMe').checked ? 'Da' : '')+'&action=rlog';
						request.open("POST", "server.php", true);
						request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
						request.send(params);
						return false;
					}

			} else alert(err);


			return false;
		}
	}


	/*$('#left2 .listusers li').hover(showPicOnHover() , function(){
		return nd();
	});*/

	$('.showPicOnHover').hover(function(){
		try {

			var href = $(this).attr('href');
			var userId = href.substring(href.lastIndexOf('/') + 1, href.lastIndexOf('.'));

		  var response = $.ajax({
				url: "ajax.php",
				type: "POST",
				async: false,
				data: ({getPhotoId : 1, userId : userId})
			 }
			).responseText;

			// document.write('poze/thumbs/' + userId + '_'  + photoId + '.jpg');

			var array = response.split('|')

			return overlib('<img src="' +  array[0] + '" alt="" style="margin:' + (48 + (100 - array[2]) / 2) +'px auto 0 auto;display:block;"/>',
										 BELOW, LEFT, TEXTCOLOR, '#ffffff', TEXTSIZE, 2,
										 BACKGROUND, 'bule_simple.png' , FGCOLOR, ' ',
										 WIDTH, 112, HEIGHT , 155
			);

		} catch (e) {
			alert(e);
		}
	} , function(){
		return nd();
	});

	try {

		if ($('.rating').length > 0) {

			function getRating(section, id)
			{

				var response =  $.ajax({
					type: 'POST',
					url: 'ajax.php',
					data: ({getRating: 1, section: section, id: id}),
					cache: false,
					async: false
				}).responseText;

				// alert(response);

				var arr = response.split('|');
				var rating = arr[0];
				var procent = rating * 20;
				var numar_voturi = arr[1];
				var text = rating + ' / 5; voturi: ' + numar_voturi;


				$('#' + section + '-' +  id).next('.rating-info').remove();
				$('#' + section + '-' +  id).after('<div class="rating-info" title="' + text + '">' + text + '</div>');
				$('#' + section + '-' +  id + ' .current-rating').css('width', procent + '%');

			}

			$('.rating').each(function(){
				var arr = $(this).attr('id').split('-');
				var section = arr[0];
				var id = arr[1];
				getRating(section, id);
			});

			$('.rating a').click(function() {

				var parent = 	$(this).parents('ul.rating');
				var arr = parent.attr('id').split('-');
				var section = arr[0];
				var id = arr[1];

				if ($.cookie(section + '-' + id) != 'votat') {
					var response = $.ajax({
						type: 'POST',
						url: 'ajax.php',
						data: ({voteaza: 1, section: section, id: id, vot: $(this).text()}),
						cache: false,
						async: false
					}).responseText;

					if (response == 'succes') {
						// $.cookie(section + '-' + id, 'votat');
						parent.find('li:not(.current-rating)').remove();
						getRating(section, id);
						alert('Va multumim pentru vot');
					} else if (response == 'notLoggedIn') {
						alert('Trebuie sa fiti logat pentru a vota');
					} else if (response == 'voteOwn') {
						alert('Nu va puteti vota propriul profil');
					}

				} else {
					alert('Ati votat deja..');
				}
			});

		}
	} catch (e) {
		alert(e);
	}

	$('#cautareFun input').focus(function(){
		if (this.value == 'cautare video' || this.value == 'cautare jocuri') {
			this.value = '';
		}
	});

	$('#cautareFun input').blur(function(){
		if (this.value == '') {
			var arr = $(this).parents('#cautareFun').attr('action').split('.');
			var arr2 = arr[0].split('-');
			this.value = 'cautare ' + arr2[1];
		}
	});

	$('#cautareFun').submit(function(){
		var value = $('#cautareFun input').val();
		if (value == 'cautare video' || value == 'cautare jocuri' || $.trim(value) == '')  {
			return false;
		}
	});

	$('#menu li').hover(function(){
		if ($(this).find('ul').css('display') == 'none') {
			$(this).find('ul').slideToggle();
		}
	}, function(){
		if ($(this).find('ul').css('display') == 'block') {
			$(this).find('ul').slideToggle();
		}
	});

	$('#menu li li').hover(function(){
		$(this).css('background-color', '#BF3E4D');
	}, function(){
		$(this).css('background-color', '');
	});



});
