/**
 * Visit Bournemouth
 * @author nprice
 */
 
NewMind.registerNameSpace("NewMind.Site"); //not destructive so can be called multiple times


/**
* Image Captions
* @Requires JQuery.1.2.6
* @author nprice
*
* DESC: Adds image title or alt text captions to all user content images with "caption" in the class
* PRE:	Requires a jquery selector string of the collection of image elements you want captions on
* POST: Wraps each image with a container and adds a caption sibling element. Some default styling is implicit
*		but extra styling should be applied for the look of the caption.
*
* @param String jquery selector of a collection of image elements
**/
NewMind.Site.ImageCaptions = function(strSelector) {

	$(strSelector).each(function(index) {
		var thisCapImg = $(this);
		var imageNote = "";
		var classAry = new Array();
		var bCaptionClass = false;
		var x = 0;

		//split the image style classes by spaces into an array
		classAry = thisCapImg.attr('class').split(" ");

		//loop through the class names checking if "caption" is used
		for (x in classAry) {
			if (classAry[x] === 'caption') {
				bCaptionClass = true;
			}
		}

		//thisCapImg[0] is there to convert to a DOM element because jquery object doesnt have a tagName
		if (bCaptionClass && thisCapImg.attr('tagName') === 'IMG' && (thisCapImg.attr('alt') !== "" || thisCapImg.attr('title') !== "")) {

			var strImageWidth = thisCapImg.css('width');

			//weird IE issue with css method not finding the width using attr method internally
			//if the css width is set then use that otherwise use the width attribute
			if (thisCapImg.css('width') === 'auto') {
				strImageWidth = thisCapImg.attr('width') + 'px';
			} else {
				strImageWidth = thisCapImg.css('width');
			}

			//wrap the image and give the span the class the image had for inherited styling
			//this is especially required for floated images
			$(thisCapImg).wrap('<span class="imgCaptionWrapper ' + thisCapImg.attr('class') + '"></span>');
			$(thisCapImg).parent().css({ 'position': 'relative', 'display': 'block', 'line-height': '1%', 'width': strImageWidth });

			//title is for comments on what's in an image 
			//where as alt is explaining what can be seen in an image for the visually impaired.
			if (thisCapImg.attr('title') !== "") {
				imageNote = thisCapImg.attr('title');
			} else if (thisCapImg.attr('alt') !== "") {
				imageNote = thisCapImg.attr('alt');
			}

			$('<span class="imageCaption"><p>' + imageNote + '</p></span>').insertAfter(thisCapImg);
			thisCapImg.css({ 'display': 'block' }).next()
				.css({ 'width': strImageWidth, 'display': 'block' });
		}
	});
};

/**
* @Requires JQuery.1.2.6
* @author nprice
* 
* Rounded Corners for IE, because it doesnt support the CSS border-radius, 
* we are prepending extra markup to elements that have a rounded class. The extra markup
* can then be used for adding multiple background images.
*/
NewMind.Site.RoundedCorners = function() {
	var strAllCorners = "<span class=\"corner cornertl\"></span><span class=\"corner cornertr\"></span><span class=\"corner cornerbl\"></span><span class=\"corner cornerbr\"></span>";
	var strTopCorners = "<span class=\"corner cornertl\"></span><span class=\"corner cornertr\"></span>";
	var strBottomCorners = "<span class=\"corner cornerbl\"></span><span class=\"corner cornerbr\"></span>";
	$("div.rounded").prepend(strAllCorners);
	$("div.roundedTop, div.hdMainNav ul li.selected").prepend(strTopCorners);
	$("div.roundedBottom").prepend(strBottomCorners);
	$("div.ctl_BannerAd a").prepend(strAllCorners);
	//$('div.prodMltSrch form.searchform').after('<div class="roundedBottom" style="border-top: 5px solid white;"></div>');
};


NewMind.Site.ProdSearchContainers = function() {
	//creating a wrapper for all product searches exept whatsnearbySearch searches(these are done separately)
	//multi searches will all be placed inside the same wrapper
	//non multi there should only be one on a page and this will also go in the wrapper
	var prodSearches = $('#rightCol > div.ctl_ProductSearch'); //direct child
	prodSearches.eq(0).before('<div id="prodSrcWrapper" class="rounded"></div>');
	prodSearches.each(function() { 
		if (!($(this).hasClass('whatsnearbySearch'))){
				$(this).appendTo('#prodSrcWrapper'); 
		}else{
			$(this).before('<div id="prodSrcNBWrapper" class="rounded"></div>');
			$(this).appendTo('#prodSrcNBWrapper'); 
		}
			
	});

};

