/**
 * @author gbhatnag
 */

// global array
var selected_tags = new Array();
	
$(function() {  // onready
	
	/*
   * managing behavior for item action icons
   */
	$( ".article_actions ul li" ).mouseover( function() {
		$(this).addClass( "ui-state-hover" );
	}).mouseout( function() {
		$(this).removeClass( "ui-state-hover" );
	});
	
	$( ".article_actions ul li:first-child" ).toggle( function() {
		$(this).addClass( "ui-state-highlight" );
		$(this).addClass( "t_Favorites" );
	}, function() {
		$(this).removeClass( "ui-state-highlight" );
		$(this).removeClass( "t_Favorites" );	
	});
	
	/*
	 * managing behavior for tags
	 */
	$( ".tag_link" ).click( function() {
		if( $(this).hasClass("tag_highlighted") ) {
			/* de-selecting a tag */
			
			// remove from selected_tags array
			remove_tag(this.id);
			
			// remove highlighting
			$( "." + this.id ).removeClass("tag_highlighted");
			
			// filter with new tagged state
			if ($(this).hasClass("union")) {
	  		filter_posts_union();
	  	} else {
				filter_posts_intersection_deselected();
			}
		} else {
			/* selecting a tag */
			
			// add to selected_tags array
			selected_tags.push(this.id);
			
			// add highlighting
			$( "." + this.id ).addClass("tag_highlighted");
			
			// filter with new tagged state
			if ($(this).hasClass("union")) {
	  		filter_posts_union();
	  	} else {
				filter_posts_intersection_selected(this.id);
			}
		}
	});
	
	/*
	 * dialog for adding content
	 */
	$("#add_form").dialog({
		bgiframe: true,
		autoOpen: false,
		height: 600,
		width: 530,
		modal: true,
		title: "Add Content",
		buttons: {
			'Add Content': function(){
				addContent();
			},
			'Cancel': function(){
				$(this).dialog('close');
			}
		}
	});

	$( "#add_link" ).click( function() {
		$("#add_form").dialog('open');
		return false;
	});
	
	/*
	 * dialog for exporting content
	 */
	$("#export_form").dialog({
		bgiframe: true,
		autoOpen: false,
		height: 320,
		width: 520,
		modal: true,
		title: "Export Citations",
		buttons: {
			'Export Directly to RefWorks': function(){
				$(this).dialog('close');
			},
			'Download Citations': function(){
				$(this).dialog('close');
			},
			'Cancel': function(){
				$(this).dialog('close');
			}
		}
	});

	$( "#export" ).click( function() {
		$("#export_form").dialog('open');
		return false;
	});
	
	/*
	 * add content form
	 */
	$( ".type_option" ).click( function() {
		if( $(this).hasClass("tag_highlighted") ) {
			// remove highlighting
			$( "." + this.id ).removeClass("tag_highlighted");
		} else {
			/* selecting a tag */
			$("tr.form_row").show();
			$("tr.content").hide();
			$(".type_option").removeClass("tag_highlighted");
			
			$("tr."+this.id).show();
			$( this ).addClass("tag_highlighted");
		}
	});
	
	/*
	 * dealing with '#' links
	 */
	$( "a[href='#']" ).click( function() {
		return false;
	});
	
	/*
	 * notice
	 */
	var flip=0;
	$("p#note_header").click( function() {
		$("div#note_content").slideToggle("normal");
		if( flip++ % 2 == 0 ) {
			$("span#info_switch_icon").removeClass("ui-icon-triangle-1-e");
			$("span#info_switch_icon").addClass("ui-icon-triangle-1-s");
		} else {
			$("span#info_switch_icon").removeClass("ui-icon-triangle-1-s");
			$("span#info_switch_icon").addClass("ui-icon-triangle-1-e");
		}
	});
});

function filter_posts_union() {
	// if we have no selected tags, show all posts
	if( selected_tags.length == 0 ) {
		$("div.article_post").slideDown("normal");
	} else {
		// there are tags selected, show only those that are tagged
		$("div.article_post").each( function( index ) {
			isShown = false;
			for( i in selected_tags ) {
				if( $(this).hasClass(selected_tags[i]) ||
					$(this).find( "." + selected_tags[i] ).get(0) ) {
					$(this).slideDown("normal");
					isShown = true;
				}
			}
			if( !isShown ) {
				$(this).slideUp("normal");
			}
		});
	}
}

function filter_posts_intersection_selected(tag){
	$("div.article_post:visible").each( function( index ) {
		if( !$(this).hasClass(tag) &&
		    !$(this).find( "." + tag ).get(0) ) {
			$(this).slideUp("normal");
		}
	});
}

function filter_posts_intersection_deselected(){
	// if we have no selected tags, show all posts
	if (selected_tags.length == 0) {
  	$("div.article_post").slideDown("normal");
  } else {
		// there are tags selected, show only intersections
  	$("div.article_post").each( function( index ) {
			isShown = false;
			for( i in selected_tags ) {
				if( $(this).hasClass(selected_tags[i]) ||
					$(this).find( "." + selected_tags[i] ).get(0) ) {
					isShown = true;
				} else {
					isShown = false;
					break;
				}
			}
			if( isShown ) {
				$(this).slideDown("normal");
			}
		});
  }
}

function remove_tag( tag_id ) {
	// find this tag
	var tag_index = null;
	for( x in selected_tags ) {
		if( selected_tags[x] == tag_id ) {
			tag_index = x;
			break;
		}
	}
	
	// slice around tag_index
	if( tag_index != null ) {
		front = selected_tags.slice(0, tag_index);
		end   = selected_tags.slice(tag_index + 1);
		selected_tags = front.concat(end);
	} else {
		alert( "Error: can't find selected tag.." );
	}
}

function addContent() {
	if( $("#page_id").hasClass( "instructor1" ) ) {
		window.location = "./instructor2.html";
	} else if( $("#page_id").hasClass( "librarian1" ) ) {
		window.location = "./librarian2.html";
	} else {
		$("#add_form").dialog('close');
	}
}
