var gata = 0;
var oldlength = 0;
var maxlimit = 0;
var leftchars = 0;
var current = 0;
var ok = 1;
var msup = 0;
var key = 0;
var NEXT_PG = 0;
var PREV_PG = 0;
var $j = jQuery.noConflict();
$j(document).ready(function(){

//$(function() {
	//////////////////  -  \\\\\\\\\\\\\\\\\\\\
	if ($j('img.photo_details').length > 0)
		$j('img.photo_details').cluetip({
			tracking:true
			,width:150
			,positionBy:'mouse'
			,dropShadow:false
			,attribute:'alt'
			,cluetipClass: 'jtip'
		});
	/////////////////////// - \\\\\\\\\\\\\\\\\\\\\
	
	if ($j('#form_sel_photo').length > 0) {
		$j('#form_sel_photo').ajaxForm({
			success: function(response){
				if (response != 'File successfully uploaded') {
					$j('#right_side').html(response);
					gata = 0;
				}
				else {
				
					gata = 1;
					self.parent.tb_remove();
					self.parent.document.location = 'index.php?my_profile_page=3';
				}
			}
		});
	}
	if ($j('#formPhotoComment').length > 0) {
		
		$j('#formPhotoComment').ajaxForm({
			success: function(response){
				if (response != 'success') {
					alert(response);
				}
				else {
					$j('#photocomments').load(HTTP + 'index.php?load_pic_comments=' + PIC_ID);
					$j('#formPhotoComment').clearForm();
				}
			}
		});
	}
	if ($j('#formMail').length > 0) {
		$j('#formMail').ajaxForm({
			success: function(response){
				
				if(response == 'stepped email'){
					document.location = HTTP + 'index.php?stepped_emails';
				}
				else if (response != 'E-mail successfully sent') {
					$j('#right_side').html(response);
					
				}
				else {
					
					$j('#right_side').html('');
					self.parent.tb_remove();
				}
			}
		});
		$j('#formMail').submit(function(){
			$j('#right_side').html('<img align="absmiddle" src="'+ HTTP +'_layouts/images/loading.gif" />&nbsp;&nbsp;<span style="color: #000000;">Please wait... Sending email(s).</span>');
		})
	}
	if($j('#formEdsel_photo').length > 0)
		$j('#formEdsel_photo').ajaxForm({success: function(response) {
				if (response != 'File successfully uploaded') {
					$j('#right_side').html(response);
					gata = 0;
				}
				else {
					gata = 1;
					self.parent.tb_remove();
					self.parent.document.location = 'index.php?user_photos=' + USERPROFILE_ID;
				}				
		}});
	if($j('#formSubsel_photo').length > 0)
		$j('#formSubsel_photo').ajaxForm({success: function(response) {
				if (response != 'File successfully uploaded') {
					$j('#right_side').html(response);
					gata = 0;
				}
				else {
					gata = 1;
					self.parent.tb_remove();
					self.parent.document.location = HTTP + 'edit_user_photos/0' + USERPROFILE_ID + '.html';
				}				
		}});
	
	if($j('#formEdselm_photo').length > 0)
		$j('#formEdselm_photo').ajaxForm({success: function(response) {
				if (response != 'File successfully uploaded') {
					$j('#right_side').html(response);
					gata = 0;
				}
				else {
					gata = 1;
					self.parent.tb_remove();
					self.parent.document.location = 'index.php?edit_page=' + PAGE_ID;
				}				
		}});
	if($j('#formAddPage').length > 0)
		$j('#formAddPage').ajaxForm({success: function(response) {
				if (response != 'Page successfully created') {
					$j('#right_side').html(response);
					gata = 0;
				}
				else {
					gata = 1;
					self.parent.tb_remove();
					self.parent.document.location = 'index.php?page_manager';
				}				
		}});
	if($j('#formAddSection').length > 0)
		$j('#formAddSection').ajaxForm({success: function(response) {
				if (response != 'Section successfully created') {
					$j('#right_side').html(response);
					gata = 0;
				}
				else {
					gata = 1;
					self.parent.tb_remove();
					self.parent.document.location = 'index.php?page_manager';
				}				
		}});
	/*$('form input[type=text]').blur(function() { 
		$(this).removeClass('box_username_over');
		$(this).addClass('box_username');  
	});
	$('form input[type=text]').focus(function() { 
		$(this).removeClass('box_username');
		$(this).addClass('box_username_over');  
	});
	$('form input[type=password]').blur(function() { 
		$(this).removeClass('box_username_over');
		$(this).addClass('box_username');  
	});
	$('form input[type=password]').focus(function() { 
		$(this).removeClass('box_username');
		$(this).addClass('box_username_over');  
	});
	
	$('form textarea').blur(function() { 
		$(this).removeClass('text_area_over');
		$(this).addClass('text_area');  
	});
	
	$('form textarea').focus(function() { 
		$(this).removeClass('text_area');
		$(this).addClass('text_area_over');  
	});*/
	
	$j('form#formQSet input[type=text]').keyup(function(e){		
		var text = $j(this).val();
		var textlength = text.length;
		if(textlength > 100){
			$j(this).val(text.substr(0,100));
			 return false;
		}else{
			return true;
		}
 	});
	
	$j('form#formText textarea').keyup(function(e){
		
		maxlimit = $j('#formleftchars').attr('value');

		all_text = '';
		$j('form#formText textarea').each(function(){
			all_text += $j(this).val();	
		});
				
		remaining_chars = maxlimit-all_text.length;
		if (remaining_chars < 0) {
			text_value = $j(this).val();
			
			$j(this).val(text_value.substr(0, text_value.length+remaining_chars));
			remaining_chars='0';
		}
		if(remaining_chars == 0)
			remaining_chars = '0';

		$j('span#charsleft').html(remaining_chars);
		
	});
	
	$j('form#formUserQ textarea').keyup(function(e){
		if ( ! include_textarea($j(this).attr('id'))) return;
		maxlimit = $j('#formleftchars').attr('value');

		all_text = '';
		$j('form#formUserQ textarea').filter(filter_textarea).each(function(){
			all_text += $j(this).val();	
		});
				
		remaining_chars = maxlimit-all_text.length;
		if (remaining_chars < 0) {
			text_value = $j(this).val();
			
			$j(this).val(text_value.substr(0, text_value.length+remaining_chars));
			remaining_chars='0';
		}
		if(remaining_chars == 0)
			remaining_chars = '0';

		$j('span#charsleft').html(remaining_chars);
		
	});
	
	$j('form#frmc textarea').keydown(function(e){		
		if (ok == 1) {
			oldlength = $j(this).val().length;
			ok = 0;			
			key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
		}
 	});
	
	$j('form#frmc textarea').keyup(function(e){
		//detect if the check box is checked
		var name = $j(this).attr('id');			
		tp = name.substr(9);
		docount = 0
		if ($j('#check_' + tp).val() != null) {
			if ($j('#check_' + tp).is(':checked')) 
				docount = 1;
		}else docount = 1;
		//detect shift
		key1 = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
		if (key != 9 && key1 != 16 && key == key1 && docount == 1) {	
			if(ok == 0) ok = 1;
			else ok = 0;
						
			maxlimit = $j('#maxlimit').attr('value');
			var totals = $j('#totalchars').attr('value');
			//actualizez numarul de caractere al formularului fara textarea curennt
			currentlength = $j(this).val().length;
			
			//alert(oldlength + '>' + currentlength);
			
			totals = totals - oldlength;
			
			limit = maxlimit - totals;
				
			leftchars = $j('span#charsleft').html();
			var text1 = $j(this).val();
		//	current = maxlimit - totals - currentlength;
			if(currentlength > limit){
				$j(this).val(text1.substr(0, limit));
				totals = totals + limit;
			}else{
				$j('span#charsleft').html(limit-currentlength);
				totals = totals + currentlength;
			}		
			
			$j('#totalchars').attr('value', totals);
		}else if(key == 9 && key1 != 16){
			oldlength = $j(this).val().length;
			ok = 0;
		}else if(key1 == 16){
			docount = 1;
		}
 	//	limitChars('', 20, 'charlimitinfo');
 	});
	
	 if ($j("#dob").length > 0) {
		$j("#dob").datepicker({
					changeFirstDay: false,
					dateFormat: 'dd/mm/yy',
					showOn: 'both',
					buttonImage: HTTP+'_layouts/images/calendar1.gif',
					buttonImageOnly: true,
					yearRange: '1930:2030',
					defaultDate: new Date ( "January 1, 1990" )
				});
		}
	 
	 if ($j("#deadline").length > 0) {
			$j("#deadline").datepicker({
						changeFirstDay: false,
						dateFormat: 'dd/mm/yy',
						showOn: 'both',
						buttonImage: HTTP+'_layouts/images/calendar1.gif',
						buttonImageOnly: true,
						yearRange: '1930:2030',
						defaultDate: new Date ()
					});
			}
	
	//$j('.ui-datepicker-trigger').attr('src', HTTP + $j('.ui-datepicker-trigger').attr('src'));
		
	//SEO();
	
	document.onkeydown = keydownfnc;
});



