﻿jQuery(document).ready(function() {
	SetRollOvers();
	//SetEnterSubmit();
	SetReturnClick();
	SetExternalLinks();
	SetBiggerClick();
	//Init tiny mce boxes
	if ($(".mceEditor").length > 0) {
		$(".mceEditor").each(function() {
			$(this).tinymce();
		});
	}
	//SetGalleryExpand();
	InitSearchFilter();
	InitGallery();
	SetCouncilMembers();
	SetLoginClicks();
	SetTwoColFormFunctions();
	SetMemberListing();
	SetContentExpanders();
	if ($('.highlight-colour span').length > 0) {
		SetCalenderPopups();
	}
	if ($('#user-image-cropper .profile-pic, #user-image-cropper .profile-logo').length > 0) {
		SetUserImageCroper();
	}
	SetPageHeight();
});



function InitSearchFilter() {
	$("fieldset.search ul").addClass("search-filter");
	$(".filter-holder").hide();
	$(".keyword").focus(function() {
		$(".filter-holder").slideDown("fast");
	});
	/*$(".keyword").blur(function() {
		$(".filter-holder").slideUp();
	});*/
	$("a.close").click(function() {
		$(".filter-holder").slideUp("fast");
		return false;
	});
}

//===================================new gallery
function InitGallery() {

	$(".expander").hide();
	$(".expanding-section h3").hover(
		function() {
			$(this).addClass("hover");
		}, function() {
			$(this).removeClass("hover");
		}
	);
		$(".expanding-section h3").toggle(
		function() {
			$(this).siblings(".expander:first").slideDown();
			$(this).addClass("open");
			// text
			if ($(this).hasClass("photo-h")) {
				$(this).children("span").text("- Hide gallery");
			} else if ($(this).hasClass("video-h")) {
				$(this).children("span").text("- Hide videos");
			} else if ($(this).hasClass("detail-h")) {
				$(this).children("span").text("- Hide details");
			} else if ($(this).hasClass("port-h")) {
				$(this).children("span").text("- Hide items");
			}
		}, function() {
			$(this).siblings(".expander:first").slideUp();
			$(this).removeClass("open");
			// text
			if ($(this).hasClass("photo-h")) {
				$(this).children("span").text("+ Expand gallery");
			} else if ($(this).hasClass("video-h")) {
				$(this).children("span").text("+ Expand videos");
			} else if ($(this).hasClass("detail-h")) {
				$(this).children("span").text("+ Expand details");
			} else if ($(this).hasClass("port-h")) {
				$(this).children("span").text("+ Expand items");
			}
		}
	);

	$(".detail-h").trigger("click");
	if ($(".photo-h").parent().children('ul.gallery').children('li').length < 10)
		$(".photo-h").trigger("click");
	$(".port-h").trigger("click");
	
}

//===================================Gallery toggle
function SetGalleryExpand() {
	if ($('.gallery a, .gallery input').length > 5) {
	    var gallItems = $('.gallery').html();
	    var gallHead = $('.gallery h4').html();
		$('.gallery').html('<div class="gall-slider"></div>');
		$('.gallery').css('position', 'relative');
		$('.gallery').css('padding-bottom', '0px');
		$('.gallery .gall-slider').html('<div class="gall-content">' + gallItems + '</div>');
		$('.gallery h4').remove();
		$('.gallery').prepend('<h4>' + gallHead + '</h4>');
		$('.gallery h4').css('position', 'relative');
		$('.gallery h4').append('<div class="gallery-slide-toggle">Expand</div>');
		$('.gallery-slide-toggle').css('position', 'absolute');
		$('.gallery-slide-toggle').css('bottom', '7px');
		$('.gallery-slide-toggle').css('right', '6px');
		$('.gallery-slide-toggle').css('cursor', 'pointer');
		$('.gallery-slide-toggle').css('padding', '0 15px');
		$('.gallery-slide-toggle').css('background-color', '#00a13b');
		$('.gallery-slide-toggle').css('color', '#fff');
		$('.gallery-slide-toggle').css('font-weight', 'normal');

		$('.gallery-slide-toggle').toggle(function() {
			var numItems = $('.gallery .gall-slider a,.gallery .gall-slider input').length;
			var width = (105) * numItems;
			$('.gall-slider').css("height", "127px");
			$('.gall-slider').css("overflow", "auto");
			$('.gallery .gall-content').css("width", width + "px");
			$('.gallery .end-row').css('margin-right', '5px');
			$(this).html('+ Click to expand');
		}, function() {
			$('.gallery').css("height", "auto");
			$('.gall-content').css("width", "520px");
			$('.gall-content').css("height", "auto");
			$('.gall-slider').css("overflow", "hidden");
			$('.gall-slider').css("height", "auto");
			$('.gallery .end-row').css('margin-right', '0px');
			$(this).html('- Hide');
		});

		$('.gallery-slide-toggle').hover(function() {
			$(this).css('background-color', '#A4E13C');
			$(this).css('color', '#4c8e39');
		}, function() {
		    $(this).css('background-color', '#00a13b');
			$(this).css('color', '#fff');
		});

		$('.gallery-slide-toggle').trigger("click");
	}
}

