// Globals
// Major version of Flash required
var requiredMajorVersion = 8;
// Minor version of Flash required
var requiredMinorVersion = 0;
// Minor revision of Flash required
var requiredRevision = 0;


// Global array to hold article box heights

//var articleBoxHeights = new Array();
var showsbox = 0;
var salesbox = 0;
$(document).ready(
	function () {
		$('a.closeEl').bind('click', toggleContent);
		$('div.groupWrapper').Sortable(
			{
				accept: 'groupItem',
				helperclass: 'sortHelper',
				activeclass : 	'sortableactive',
				hoverclass : 	'sortablehover',
				handle: 'div.itemHeader',
				tolerance: 'pointer',
				onChange : function(ser)
				{
				serialize();
				},
				onStart : function()
				{
					$.iAutoscroller.start(this, document.getElementsByTagName('body'));
				},
				onStop : function()
				{
					$.iAutoscroller.stop();
				}
			}
		);
		// home page cookie hide boxes
		var tmphomeboxlen;
		try{
			tmphomeboxlen = $.cookie('aahomeboxhide').length;
		}
		catch(err){
			tmphomeboxlen = 0;
 		}
		if (tmphomeboxlen > 0) {
		var boxestohide = $.cookie('aahomeboxhide').split(',');
			for ( var i=0, len=boxestohide.length; i<len; ++i ){
				$('div#' + boxestohide[i] + ' div.itemContent').hide();
				$('div#' + boxestohide[i] + ' div.itemHeader a.closeEl').attr('id', 'closeElup');
			}
			
		}	
			// home page help function
			$('div.helphomepage a').click(function() {
				if ($('div.helpGroupItem').css('display') == 'none') {
					$('div.helpGroupItem').slideDown('slow');
				} else {
					$('div.helpGroupItem').slideUp('slow');
				}
			});
						
			$('div.resethomepage a').click(function() {
				$.cookie('aahomeboxpos', null);
				$.cookie('aahomeboxhide', null);
			});
		// article pages
		// fill global array
		/*
		var i=0;
		for (i=0;i<=10;i++){
			articleBoxHeights[i] = $('#article-' + i).outerHeight();
		}
		$('a.activatearticle').bind('click', toggleArticle);
		$('div.articlecollapse').css('height','165px');
		*/
		// share links
		/*
		$('div.articleshare').hide();
		$('a.articleshare').click(function() {
			var targetContent = $(this).parent('div.articlebar').parent('div.hentry');
			if (targetContent.children('div.articlecontent').children('div.articleshare').css('display') == 'none') {
				targetContent.children('div.articlecontent').children('div.articleshare').slideDown('slow');
			} else {
				targetContent.children('div.articlecontent').children('div.articleshare').slideUp('slow');
			}
		});
		*/
		$('a.articleprint').click(function() {
			$('div.hentry').removeClass('printhide');
			$('div.hentry').addClass('printhide');
			$(this).parent('div.articlebar').parent('div.hentry').children('div.articlecontent').children('div.articlecollapse').css('height', 'auto');
			$(this).parent('div.articlebar').parent('div.hentry').removeClass('printhide');
			window.print();
			return false;
		});
		
		//newsletter subscribe
		$('div.subbox form').hide();
		$('a#subscribex').click(function() {
			$('div.subbox form').slideToggle('slow');
		});

		//comments sections
		/*
		$('div.commentscollapse').hide();
		$('a#activatecomments').click(function() {
			$('div.commentscollapse').slideToggle('slow');
		});
		*/
		
		// about - links page
		$('ul.otherlinks li ul').hide();
		$('ul.otherlinks li a').click(function() {
			var targetContent = $(this).parent('li');
			if (targetContent.children('ul').css('display') == 'none') {
				targetContent.children('ul').slideDown('slow');
			} else {
				targetContent.children('ul').slideUp('slow');
			}
		});
		
		// shows & sales box
		salesbox = $('div#salesbox ul').outerHeight();
		showsbox = $('div#showsbox ul').outerHeight();
		$('div#salesbox ul').css('height','250px');
		$('div#showsbox ul').css('height','250px');
		$('div#salesbox a.salesx').click(function() {
		if ($('div#salesbox ul').css('height') == '250px') {
			$('div#salesbox ul').animate({height: salesbox + 'px'}, 'slow' );
			$(this).html('Hide &#171;');
		} else {
			$('div#salesbox ul').animate({height: '250px'}, 'slow' );
			$(this).html('Show all &#187;');
		}
		});
		$('div#showsbox a.salesx').click(function() {
		if ($('div#showsbox ul').css('height') == '250px') {
			$('div#showsbox ul').animate({height: showsbox + 'px'}, 'slow' );
			$(this).html('Hide &#171;');
		} else {
			$('div#showsbox ul').animate({height: '250px'}, 'slow' );
			$(this).html('Show all &#187;');
		}
		});
		
		// submitProcess
		$("#contactError").hide();
		$("#errorCross").hide();
		$("#contactform").submit(function() {
			$("#contactError").hide();
			$("#errorCross").hide();
			if ($("#email_con").val() == "") {
				$("#contactError").show();
				$("#errorCross").show();
				return false;
			}
		});
	}
);
var toggleContent = function(e)
{
	var targetContent = $('div.itemContent', this.parentNode.parentNode);
	if (targetContent.css('display') == 'none') {
		//targetContent.show();
		targetContent.css('zoom','1');
		targetContent.slideDown('slow', function callback() {serialize();});
		//targetContent.slideDown('slow');
		$(this).attr('id', 'closeEldown');
	} else {
		targetContent.slideUp('slow', function callback() {serialize();});
		$(this).attr('id', 'closeElup');
	}
	return false;
};
var toggleArticle = function(e)
{
	var targetContent = $('div.articlecollapse', this.parentNode.parentNode);
	if (targetContent.css('height') == '165px') {
		targetContent.animate({height: articleBoxHeights[parseInt(targetContent.attr('id').substring(8))] + 'px'}, 'slow' );
		$(this).html('Close article &#171;');
	} else {
		targetContent.animate({height: '165px'}, 'slow' );
		$(this).html('Read more &#187;');
	}
	return false;
};
function serialize(s)
{
	var boxpos;
	var boxhide = "";
	boxpos = $.SortSerialize('sort1').hash.replace(/sort1\[\]=/gi, '').replace(/&/g, ',') + '#' + $.SortSerialize('sort2').hash.replace(/sort2\[\]=/gi, '').replace(/&/g, ',') + '#' + $.SortSerialize('sort3').hash.replace(/sort3\[\]=/gi, '').replace(/&/g, ',');	
	//serial = $.SortSerialize(s);
	//alert(serial.hash);
	$.cookie('aahomeboxpos', boxpos);
	// ids 
	if($('div#news div.itemContent').css('display') == 'none') {
		boxhide += 'news,';
	}
	if($('div#youth-development div.itemContent').css('display') == 'none') {
		boxhide += 'youth-development,';
	}
	if($('div#features div.itemContent').css('display') == 'none') {
		boxhide += 'features,';
	}
	if($('div#links div.itemContent').css('display') == 'none') {
		boxhide += 'links,';
	}
	if($('div#consumer div.itemContent').css('display') == 'none') {
		boxhide += 'consumer,';
	}
	if($('div#breeder-resources div.itemContent').css('display') == 'none') {
		boxhide += 'breeder-resources,';
	}
	if($('div#shows-and-sales div.itemContent').css('display') == 'none') {
		boxhide += 'shows-and-sales,';
	}
	if($('div#about div.itemContent').css('display') == 'none') {
		boxhide += 'about,';
	}
	$.cookie('aahomeboxhide', boxhide);
};