function limitChars(){
	maxlimit = $j('#maxlimit').attr('value');
}

function keydownfnc(e){
	//alert('aici');
	if (e == null) {
		keycode = event.keyCode
	}
	else {
		keycode = e.which
	}
	
	if (keycode == 27) {
		tb_remove();
		document.onkeydown = keydownfnc;
	}
	else if (NEXT_PG != null){
		if (keycode == 39 && NEXT_PG != 0) {
			//alert('asd');
			MM_swapImage('right_arr', '', HTTP + '_layouts/images/arrow_right_blue.png', 1);
		}
		else if (keycode == 37 && PREV_PG != 0) {
			MM_swapImage('left_arr', '', HTTP + '_layouts/images/arrow_left_blue.png', 1);
		}
	}
};

function SEO() { 
	if ($j('#seo').length > 0) {
		if ($j('#seo').attr('value') == 1) return;
		$j('#seo').attr('value', 1);
	}
	

	//host = 'http://localhost/YearbookMachine/server';
	host = HTTP.substr(0,HTTP.length - 1);
	//alert(host);
	if (document.location.href.indexOf('admin.php') >= 0) 
    //host = 'http://localhost/YearbookMachine/server/admin.php';
	host = HTTP + 'admin.php';
	//alert();
	//str = "javascript: if(window.confirm('Are you sure you want to enable this user?')) document.location='?users&enableuser=225'";
	//str = "javascript: if(window.confirm('Are you sure you want to delete this user?')) document.location='?users&delete_user=224'";
	//str = '/YearbookMachine/server/admin.php/manage_users.html?manage_users&pag=2';
	//str = '?';
	//TransformLink(str);
	//alert(TransformLink(str));
	//return;

	// transform the links
	$j('a').each(function() {
		if ($j(this).attr('class') != 'thickbox' && $j(this).attr('class') != 'noseo' && $j(this).attr('href') != null) {
			$j(this).attr('href', TransformLink($j(this).attr('href')));
		}
		else if($j(this).attr('href') != undefined){
			link_val = $j(this).attr('href').toString();
			if(link_val.indexOf("http") < 0)
			$j(this).attr('href', host + $j(this).attr('href'));
		}	
	});

	// transform the forms actions
	$j('form').each(function() {
		$j(this).attr('action', TransformLink($j(this).attr('action')));
	});

	link_val_select = new Array();
	link_val_button = new Array();

	// onchange for select
	$j('select').each(function() {
		if (typeof($j(this).attr('onchange')) != 'undefined')
			link_val = $j(this).attr('onchange').toString();
		if (typeof(link_val)=='undefined' || !link_val) return;

		if (link_val.indexOf('function') >= 0) {
			//if (link_val.indexOf('"') >= 0) arr = link_val.split('"'); else arr = link_val.split('\'');
			//link_val = arr[1];
			return;
		}else  {
			if (link_val.indexOf('document.location.href=') >= 0) len = 25;
			else if (link_val.indexOf('document.location=') >= 0) len = 20;
			else return;
			if (!$j.browser.msie) {
				link_val = link_val.substr(len - 1, link_val.length-len-12);
			}
			else {
				len += 22;
				link_val = link_val.substr(len, link_val.length - len - 15);
			}
		}
		link_val = TransformLink(link_val);
		link_val = link_val.substr(0, link_val.length - 7);


		if (typeof($j(this).attr('id')) != 'undefined') {
			link_val_select[$j(this).attr('id')] = link_val;
			$j(this).change(function(){
				document.location = link_val_select[$j(this).attr('id')] + '/0'+this.value+'.html';
			});
		}
	});

	// onchange for buttons
	$j('input[type=button]').each(function() {
		if (!$j.browser.msie) {
			link_val = $j(this).attr('onclick').toString();
			

			if (link_val.substr(0, 16) == 'function onclick') {
				pos = link_val.indexOf('document.location');
				link_val = link_val.substr(pos);
				link_val = link_val.substr(0, link_val.length - 2);
			}

			if (!link_val) return;
			if (link_val.indexOf('document.location') == -1) return;
			pos = link_val.indexOf('=');
			
			link_val = link_val.substr(pos+1, link_val.length-pos);
	
			link_val = replaceChars(link_val, '\'', '');
			link_val = replaceChars(link_val, ' ', '');
			link_val = replaceChars(link_val, ';', '');
			link_val = replaceChars(link_val, '"', '');
			link_val = replaceChars(link_val, '}', '');
			link_val = replaceChars(link_val, "\n", '');

		}
		else {
			
			if (typeof($j(this).attr('onclick')) != 'undefined')
				link_val = $j(this).attr('onclick').toString(); 
			if (!link_val) return;
			
			if (link_val.indexOf('document.location.href=') >= 0) len = 25;
			else if (link_val.indexOf('document.location=') >= 0) len = 20;
			else return;
			len += 22;
			link_val = replaceChars(link_val, '\'', '');
			link_val = link_val.substr(len, link_val.length - len - 3);
		}


		link_val = TransformLink(link_val);

		if (typeof($j(this).attr('id')) != 'undefined') {
			link_val_button[$j(this).attr('id')] = link_val;
			$j(this).attr('onclick', '');
			$j(this).click(function() {
				document.location = link_val_button[$j(this).attr('id')];
			});
		}
	});
	
	// onchange for images
	$j('img#butt').each(function() {
		if (!$j.browser.msie) {
			link_val = $j(this).attr('onclick').toString();
			if (link_val.substr(0, 16) == 'function onclick') {
				pos = link_val.indexOf('document.location');
				link_val = link_val.substr(pos);
				link_val = link_val.substr(0, link_val.length - 2);
				
			
			}

			if (!link_val) return;
			
			if (link_val.indexOf('document.location') == -1) return;
			pos = link_val.indexOf('=');
			
			link_val = link_val.substr(pos+1, link_val.length-pos);
	
	
			link_val = replaceChars(link_val, ' ', '');
			link_val = replaceChars(link_val, ';', '');
			link_val = replaceChars(link_val, '"', '');
			link_val = replaceChars(link_val, '}', '');
			link_val = replaceChars(link_val, "\n", '');

		}
		else {
			
			if (typeof($j(this).attr('onclick')) != 'undefined')
				link_val = $j(this).attr('onclick').toString(); 
			if (!link_val) return;
			
			
			if (link_val.indexOf('document.location.href=') >= 0) len = 25;
			else if (link_val.indexOf('document.location=') >= 0) len = 20;
			else return;
			len += 22;
			link_val = link_val.substr(len, link_val.length - len - 3);
		}


		link_val = TransformLink(link_val);
		

		if (typeof($j(this).attr('id')) != 'undefined') {
			link_val_button[$j(this).attr('id')] = link_val;
			$j(this).attr('onclick', '');
			$j(this).click(function() {
				document.location = link_val_button[$j(this).attr('id')];
			});
		}
	});
	
	//correct the images path
	root = HTTP;
	//root = 'http://localhost/YearbookMachine/server/';
	subdirs = document.location.href.split('/').length - root.split('/').length;
	subdirs_str = '';
	for (i = 0; i < subdirs; i++) subdirs_str += '../';

	
	if (subdirs > 0) 
	$j('img').filter(function() { return $j(this).attr('src').substr(0, 15) == '_layouts/images'; }).each(function() {
		//$j(this).attr('src', subdirs_str + $j(this).attr('src'));
	});
	
}

