$(document).ready(function() {
	
	/** INITIALIZE SECTION */
	searchSubInitializer();
	zoneSubInitializer();
	
	/** LISTENER SECTION */
	
	
	/** FUNCTION SECTION */
	function zoneSubInitializer() {
		//to prevent secondary subj tree initialization because of IE7 script error
		var tab = 'z_ztab1';
		var id  = 'ztab1';
		var publishermaintab = $('#zone_subject');

		$('#'+tab).parent().parent().find('li').each(function() {
			$(this).attr('id','notselected');
		}
		);
		$('#'+tab).parent().removeAttr('id');
		
		$('#zone_subject .tab').each(function() {
			$(this).attr('style','display: none;');
		});
		
		clearSession('ztab1');
		clearSession('ztab2');
		clearSession('ztab3');
		if (!isAlreadyInitialize(id)) {
			setCheckedTreeCustom(id);
			setBeginCustom(id);
			setColoredTabsCustom(id);
		}
		
		$('#'+id).fadeIn();
		$('#'+id).parent().show();
	};
	
	function searchSubInitializer() {
		if (toEmptyString('searchSubjTab') == '') { /** default tab opened */
			$.session('searchSubjTab:','s_stab2');
		}
		if (toEmptyString('searchSubjId') == '') { /** default tab opened */
			$.session('searchSubjId:','stab2');
		}

		var tab = $.session('searchSubjTab:')
		var id  = $.session('searchSubjId:');

		var tabDiv = $('#'+tab);
		
		if (null != tabDiv) {
			$('#'+tab).parent().parent().find('li').each(function() {
				$(this).attr('id','notselected');
			}
			);
			$('#'+tab).parent().removeAttr('id');
			
			$('.tab').each(function() {
				$(this).attr('style','display: none;');
			});
			
			$('#'+id).fadeIn('slow');
			$('#'+id).parent().show('slow');
			
			setCheckedTreeCustom(id);
		}
		
	};
	
});

/** EXTERNAL FUNCTION SECTION */
function SearchSubjectTabAnimation(name,id,tab) {
	
	$.session('searchSubjTab:',$(tab).attr('id'));
	
	if ($.session('searchSubjId:') == id) {
		return;
	} 
	$.session('searchSubjId:',id);

	$(tab).parent().parent().find('li').each(function() {
		$(this).attr('id','notselected');
	}
	);

	$(tab).parent().removeAttr('id');
	
	$('#search_subject .tab').each(function() {
		$(this).attr('style','display: none;');
	});

	setCheckedTreeCustom(id);

	$('#'+id).fadeIn('slow');
	$('#'+id).parent().show('fast');
};

function subjectTabAnimation(name,id,tab) {
	if ($.session('id:') == id) {
		return;
	} 
	
	$('#zone_subject .tab').each(function() {
		$(this).attr('style','display: none;');
	});
	
	progId = 'z_subj_tab_loading';
//	setupProgressBar(progId, 10);
	
	$.session('tab:',$(tab).attr('id'));
	
	$.session('id:',id);

	$(tab).parent().parent().find('li').each(function() {
		$(this).attr('id','notselected');
	});

	$(tab).parent().removeAttr('id');

	if (!isAlreadyInitialize(id)) {
		setCheckedTreeCustom(id);
//		changeValueProgressBar(progId, 10);
		setBeginCustom(id);
//		changeValueProgressBar(progId, 50);
		setColoredTabsCustom(id);
//		changeValueProgressBar(progId, 90);
	}
	
//	closeProgressBar('z_subj_tab_loading');
	$('#'+id).fadeIn('slow');
	$('#'+id).parent().show('fast');
	
};

function setupProgressBar(id, value){
	$('#progressBarDiv').fadeIn('fast');
	$('#'+id).progressbar({
		value: value
	});
}

function changeValueProgressBar(id, value){
	$('#'+id).progressbar("value", value);
}

function closeProgressBar(id){
	$('#progressBarDiv').fadeOut('fast');
	$('#'+id).progressbar('destroy');
}

function clearSession(id){
	$.session(''+id,"--");
}

function isAlreadyInitialize(id){
	if (null == $.session(''+id) || "--" == $.session(''+id)) {
		$.session(''+id,"y");
		return false;
	}
	return true;
}

