/*
* @Class: Munchkin
* @Description: Munchkin core functions
*/
var Munchkin 		= function () {};
Munchkin.prototype 	= {

	/*
	* @function: initTipsCarousel
	*/
	initTipsCarousel: function () {
		//Xml Tips parser
		var locationStr = location.pathname.substring(0, location.pathname.lastIndexOf('/') + 1);

		$.get(locationStr + 'xml/tips.xml', function (xml) {
			var json = $.xml2json(xml), carrrouselContent = "", urlalias;
			for (i = 0; i < json.tip.length; i += 1) {
				urlalias = json.tip[i].urlalias;
				if (!urlalias) {
					urlalias = "<br />";
				}
				carrrouselContent += "<li><span class=\"photo\"><img src=" + json.tip[i].photo + " width=\"69\" height=\"69\" /></span><h3>" + json.tip[i].name + "</h3><a href='" + json.tip[i].url + "' target='_blank' title=''>" + urlalias + "</a><span><h5>Mommy Blogger Tip</h5><p>" + json.tip[i].textTip + "</p></span></li>\r\n";
			}

			if (json.tip.length > 1) {
				//$('#next-tip').html('<img src="images/button-green-right-active.jpg" title="Next" alt="Next" width="12" height="11" />');
				$('#next-tip img').attr('src', 'images/button-green-right-active.jpg');
			}

			$('#tips-carousel').html(carrrouselContent);
			var tipsCarousel = function (carousel) {
				var tipTrackingBeaconUrl = "http://view.atdmt.com/action/Munchkin_DP_TipScroll1", inc = 0, incTxt = '';
				$('#tipnumber').html("1 of " +  $('#tips-carousel li').size());
				$('<img id="tip-tracking-beacon" height="1" width="1" src="images/clear.gif"/>').appendTo('#squared-three');
				$('#next-tip').bind('click', function () {
					carousel.next();
					if (inc > 0) {
						incTxt = '?' + inc;
					}
					$("#tip-tracking-beacon").remove();
					$('<img id="tip-tracking-beacon" height="1" width="1" src="' + tipTrackingBeaconUrl + incTxt + '"/>').appendTo('#squared-three');
					inc += 1;
					$('#tipnumber').html(carousel.first + " of " +  $('#tips-carousel li').size());

					if (carousel.first === $('#tips-carousel li').size()) {
						$('#next-tip img').attr('src', 'images/button-green-right-inactive.jpg');
					}

					if ($('#tips-carousel li').size() > 1 && carousel.first > 1) {
						$('#prev-tip img').attr('src', 'images/button-green-left-active.jpg');
					}

					return false;
				});

				$('#prev-tip').bind('click', function () {
					carousel.prev();
					if (inc > 0) {
						incTxt = '?' + inc;
					}
					$("#tip-tracking-beacon").remove();
					$('<img id="tip-tracking-beacon" height="1" width="1" src="' + tipTrackingBeaconUrl + incTxt + '"/>').appendTo('#squared-three');
					inc += 1;
					$('#tipnumber').html(carousel.first + " of " +  $('#tips-carousel li').size());

					if (carousel.first === 1) {
						$('#prev-tip img').attr('src', 'images/button-green-left-inactive.jpg');
					}

					if ($('#tips-carousel li').size() > carousel.first) {
						$('#next-tip img').attr('src', 'images/button-green-right-active.jpg');
					}

					return false;
				});
			};

			//console.log($('#tips-carousel li').size());
			$('#tips-carousel').jcarousel({
				scroll: 1,
				initCallback: tipsCarousel,
				buttonNextHTML: null,
				buttonPrevHTML: null
			});

		});

	},

	/*
	* @function: initBlockPopUp
	*/
	initGuidedTourVideo: function (self) {
		$('#showGuidedTour').click(function () {

			$('#vzaar_media_player').html(self.getGuidedTourVideo());

			$.blockUI.defaults.css = {};
			$.blockUI({
				message: $('#guided-tour'),
				css: { top: '135px', left: ($(window).width() - 569) / 2 + 'px' }
			});
			$.get(guidedToursClickURL);
		});

		$('#guided-tour .guided-tour-close').click(function () {
			//Delete the video DOM
			$('#guided-tour').css({'display': 'none'});
			$('.embed-field').css({'display': 'none'});
			$.unblockUI();
			$('#vzaar_media_player').html("");
		});
	},

	/*
	* @function: getFlashVideo
	* @params:
	* @return:
	*/
	getGuidedTourVideo: function () {
		var video = "<object id=\"video\" width=\"576\" height=\"323\"  type=\"application/x-shockwave-flash\" data=\"http://vzaar.com/videos/72925.swf\"><param name=\"movie\" value=\"http://vzaar.com/videos/72925.swf\"><param name=\"allowScriptAccess\" value=\"always\"></param><param name=\"allowFullScreen\" value=\"true\"></param><param name=\"movie\" value=\"http://vzaar.com/videos/72925.swf\"></param><param name=\"wmode\" value=\"transparent\"></param><param name=\"flashvars\" value=\"autoplay=true\"></param><embed src=\"http://vzaar.com/videos/72925.swf\" type=\"application/x-shockwave-flash\" wmode=\"transparent\" width=\"576\" height=\"323\"  allowScriptAccess=\"always\" allowFullScreen=\"true\" flashvars=\"autoplay=true\"></embed><video width=\"576\" height=\"323\"  poster=\"http://vzaar.com/videos/72925.frame\"></video></object>";

		return video;
	},

	/*
	* @function: initBlockPopUp-retailer
	*/
	initRetailer: function () {
		$('#showRetailer').click(function () {
			$.blockUI.defaults.css = {};
			$.blockUI({
				message: $('#retailer-popup'),
				css: { top: '135px', left: ($(window).width() - 569) / 2 + 'px' }
			});
			$.get(buyitnowCI_URL);
		});

		$('#retailer-popup .retailer-close').click(function () {
			$.unblockUI();
		});
	},


	/*
	* @function: staticGuidedTourVideo
	*/
	staticGuidedTourVideo: function (self) {
		$('#vzaar_media_player').html(self.getGuidedTourVideo());

		$.blockUI.defaults.css = {};
		$.blockUI({
			message: $('#guided-tour'),
			element: $('.main-container').get(0),
			centerX: false,
			centerY: false
			//showOverlay: false
		});

		$('#guided-tour .guided-tour-close').click(function () {
			$.unblockUI({
				element: $('.main-container').get(0)
			});
			$('#vzaar_media_player').html("");
		});
	},

	/*
	* @function: initProductFeaturesPopup
	*/
	initProductFeaturesPopup: function (obj) {

		//Left Preview Click Event
		$('.product-feature-item a.open-feature').click(function () {
			if(popupContainerClass = $(this).attr('class').replace('open-feature', '')) {
				popupContainerClass = jQuery.trim(popupContainerClass);
				$('.product-features-image a.' + popupContainerClass).trigger('click');
			}

			return false;
		});

		//Right Icon Click Event
		$('.product-features-image a.open-feature').click(function () {

			//Reset Popups
			obj.resetPopups(this);

			if (!$(this).parent().attr('style')) {
				obj.changeIconPosition(this); //Fix green Icon when orange is clicked
				obj.openPopup(this);
			}
			//Close
			else {
				$(this).parent().removeAttr('style');
			}

			return false;
		});

		//Automatic Trigger the Green Popup
		$('.product-features-image a.tmb-product-features-popup-green').trigger('click');
	},

	/*
	* @function: changeIconPosition
	*/
	changeIconPosition: function (elm) {
		var popupContainerClass = $(elm).attr('class').replace('open-feature', '');
		popupContainerClass 	= jQuery.trim(popupContainerClass);

		switch (popupContainerClass) {
		case 'tmb-product-features-popup-orange':
			$('#product-feature-green-icon-on-image').removeClass('position3').removeClass('position4').addClass('position2');
			break;
		case 'tmb-product-features-popup-green':
			$('#product-feature-green-icon-on-image').removeClass('position2').removeClass('position3').removeClass('position4');
			break;
		case 'tmb-product-features-popup-blue':
			$('#product-feature-green-icon-on-image').removeClass('position2').removeClass('position3').addClass('position4');
			break;
		default:
			$('#product-feature-green-icon-on-image').removeClass('position2').removeClass('position4').addClass('position3');
			break;
		}
	},

	/*
	* @function: openPopup
	*/
	openPopup: function (elm) {
		var popupContainerClass = $(elm).attr('class').replace('open-feature', '');
		popupContainerClass = jQuery.trim(popupContainerClass);

		$(elm).parent().css({'background': 'url(images/' + popupContainerClass + '.png) no-repeat top left', 'z-index': '1000'});

		//Change Previews Background
		$('.product-features-text .' + popupContainerClass).css({'background': 'url(images/tmb-product-feature-shadow.png) 0 0 no-repeat'});

		//Change Diaper Background
		if (popupContainerClass === 'tmb-product-features-popup-orange') {
			//$('#product-feature-green-icon-on-image').css({'top': '160px', 'right':'162px'});
		}
		else if (popupContainerClass === 'tmb-product-features-popup-green') {
			//$('#product-feature-green-icon-on-image').css({'top': '160px', 'right':'162px'});
			//console.log('reset green');
		}

		$('.product-features-image .product-features-popups').css({'background': 'url(images/' + popupContainerClass + '-diaper.png) 0 0 no-repeat'});

		//Change Previews Icon
		currIcon = $('.product-features-text .' + popupContainerClass + ' .product-feature-icon img').attr('src');
		if (currIcon) {
			var minusIcon = currIcon.replace('.png','-minus.png');
			$('.product-features-text .' + popupContainerClass + ' .product-feature-icon img').attr('src', minusIcon);
		}
	},

	/*
	* @function: resetPopups
	*/
	resetPopups: function (elm) {
		$('.product-features-image .product-features-popups div').filter(':not(#' + $(elm).parent().attr('id') + ')').removeAttr('style');

		//Reset Minus Icon
		$('.product-features-text .product-feature-item .product-feature-icon img').each(function () {
			attrib = $(this).attr('src');
			if (attrib) {
				if (attrib.indexOf('minus')>=0) {
					$(this).attr('src', attrib.replace('-minus',''));
				}
			}
		});

		//Remove Shadow background
		$('.product-features-text .product-feature-item').removeAttr('style');
	}
};

		/*
	* @function: submit tip box
	*/

