﻿$(document).ready(function(){ 
	$('body').attr('class', 'js');

	$('#tabs > ul').tabs();

	$('form.contact_form').validate();
	$('form.nominate_form').validate();
	$('form#browse_collection').validate();
	$('form#browse_subjects').validate();
	$('form#advanced_search').validate();
	$('form#advanced_search_text').validate();
	$('form#formDOI').validate();
	$('form#quick_search').validate();

	$('#button_search_text').click(function() {
		$('form#quick_search').validate();
		$('form#quick_search').submit();
	});

	$('#button_search_text').click(function() {
		$('form#advanced_search_text').validate();
		$('form#advanced_search_text').submit();
	});

	$(".radio[name=daterange]").click(function() {
		$("#quick_search").remove();
		$("form:facet_search").submit();
	});

	$('#img_facet_pdf').click(function() {
		var v = $('#facet_pdf').val();
		v = returnNewValue(v);
		$('#facet_pdf').val(v);
		if ( jvalidateFacets() ) {
			$("#quick_search").remove();
			$("form:facet_search").submit();
		} else {
			$('#facet_pdf').val(returnNewValue(v));
			alert("At least one document type must be selected");
		}
	});	
	$('#img_facet_html').click(function() {
		var v = $('#facet_html').val();
		v = returnNewValue(v);
		$('#facet_html').val(v);
		if ( jvalidateFacets() ) {
			$("#quick_search").remove();
			$("form:facet_search").submit();
		} else {
			$('#facet_html').val(returnNewValue(v));
			alert("At least one document type must be selected");
		}
	});	
	$('#img_facet_word').click(function() {
		var v = $('#facet_word').val();
		v = returnNewValue(v);
		$('#facet_word').val(v);
		if ( jvalidateFacets() ) {
			$("#quick_search").remove();
			$("form:facet_search").submit();
		} else {
			$('#facet_word').val(returnNewValue(v));
			alert("At least one document type must be selected");
		}
	});	
	$('#img_facet_excel').click(function() {
		var v = $('#facet_excel').val();
		v = returnNewValue(v);
		$('#facet_excel').val(v);
		if ( jvalidateFacets() ) {
			$("#quick_search").remove();
			$("form:facet_search").submit();
		} else {
			$('#facet_excel').val(returnNewValue(v));
			alert("At least one document type must be selected");
		}
	});	
	$('#img_facet_pp').click(function() {
		var v = $('#facet_pp').val();
		v = returnNewValue(v);
		$('#facet_pp').val(v);
		if ( jvalidateFacets() ) {
			$("#quick_search").remove();
			$("form:facet_search").submit();
		} else {
			$('#facet_pp').val(returnNewValue(v));
			alert("At least one document type must be selected");
		}
	});	
	$('#img_facet_images').click(function() {
		var v = $('#facet_images').val();
		v = returnNewValue(v);
		$('#facet_images').val(v);
		if ( jvalidateFacets() ) {
			$("#quick_search").remove();
			$("form:facet_search").submit();
		} else {
			$('#facet_images').val(returnNewValue(v));
			alert("At least one document type must be selected");
		}
	});	
	$('#img_facet_audio').click(function() {
		var v = $('#facet_audio').val();
		v = returnNewValue(v);
		$('#facet_audio').val(v);
		if ( jvalidateFacets() ) {
			$("#quick_search").remove();
			$("form:facet_search").submit();
		} else {
			$('#facet_audio').val(returnNewValue(v));
			alert("At least one document type must be selected");
		}
	});	
	$('#img_facet_video').click(function() {
		var v = $('#facet_video').val();
		v = returnNewValue(v);
		$('#facet_video').val(v);
		if ( jvalidateFacets() ) {
			$("#quick_search").remove();
			$("form:facet_search").submit();
		} else {
			$('#facet_video').val(returnNewValue(v));
			alert("At least one document type must be selected");
		}
	});	
	$('#img_facet_other').click(function() {
		var v = $('#facet_other').val();
		v = returnNewValue(v);
		$('#facet_other').val(v);
		if ( jvalidateFacets() ) {
			$("#quick_search").remove();
			$("form:facet_search").submit();
		} else {
			$('#facet_other').val(returnNewValue(v));
			alert("At least one document type must be selected");
		}
	});	

	function returnNewValue(str) {
		var rv = ""
		if ( str.search('true') > -1 ) {
			rv = "false";
		} else {
			rv = "true";
		}
		return rv;		
	}

	function jvalidateFacets(){
		var x = "false"
		if ( $('#facet_pdf').val().search('true') > -1 || $('#facet_html').val().search('true') > -1 || $('#facet_word').val().search('true') > -1 || $('#facet_excel').val().search('true') > -1 || $('#facet_pp').val().search('true') > -1 || $('#facet_video').val().search('true') > -1 || $('#facet_audio').val().search('true') > -1 || $('#facet_images').val().search('true') > -1 || $('#facet_other').val().search('true') > -1     ) {
				x = "true";
		}

		if ( x == "true" ) {
			return true;
		} else {
			return false;
		}
	}


	$('input[type=text]').click (function() {
		value = $(this).val();
		if (value.search('Please enter') > -1) {
			$(this).val('');
		}
		if (value.search('Rhowch destun yma') > -1) {
			$(this).val('');
		}
	});

	$('#comments').click (function() {
		value = $(this).val();
		if (value.search('Please enter') > -1) {
			$(this).val('');
		}
		if (value.search('Rhowch destun yma') > -1) {
			$(this).val('');
		}
	});
	
	//thumb vs list view
	$('#list').click(function() {	
		$('#thumb_view').hide();	
		$('#list_view').show();
		$('#thumbs').html('<a href="javascript:void(0);" title="View results as thumbnails"><img src="/images/thumb.gif" title="View results as thumbnails" alt="View results as thumbnails"width="20" height="20" /></a>');
		$('#list').html('<img src="/images/list_selected.gif" width="20" height="20" title="Viewing results as a list" alt="Viewing results as a list"/>');
	});
	$('#thumbs').click(function() {
		$('#list_view').hide();
		$('#thumb_view').show();
		$('#list').html('<a href="javascript:void(0);" title="View as list"><img src="/images/list.gif" width="20" height="20" title="View as list" alt="View as list" /></a>');
		$('#thumbs').html('<img src="/images/thumb_selected.gif" width="20" height="20" title="Viewing results as thumbnails" alt="Viewing results as thumbnails"/>');
	});

	//expand help tips
	$('ol.list_headings li span').click(function() {
			var parent = $(this).parent('li');
			if (parent.children('p.show').css('display') == 'block') {
				parent.children('.show').slideUp('fast');
				parent.children('span').children('img').attr('src', '/images/close_content.png');
			}
			else {
				parent.children('.show').slideDown('fast');
				parent.children('span').children('img').attr('src', '/images/open_content.png');
			}
	});
	$('ul.list_tips li span').click(function() {
		var parent = $(this).parent('li');
		if (parent.children('p.show').css('display') == 'block') {
			parent.children('.show').slideUp('fast');
			parent.children('span').children('img').attr('src', '/images/close.png');
		}
		else {
			parent.children('.show').slideDown('fast');
			parent.children('span').children('img').attr('src', '/images/open.png');
		}
	});
	
	if (jQuery.browser.msie && jQuery.browser.version <= 6.0) {
		correctPNG();
	}


});

function correctPNG() // correctly handle PNG transparency in Win IE 5.5 & 6.
{
   var arVersion = navigator.appVersion.split("MSIE")
   var version = parseFloat(arVersion[1])
   if ((version >= 5.5) && (document.body.filters)) 
   {
      for(var i=0; i<document.images.length; i++)
      {
         var img = document.images[i]
         var imgName = img.src.toUpperCase()
         if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
         {
            var imgID = (img.id) ? "id='" + img.id + "' " : ""
            var imgClass = (img.className) ? "class='" + img.className + "' " : ""
            var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
            var imgStyle = "display:inline-block;" + img.style.cssText 
            if (img.align == "left") imgStyle = "float:left;" + imgStyle
            if (img.align == "right") imgStyle = "float:right;" + imgStyle
            if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
            var strNewHTML = "<span " + imgID + imgClass + imgTitle
            + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
            + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
            + "(src=\'" + img.src + "\');\"></span>" 
            img.outerHTML = strNewHTML
            i = i-1
         }
      }
   }    
}