function setBeginCustom(id) {
	var topUnorderedList  	= $('#'+id+' ul.top3');
	var tree 				= topUnorderedList.find('li.tree');
	var firstLevel  		= tree.find('span.first' );
	var secondLevel 		= tree.find('span.second');
	var thirdLevel  		= tree.find('span.third' );

	var firstLevelInput  	= tree.find('input.first' );
	var secondLevelInput 	= tree.find('input.second');
	var thirdLevelInput  	= tree.find('input.third' );

	var loop = null, loopLen = 0, _this = null;
	
	loop = $(topUnorderedList);
	loopLen = loop.length;
	for (var i=0;i<loopLen;i++) {
		_this = loop[i];
		$(_this).find('li:first').attr('class','search_multipleselect_bordertop'); 
	}

	loop = $(firstLevel);
	loopLen = loop.length;
	for (var i=0;i<loopLen;i++) {
		_this = loop[i];
		if ($(_this).next('ul').length > 0) {
			$(_this).wrap('<a class="closeElFirst" href="javascript:void(0);"></a>');
			$(_this).addClass('hasArrow');
		}else{
			$(_this).addClass('hasNoArrow');
		}
	}

	loop = $(secondLevel);
	loopLen = loop.length;
	for (var i=0;i<loopLen;i++) {
		_this = loop[i];
		if ($(_this).next('ul').length > 0) {
			$(_this).wrap('<a class="closeElSecond" href="javascript:void(0);"></a>');
			$(_this).addClass('hasArrow');
		}else{
			$(_this).addClass('hasNoArrow');
		}
	}

	loop = $(thirdLevel);
	loopLen = loop.length;
	for (var i=0;i<loopLen;i++) {
		_this = loop[i];
		if ($(_this).next('ul').length > 0) {
			$(_this).wrap('<a class="closeElThird" href="javascript:void(0);"></a>');
			$(_this).addClass('hasArrow');
		}else{
			$(_this).addClass('hasNoArrow');
		}
	}
	var subLevelToggles = $('#assetBeanForm #'+id+' a.closeElFirst, #assetBeanForm #'+id+' a.closeElSecond, #assetBeanForm #'+id+' a.closeElThird');
	$(subLevelToggles).click(function() {toggleSubContent(this);});
	
};


function setCheckedTreeCustom(id) {
	$('#'+id+' ul.top3').find(':checkbox').click(
		function() {
			var $par = $(this).parent();
			var clazz = $(this).attr('class');
			
			if ($(this).attr('checked')){$par.find(':checkbox').attr('checked','checked');}
			else {$par.find(':checkbox').removeAttr('checked');}
	
			if (clazz == 'first') {}
			else if (clazz == 'second') {
				var sec_parent = $(this).parents('ul.first');
				var sibs = $(sec_parent).find('input.second').length;
				var chkd = $(sec_parent).find('input.second').filter(':checked').length;
	
				var first_parent = $(sec_parent).parent('li').find('input.first');
				if (sibs == chkd) { $(first_parent).attr('checked','checked'); }
				else { $(first_parent).removeAttr('checked'); }
			}
			else if (clazz == 'third') {
				var sec_parent = $(this).parents('ul.second');
				var sibs = $(sec_parent).find('input.third').length;
				var chkd = $(sec_parent).find('input.third').filter(':checked').length;
	
				var first_parent = $(sec_parent).parent('li').find('input.second');
				if (sibs == chkd) { $(first_parent).attr('checked','checked'); }
				else { $(first_parent).removeAttr('checked'); }
	
				var sec_parent = $(sec_parent).parents('ul.first');
				var sibs = $(sec_parent).find('input.second').length;
				var chkd = $(sec_parent).find('input.second').filter(':checked').length;
	
				var first_parent = $(sec_parent).parent('li').find('input.first');
				if (sibs == chkd) { $(first_parent).attr('checked','checked'); }
				else { $(first_parent).removeAttr('checked'); }
			}
		}
	);
};

function getTabColorSubj(){
	var layout = theme;
	var tabbedhue = '#82ae32';
	
	if (layout.indexOf('green') != -1) {
		tabbedhue = '#BE7A03';
	}else if (layout.indexOf('gray') != -1){
		tabbedhue = '#82ae32';
	}
	
	return tabbedhue;
}