function MM_showHideLayers() { //v9.0
	var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3)
  with (document) if (getElementById && ((obj=getElementById(args[i]))!=null))
    { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }
}

if (document.images) {
   img1 = new Image();
   img2 = new Image();
   img3 = new Image();
   img4 = new Image();
   img5 = new Image();
   img6 = new Image();
   img7 = new Image();
   img8 = new Image();
   img9 = new Image();
   img10 = new Image();
   img11 = new Image();
   img12 = new Image();
   img13 = new Image();
   img14 = new Image();
   img15 = new Image();
   img16 = new Image();
   img17 = new Image();
   img18 = new Image();
   img19 = new Image();
   img20 = new Image();
   img1.src = "images/tmb-product-features.png";
   img2.src = "images/tmb-product-features-bg.png";
   img3.src = "images/tmb-product-features-popup-blue-diaper.png";
   img4.src = "images/tmb-product-features-popup-green-diaper.png";
   img5.src = "images/tmb-product-features-popup-orange-diaper.png";
   img6.src = "images/tmb-product-features-popup-pink-diaper.png";
   img7.src = "images/tmb-product-features-popup-purple-diaper.png";
   img8.src = "images/tmb-product-features-popup-blue.png";
   img9.src = "images/tmb-product-features-popup-green.png";
   img10.src = "images/tmb-product-features-popup-orange.png";
   img11.src = "images/tmb-product-features-popup-pink.png";
   img12.src = "images/tmb-product-features-popup-purple.png";
   img13.src = "images/button-enter-now-hover.gif";
   img14.src = "images/button-orange-hover.png";
   img15.src = "images/button-pink-hover.png";
   img16.src = "images/button-green-hover.png";
   img17.src = "images/button-green-right-inactive.jpg";
   img18.src = "images/button-green-left-inactive.jpg";
   img19.src = "images/button-green-right-active.jpg";
   img20.src = "images/button-green-left-active.jpg";
}