//========================================================sets extra styles and show/hide for forms
function SetTwoColFormFunctions() {
	
	//REGISTER FORM ONLY
	$('.choose-sector label').click(function() {
		$('.secondary-sector').show();
		var item = $(this).attr('class');
		item = item.replace('first ', '');
		$('.secondary-sector li').show();
		$('.secondary-sector .' + item).hide();
		$('.secondary-sector .' + item + ' input')[0].checked = false;
	});
	//END REGISTER FORM ONLY

	$('.twocol-form ul').each(function() {
		if ($(this).find('.city-selector option[selected]').val() != 'Other - International') {
			$(this).find('.other-address-option').css('display', 'none');
		}
	});

	$('.city-selector').change(function() {
		var list = $(this).parent().parent();
		if ($(this).val() == 'Other - International') {
			list.find('.other-address-option').show();
		} else {
			list.find('.other-address-option').hide();
		}
	});

	$('.twocol-form ul input, .twocol-form ul textarea').focus(function() {
		$(this).addClass('focus');
	});
	$('.twocol-form ul input, .twocol-form ul textarea').blur(function() {
		$(this).removeClass('focus');
	});
}

//================================================================== show loader for member listing
function SetMemberListing() {

	var loaderSrc = '/images/global/bg-loading-directory.gif';
	$('#main .member-list-title .loader-holder').css({'display':'none'});
	$('#main .member-list-title .loader-holder').append('<img src="' + loaderSrc + '" alt="loading please wait" />');

	$('.browse-member-link, .filter-options .filter-button').click(function() {
		//$('.member-list, .profile-listing-copy, .no-results-text').fadeOut(500);
		$('#main .homepage-feature').hide();
		$('.loader-holder').fadeIn(200);
	});
}

//=========================================================expand/contract divs for council members
function SetCouncilMembers() {

	$('.council-item .extra-info').hide();
	$('.council-item').prepend('<a class="slide-toggle closed" title="Show more info" href="#">+ Show/Hide more info</a>');

	$('.council-item .slide-toggle').toggle(
		function() {
			$(this).parent().find('.extra-info').slideDown(350);
			$(this).attr('title', 'Hide more info');
			$(this).addClass('opened').removeClass('closed');
			$(this).parent().addClass('open-item');
		}, function() {
			$(this).parent().find('.extra-info').slideUp(500);
			$(this).attr('title', 'Show more info');
			$(this).addClass('closed').removeClass('opened');
			$(this).parent().removeClass('open-item');
		}
	);

	$('.council-item').click(function() {
		$(this).find('.slide-toggle').trigger('click');
	});
	
	$('.council-item').hover(
		function() {
			$(this).addClass('hot').addClass('hover');
		}, function() {
			$(this).removeClass('hot').removeClass('hover');
		}
	);
}

//============================================================== set page sizes
function SetPageHeight() {
	$("#sidebar, #main").css('height', 'auto');
	//give page half sec before setting page height
	$('body').append('<div id="zxctyysdsj"></div>');
	$('#zxctyysdsj').fadeTo(500, 1, function() { Equalise($("#sidebar, #main")); });
}

//==========================================================Fade for logging in
function SetLoginClicks() {
	if ($('#main-nav a.login-button').length > 0) {
		if ($('fieldset.login span.field-error:visible').length < 1) {
			$('fieldset.login').hide();
		}
		$('#main-nav  a.login-button').toggle(function() {
			$('fieldset.login').fadeIn(500);
			return false;
		}, function() {
			$('fieldset.login').fadeOut(500);
			return false;
		});
	}

	$('.submit-login').click(function() {
		if($('fieldset.login span.error:visible').length < 1)
		{
			$('fieldset.login').fadeOut(500);
		}
	});
}

//============================================================== show popup info for calender items
function SetCalenderPopups() {
	$('.highlight-colour a .inner').css('display', 'none');
	$('.highlight-colour a').hover(
		function() {
			var height = $(this).find('.outer').height();
			$(this).find('.inner').slideDown(200);
		},
		function() {
			$(this).find('.inner').hide();
		}
	);
}


//========================================================= init the slide toggle for content lists
function SetContentExpanders() {
	$('.content-expandable-items .content-item').each(function() {
		$(this).find('.item-title').addClass('close');
		$(this).find('.item-title').prepend('<span>!</span>');
		$(this).find('.content').hide();
	});

	$('.content-expandable-items .content-item .item-title').hover(
		function() { $(this).parent().addClass('hover'); },
		function() { $(this).parent().removeClass('hover'); }
	);

	$('.content-expandable-items .content-item .item-title').click(function() {
		if ($(this).hasClass('open')) {
			$(this).addClass('close');
			$(this).removeClass('open');
			$(this).parent().find('.content').slideUp(350);
		} else {
			$(this).addClass('open');
			$(this).removeClass('close');
			$(this).parent().find('.content').slideDown(350);
		}
	});
}


function SetReturnClick() {
	$("input[type=text], input[type=password], textarea").keypress(function(ev) {
		if (ev.keyCode == 13) {
			ev.preventDefault();
			ev.stopPropagation();

			$(this).parents("fieldset").children("input[type=image], input[type=button], input[type=submit]").trigger("click");
			return false;
		}
	});
}

function SetUserImageCroper() {

	var pic_height, logo_height;

	$('#user-image-cropper img.profile-pic').load(function() {
		pic_height = $('#user-image-cropper img.profile-pic').height();
		SetCropTo(pic_height, logo_height);
	});
	$('#user-image-cropper img.profile-logo').load(function() {
		logo_height = $('#user-image-cropper img.profile-logo').height();
		SetCropTo(pic_height, logo_height);
	});
}

function SetCropTo(pic, logo) {
	if (pic == null || pic == 'undefined') {
		pic = 0;
	}
	if (logo == null || logo == 'undefined') {
		logo = 0;
	}
	
	var MAX_HEIGHT = 260;
	var SET_HEIGHT = Math.min(Math.max(pic, logo), MAX_HEIGHT);
	$('#user-image-cropper').css({ 'overflow': 'hidden', 'height': SET_HEIGHT });
}