function setColoredTabsCustom(id) {

	var tabbedhue = getTabColorSubj();
	var untabbedhue = 'black';
	var loop = null, loopLen = 0, _this = null;
	
	loop = $('#'+id+' ul.top3');
	loopLen = loop.length;
	var zoneSubjCheck = 0;
	for (var i=0; i<loopLen; i++) {
		_this = loop[i];
		
		if (($('#ztab1 ul.top1, #ztab1 li.lisecond, #ztab1 li.lithird').find(':checked').length) > 0) {
			$('#z_ztab1').find('span').attr('style', 'color: '+tabbedhue+';');
		}else{
			$('#z_ztab1').find('span').removeAttr('style');
		}
		if (($('#ztab2 ul.top3, #ztab2 li.lisecond, #ztab2 li.lithird').find(':checked').length) > 0) {
			$('#z_ztab2').find('span').attr('style', 'color: '+tabbedhue+';');
		}else{
			$('#z_ztab2').find('span').removeAttr('style');
		}
		if (($('#ztab3 ul.top3, #ztab3 li.lisecond, #ztab3 li.lithird').find(':checked').length) > 0) {
			$('#z_ztab3').find('span').attr('style', 'color: '+tabbedhue+';');
		}else{
			$('#z_ztab3').find('span').removeAttr('style');
		}
		
		if (($('#zone_sub_tab ul.top3').find(':checked').length - 1) > 0) {
			$('.subject_search2').find('a.closeEl').attr('style', 'color: '+tabbedhue+';');
			zoneSubjCheck = 1;
		}else{
			if (zoneSubjCheck != 1) {
				$('.subject_search2').find('a.closeEl').removeAttr('style');
			}
		}
		
		$(_this).find('li.search_multipleselect_bordertop, li.tree').each(
			function() {
				if ($(this).find(':checked').length > 0) {
					$(this).find('span.first').attr('style', 'color: '+tabbedhue+';');
				}
				else {
					if (zoneSubjCheck != 1) {
						$(this).find('span.first').removeAttr('style');
					}
				}
			}
		);

		$(_this).find('li.lisecond').each(
			function() {
				if ($(this).find(':checked').length > 0) {
					$(this).find('span.second').attr('style', 'color: '+tabbedhue+';');
				}
				else {
					if (zoneSubjCheck != 1) {
						$(this).find('span.second').removeAttr('style');
					}
				}
			}
		);

		$(_this).find('li.lithird').each(
			function() {
				if ($(this).find(':checked').length > 0) {
					$(this).find('span.third').attr('style', 'color: '+tabbedhue+';');
				}
				else {
					if (zoneSubjCheck != 1) {
						$(this).find('span.third').removeAttr('style');
					}
				}
			}
		);
	}

	
	$('#'+id+' ul.top3').bind('click',
		function() {
			var zoneSubjCheck = 0;
			
			if (($(this).find(':checked').length) > 0) {
				$('#z_'+id).find('span').attr('style', 'color: '+tabbedhue+';');
			}else{
				$('#z_'+id).find('span').removeAttr('style');
			}
			
			if (($(this).find(':checked').length) > 0) {
				$('.subject_search2').find('a.closeEl').attr('style', 'color: '+tabbedhue+';');
				zoneSubjCheck = 1;
			}else{
				if (zoneSubjCheck != 1) {
					$('.subject_search2').find('a.closeEl').removeAttr('style');
				}
			}
			
			
			var loop11 = null, loopLen11 = 0, _this11 = null;
			
			loop11 = $(this).find('li.search_multipleselect_bordertop, li.tree');
			loopLen11 = loop11.length;
			for (var i=0; i<loopLen11; i++) {
				_this11 = loop11[i];
				if ($(_this11).find(':checked').length > 0) {
					$(_this11).find('span.first').attr('style', 'color: '+tabbedhue+';');
				}
				else {
					if (zoneSubjCheck != 1) {
						$(_this11).find('span.first').removeAttr('style');
					}
				}
			}
	
			loop11 = $(this).find('li.lisecond');
			loopLen11 = loop11.length;
			for (var i=0; i<loopLen11; i++) {
				_this11 = loop11[i];
				if ($(_this11).find(':checked').length > 0) {
					$(_this11).find('span.second').attr('style', 'color: '+tabbedhue+';');
				}
				else {
					if (zoneSubjCheck != 1) {
						$(_this11).find('span.second').removeAttr('style');
					}
				}
			}
	
			loop11 = $(this).find('li.lithird');
			loopLen11 = loop11.length;
			for (var i=0; i<loopLen11; i++) {
				_this11 = loop11[i];
				if ($(_this11).find(':checked').length > 0) {
					$(_this11).find('span.third').attr('style', 'color: '+tabbedhue+';');
				}
				else {
					if (zoneSubjCheck != 1) {
						$(_this11).find('span.third').removeAttr('style');
					}
				}
			}
		}
	);
	
	
}



