jQuery.noConflict();

var svbUtils = {
	init: function () {
		// onload functions
		svbUtils.equalHeight(jQuery('.equalheight .equal'));
		svbUtils.initGallery();
		svbUtils.DefaultSearchText(jQuery('.top-search-field, .pagesearch'));
		//svbUtils.toggleEditMode();
		svbUtils.pageSearch.init();	
	},
	GalleryPrevFunctions: function (carousel, id) {
		//Looks for the amount of gallery items and adds navigations controls
		if (jQuery('.galleryitem', carousel).length > 1) {
			jQuery(carousel).append('<div class="gallery-navigation"><a class="gallery-nav-prev">Prev</a><div class="gallery-nav-stepper"></div><a class="gallery-nav-next">Next</a></div>');
		}
		var captionBox = jQuery(this).parents('.carousel').find('.captionbox');
		captionBox.html(jQuery(this).find('h4,p').clone().fadeIn());

		var url = jQuery(this).find('a:first').attr('href');
		if (url !== undefined) {
			captionBox.find('p').wrap('<a href="' + url + '"></a>');
		}
	},
	initGallery: function (carousel, id) {

		//Image carousel
		jQuery('.carousel').each(function (i) {
			jQuery(this).addClass('carousel' + i),
                jQuery(this).find('.carousel-content').cycle({
                	fx: 'scrollHorz',
                	rev: 0,
                	timeout: 10000,
                	before: svbUtils.GalleryPrevFunctions(this, i),
                	//after: onAfter,
                	next: '.carousel' + i + ' .gallery-nav-next',
                	prev: '.carousel' + i + ' .gallery-nav-prev',
                	pause: 1,
                	pager: '.carousel' + i + ' .gallery-nav-stepper'
                });
		});
	},
	equalHeight: function (group) {

		var tallest = 0;
		group.each(function () {
			var thisHeight = jQuery(this).height();
			if (thisHeight > tallest) {
				tallest = thisHeight;
			}
		});
		group.css('min-height', tallest);

	},
	DefaultSearchText: function (group) {

		group.each(function () {
			//Default search text handler
			var DefaultTextHolder = jQuery(this).attr('title');
			jQuery(this).focus(function (srcc) {
				jQuery(this).addClass('fieldActive');
				jQuery(this).val('');
			});
			jQuery(this).blur(function () {

				if (jQuery(this).val() == '') {
					jQuery(this).removeClass('fieldActive');
					jQuery(this).val(DefaultTextHolder);
				}
			});
			jQuery(this).blur();
		});

	},
	pageSearch:
	{
		init: function () {
			var $txtSearch = jQuery('#txtSearch'); //Looks for the inputfield for the searchstring
			if ($txtSearch.length) {
				$txtSearch.keydown(function (event) {
					if (event.keyCode == '13') { // Catch the 'enter' event
						svbUtils.pageSearch.doSearch($txtSearch); //Do the search
						return false;
					};
				});
				var $btnSearch = jQuery('#btnSearch'); //Looks for the searchbutton
				$btnSearch.click(function () { // Set click action for the button
					svbUtils.pageSearch.doSearch($txtSearch); //Do the search
					return false;
				});
			}
		},
		doSearch: function ($txtSearch) {
			if ($txtSearch.val() != $txtSearch.attr('title')) { // If the searchstring is different from the default helptext
				var searchUrl = jQuery('#searchUrl').val(); //Look for the url

				if (searchUrl != '') {
					window.location.href = searchUrl + jQuery('#txtSearch').val(); //load the searchpage with the new query
				}
			};
		}
	},
	toggleEditMode: function () {

		var togglers = jQuery('.toogleedit');

		togglers.each(function () {
			jQuery(this).find('dt').click(function () {
				jQuery(this).parent().toggleClass('off');
			})
		})

	},
	setNewHeightContentPage: function () {
		// Check if editormode
		if (typeof Sitecore == 'undefined' && !jQuery('body').hasClass('preview')) {

			var outerobject;
			var rowamount;
			if (jQuery('.row').hasClass('row-nine')) {
				outerobject = jQuery('.row-nine');
				rowamount = 3;
			} else if (jQuery('.row').hasClass('equalheightFP')) {
				outerobject = jQuery('.equalheightFP');
				rowamount = 4;
			}
			else if (jQuery('.row').hasClass('row-six')) {
				outerobject = jQuery('.row-six');
				rowamount = 2;
			} else if (jQuery('.row-twelve').children().hasClass('sitemap')) {
				outerobject = jQuery('.sitemap');
				rowamount = 4;
			} else { return; }

			outerobject.each(function (index) {
				var object;
				if (outerobject.eq(index).hasClass('listcontainer')) {
					if (outerobject.eq(index).find('.sbvlist').parent().hasClass('spotlist')) {
						outerobject.eq(index).find('.sbvlist').each(function (index2) {
							object = outerobject.eq(index).find('.sbvlist').eq(index2).children();
							runEqualHeight(object);
						});

					} else { return; }
				} else if (outerobject.eq(index).hasClass('spotcontainer')) {
					object = outerobject.eq(index).children();
					runEqualHeight(object);
				} else if (outerobject.eq(index).hasClass('equalheightFP')) {
					object = outerobject.eq(index).children();
					runEqualHeight(object);
				} else if (outerobject.eq(index).hasClass('sitemap')) {
					object = outerobject.eq(index).children().children().children().children();
					runEqualHeight(object);
				} else { return; }
				function runEqualHeight(object) {
					var d = 0;
					var n = (object.length) - 1;
					if (object.length > 1) {
						while (d <= n) {
							var objectHeights = new Array();
							for (var i = 0; i <= (rowamount - 1); i++) {
								objectHeights[i] = object.eq(d + i).height();

							}
							var newHeight = objectHeights.sort(function (a, b) { return b - a; })[0];

							for (var j = 0; j <= (rowamount - 1); j++) {
								object.eq(d + j).css('height', newHeight);
							}

							d = d + rowamount;
						}
					}
				}
			});
		}
	}
};



jQuery(document).ready(function () {
	svbUtils.init();
});

/*Load events*/
jQuery(window).load(function () {
	svbUtils.setNewHeightContentPage();
});