function replaceChars(entry, out, add) {
	temp = "" + entry; // temporary holder

	while (temp.indexOf(out)>-1) {
		pos= temp.indexOf(out);
		temp = "" + (temp.substring(0, pos) + add + 
		temp.substring((pos + out.length), temp.length));
	}
	return temp;
}

function TransformLink(link_val) {
	if(link_val == undefined) return;
	
	if (link_val.indexOf('.html') >= 0) {
		if(link_val.indexOf('?') > 0){
			
			pos1 = link_val.indexOf('?');
			link_val = link_val.substr(pos1);
			
		}else
		return;
	}
	if (!link_val || typeof(link_val) == 'undefined') return '';
	if (link_val.charAt(0) == '?') {		
		link_val = link_val.substr(1);
	}
	else 
		if (link_val.substr(0, host.length) == host) {
		
			link_val = link_val.substr(host.length);
			pos1 = link_val.indexOf('?') + 1;
			link_val = link_val.substr(pos1);
		}
		else 
			if (link_val.indexOf('.location=\'') > 0) {
				pos1 = link_val.indexOf('.location=\'') + 11;
				pos2 = link_val.substr(pos1).indexOf('\'');
				return link_val.substr(0, pos1) + TransformLink(link_val.substr(pos1, pos2)) + link_val.substr(pos1 + pos2);
			}
			
			else 
				if (link_val.indexOf('?') >= 0 && link_val.indexOf('javascript:') == -1) {
				
					pos1 = link_val.indexOf('?') + 1;
					link_val = link_val.substr(pos1);
				}
				else 
					return;

	
	if (link_val == '') return host;
	if (link_val.indexOf('&amp;') >= 0)
			arr_link_val = link_val.split('&amp;');
	else	arr_link_val = link_val.split('&');

	res = '';
	for (i = 0; i < arr_link_val.length; i++) {
		arr_attr = arr_link_val[i].split('=');
		if (arr_attr.length == 2)
				res += arr_attr[0] + '/0' + arr_attr[1] + '/';
		else 	res += arr_attr[0] + '/';
	}
	
	ress = res;
	res = ress.substr(0, ress.length - 1) + '.html';
	res = replaceChars(res, '#', '/');
	//if (res.indexOf('#') >= 0) alert(res);
	if(res.indexOf('.csv') > 0) 
		res = res.substr(0, res.length - 5);

	return host + '/' + res;
}

///////////////////////////////  \\\\\\\\\\\\\\\\\\\\

	function ViewNotifications(){
		$j('#read_notifications').slideToggle();		
		if($j('#view_notifications').html() == 'View all')
			$j('#view_notifications').html('View unread');
		else
			$j('#view_notifications').html('View all');		
	} 
	
	function ReadNotifications(cond){	
		$j('#notifications').html('<img align="absmiddle" src="'+ HTTP +'_layouts/images/loading.gif" style="padding-left: 150px;"/>');
		$j.post(HTTP + 'read_notifications/'+cond, {}, function(response){	    			
    		$j('#notifications').html(response);	    		
	    });	
	}
	function CheckToDoList(id,section,status,book){		
		$j('#content_to_do_list').html('<img align="absmiddle" src="'+ HTTP +'_layouts/images/loading.gif" style="padding-left: 83px; padding-top: 70px;"/>');
		$j.post(HTTP + 'check_to_do_list/'+id+'/'+section+'/'+status+'/'+book, {}, function(response){    		
			$j('#content_to_do_list').html(response);	    		
	    });	
	}
	
	
	
	