/**
* @Requires JQuery.1.2.6
* @author nprice
*
* a way to add a styling class for the siblings of the selected first level nav item
*/
NewMind.Site.DisplayGalThumbs = function() {
	var galThumbs = new Array();
	galThumbs[0] = "durleychinebeach.png";
	galThumbs[1] = "bournemouthgardens.png";
	galThumbs[2] = "bournemouthatnight.png";
	galThumbs[3] = "bournemouthtowncentre.png";
	galThumbs[4] = "sandcastlesatbournemouthpier.png";
	galThumbs[5] = "bournemouthpierthepurbecks.png";

	$('div.bHomeGallery ul.galControls li a').each(function(index) {
		//setting the height and width here because the IE pngfix needs
		//these values when creating a span in its place
		$(this).html($(this).text() + '<img width="62px" height="62px" src="/dbimgs/' + galThumbs[index] + '" alt="" />');
	});
};

//
// splitting highlights on homepage
//

NewMind.Site.SplitHomeHighlights = function() {
	//need to be able to distinguish the highlight block with script enabled
	//for styling, scriptEnabled is a generic class so adding a
	//more specific class, we need this because highlight control doesn't
	//have a wrapper.. wrapping it with .js would be slower
$('div.bHighlightsthis').removeClass('bHighlightsthis').addClass('bHighlightsthisscript');
$('div.bHighlightsnext').removeClass('bHighlightsnext').addClass('bHighlightsnextscript');
	$('div.ctl_Highlights ul').each(function(i) {
		var iTotal = $(this).find('li').length;
		if (iTotal > 1) {
			var iSplit = Math.round(iTotal / 2) - 1;
			var objNewList = $('<ul/>');
			objNewList.addClass($(this).attr('class')).addClass('list2');
			$(this).addClass('list1');
			objNewList.append($(this).find("li:gt(" + iSplit + ")"));
			$(this).after(objNewList);
		}
	});
};


/**
* @Requires JQuery.1.2.6
* @author nprice
*
* a way to add a styling class for the previous sibling of the selected first level nav item
* we want to be able to remove the right border on an item that is before the selected item
*/
NewMind.Site.FormatCurrentNavItem = function() {
	var itemSelected = $('div.hdMainNav > ul > li.selected');
	itemSelected.prev().addClass('norightborder');
};
 
/*
NewMind.Site.SearchTabs = function() {
	var $searchTabs = $(".prodMltSrch").find("a.prodMltSrchLnk");
	$searchTabs.wrap('<li class="searchTabsLink"></li>');
	$("div.bAccommSearch").before('<ul class="searchTabsContainer"></ul>');
	$searchTabs.appendTo($("ul.searchTabsContainer"));
};
*/


function correctPNG() { // correctly handle PNG transparency in Win IE 5.5 or higher.       
	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; }

			//taking this out because parentElement is not always found
			//also old and probably should be replaced with parentNode but
			//thats breaking so far with tests, this is only styling change
			//so just do it in CSS if its going to be an issue
			//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 + "\', sizingMethod='scale');\"></span>";
			img.outerHTML = strNewHTML;
			i = i - 1;
		}
	}
}



$(document).ready(
	function() {
		NewMind.Site.ProdSearchContainers();
		NewMind.Site.DisplayGalThumbs();
		NewMind.Site.SplitHomeHighlights();
		NewMind.Site.FormatCurrentNavItem();
		
		//hiding a second english flag because of hosts.. this is only temp while local
		//is being used in cms host names
		//this is zero based
		$('div.ctl_LanguageSelector ul li:eq(1)').hide();
	}
);

//running the functions on window load
$(window).load(
	function() {
		//NewMind.Site.ImageCaptions('#main > .ctl_Content img');
	//NewMind.Site.SearchTabs();
	$("div.ctl_BannerAd a").addClass('rounded');
	}
);


	//IE only functions
	// rounded corners & hover effect on div's in featured slideshow gallery

	if ($.browser.msie ) {
		try {

			//the corners do not position correctly in IE6 even with absolutes and no
			//padding or margin or width issues... its too buggy so they are being
			//used in v7 and above
			if ((parseInt($.browser.version) >= 7)) {
				$(document).ready(NewMind.Site.RoundedCorners);
			}
			$(window).load(correctPNG);
		}
		catch (e) {
			// just in case this fails dont do anything
		}
	}

