﻿$(document).ready(function() {
	/*
	if ($('#ad_wrap').length > 0) {
		$('#ad_wrap').carousel({
			prevBtnInsert: 'appendTo',
			autoSlide: true,
			autoSlideInterval: 7000,
			//effect: 'slide',
			effect: 'fade',
			//animSpeed: 'slow',
			loop: true
		});
	}

	$('#ad_wrap .carousel-next').addClass('clearAfter');
	//*/

	/* tipsy tooltips */
	if ($('a.landing-tip').length > 0) { $('#center_col a.landing-tip').tipsy({ gravity: 'w', fade: true, opacity: 0.9, html: true }); }
	/* ------------------------------------------------------------------- */

	var admin = new Admin();

	// check for IE7
	if (admin.testIE7()) {
		// adjust layout margins for IE7 only
		//if ($('#content_wrap').length > 0) $('#content_wrap').css('margin-top', '5px');
		$('#top_nav').css('margin-top', '10px');
		if ($('#content_bg_wrap').length > 0) $('#content_bg_wrap').css('margin-top', '0px');
		if ($('#ad_wrap').length > 0) $('#ad_wrap').css('padding-top', '10px');
		$('#section_head').css('padding-top', '5px');
	}

	/* login pop-up */
	admin.setupLoginPop();

	/*-- Navigation Adjustments --*/
	var contentW = $('#content_wrap').width();
	/*$('#nav_list').children('li').each(function() {
	var ul = $(this).children('ul:first');
	var pos = ul.position();
	var off = $(this).offset();
	var ulW = ul.width();

	if (!admin.testIE7()) {
	if ((off.left + ulW) > contentW) {
	if ($(this).children('ul:first').length > 0) $(this).children('ul:first').css('right', -15);
	}
	else {
	if ($(this).children('ul:first').length > 0) $(this).children('ul:first').css('left', pos.left - 15);
	}
	}
	else {
	if ($(this).children('ul:first').length > 0) $(this).children('ul:first').css('margin-top', 23);
	}
	});*/
	$('#nav_list').children('li').each(function() {
		var ul = $(this).children('ul:first');
		var pos = ul.position();
		var off = $(this).offset();
		var ulW = ul.width();

		if (!admin.testIE7()) {
			if ((off.left + ulW) > contentW) {
				if (ul.length > 0) ul.css('right', -15);
			}
			else {
				if (ul.length > 0) ul.css('left', pos.left - 15);
			}
		}
		else {
			if (ul.length > 0 && $(this).hasClass('nav_selected')) ul.css('margin-top', 23).css('margin-left', -115);
		}
	});
	/*----------------------------*/

	/* Set section colors - links, headers, etc */

	var cntSectionColor;
	var mainContentSel = $('#main_content');
	if (mainContentSel.hasClass('section_exchange')) {
		cntSectionColor = admin.EXCHANGE_SECTION_CLR;
		$('#main_content #right_content').find("h1, h2, h3, h4, h5, h6, a[href]").each(function() {
			if (!$(this).attr('style')) { $(this).css('color', admin.EXCHANGE_SECTION_CLR); }
		});
	}
	if (mainContentSel.hasClass('section_travel')) {
		cntSectionColor = admin.TRAVEL_SECTION_CLR;
		$('#main_content #right_content').find("h1, h2, h3, h4, h5, h6, a[href]").each(function() {
			if (!$(this).attr('style')) { $(this).css('color', admin.TRAVEL_SECTION_CLR); }
		});
	}
	if (mainContentSel.hasClass('section_rentals')) {
		cntSectionColor = admin.RENTALS_SECTION_CLR;
		$('#main_content #right_content').find("h1, h2, h3, h4, h5, h6, a[href]").each(function() {
			if (!$(this).attr('style')) { $(this).css('color', admin.RENTALS_SECTION_CLR); }
		});
	}
	if (mainContentSel.hasClass('section_management')) {
		cntSectionColor = admin.MANAGEMENT_SECTION_CLR;
		$('#main_content #right_content').find("h1, h2, h3, h4, h5, h6, a[href]").each(function() {
			if (!$(this).attr('style')) { $(this).css('color', admin.MANAGEMENT_SECTION_CLR); }
		});
	}
	if (mainContentSel.hasClass('section_about-us')) {
		cntSectionColor = admin.ABOUTUS_SECTION_CLR;
		$('#main_content #right_content').find("h1, h2, h3, h4, h5, h6, a[href]").each(function() {
			if (!$(this).attr('style')) { $(this).css('color', admin.ABOUTUS_SECTION_CLR); }
		});
	}
	if (mainContentSel.hasClass('section_my-tpi')) {
		cntSectionColor = admin.MYTPI_SECTION_CLR;
		$('#main_content #right_content').find("h1, h2, h3, h4, h5, h6, a[href]").each(function() {
			if (!$(this).attr('style')) { $(this).css('color', admin.MYTPI_SECTION_CLR); }
		});
	}
	if (mainContentSel.hasClass('section_other')) {
		cntSectionColor = admin.OTHER_SECTION_CLR;
		$('#main_content #right_content').find("h1, h2, h3, h4, h5, h6, a[href]").each(function() {
			if (!$(this).attr('style')) { $(this).css('color', admin.OTHER_SECTION_CLR); }
		});
	}

	/* set external links to open in new window */
	function isNotDomain(n, element) {
		return ($(element).attr('href').indexOf('dev.merchant.tradingplaces.com') == -1
				&& $(element).attr('href').indexOf('condo.tradingplaces.com') == -1
				&& $(element).attr('href').indexOf('www.tradingplaces.com') == -1
				&& $(element).attr('href').indexOf('tradingplaces.com') == -1
				&& $(element).attr('href').indexOf('beta.testing.tradingplaces.com') == -1
				&& $(element).attr('href').indexOf('live.beta.tradingplaces.com') == -1);
	}

	var externalLinks = $("#main_content a:[href^='http://']");
	//var externalLinks = $("#main_content a[href]");
	var filteredExtLinks = externalLinks.filter(isNotDomain);
	filteredExtLinks.attr('target', '_blank');

	/* Catch any existing testing links that might be out there and
	point them to the live servers */
	$("#main_content a[href]").each(function() {
		var hrefVal = $(this).attr('href');
		if (hrefVal.indexOf('dev.merchant') != -1) {
			$(this).attr('href', hrefVal.replace('dev.merchant', 'condo'));
		}
	});

	var externalImgs = $('#main_content img[src]');
	externalImgs.each(function() {
		var imgSrc = $(this).attr('src');
		if (imgSrc.indexOf('testing.cmsfrontend') != -1) {
			$(this).attr('src', imgSrc.replace('testing.cmsfrontend', 'www'));
		}
	});

	/* Fancy title inside of a page by classname */
	$('.fancyFontTitle').each(function(n, e) {
		var e = $(e);
		e.html("<img src=\"/services/drawing.asmx/Text?fontName=Ambiance%20BT&textToWrite=" + encodeURIComponent(e.text()) + "&fontSize=28&fontColor=" + cntSectionColor.replace('#', '') + "&hideWatermark=1\" alt=\"" + e.text() + "\" />")
		 .css({ 'margin-bottom': '-15px', 'padding-bottom': '0' });
	});

	$('div.hdFanceyFontClass, div.exFanceyFontClass, div.maFanceyFontClass').each(function(n, e) {
		var e = $(e);
		e.html("<img src=\"https://www.tradingplaces.com/services/drawing.asmx/Text?fontName=Futura%20Md%20BT&textToWrite=" + encodeURIComponent(e.text().toUpperCase()) + "&fontSize=24&fontColor=" + cntSectionColor.replace('#', '') + "\" alt=\"" + e.text() + "\" />");
	});
});

