// $Id$
var $j = jQuery.noConflict();

$j(document).ready(function(){

	// cufon
	Cufon.replace('h1, h2, #lc-featured-articles h3, #lc-featured-articles p', { fontFamily: 'Avenir LT Medium', hover: true, hoverables: { a: true } });
	Cufon.replace('.heading-style1', { fontFamily: 'Avenir LT', hover: true, hoverables: { a: true } });
	Cufon.replace('.avenir-lt-medium, button.button2>span>span, #top a', { fontFamily: 'Avenir LT Medium', hover: true, hoverables: { a: true, li: true } });
	Cufon.replace('#home-main h1:not(.nocufon), #home-main h2:not(.nocufon), #home-main h3:not(.nocufon), #home-main p:not(.nocufon)', { fontFamily: 'Avenir LT', hover: true, hoverables: { a: true } });
	Cufon.replace('#feed .ticker h2', { fontFamily: 'Avenir LT', hover: true, hoverables: { a: true } });
	Cufon.replace('.testi', { fontFamily: 'Avenir LT', hover: true, hoverables: { a: true } });
	Cufon.replace('#site-links h3, #pg-links a', { fontFamily: 'Avenir LT', hover: true, hoverables: { a: true } });
	Cufon.replace('#mamatinis h3, #mamatinis h4, #mamatinis p', { fontFamily: 'Avenir LT Medium', hover: true, hoverables: { a: true, li: true } });
	Cufon.replace('.product-mamatini .short-description li, .product-mamatini .add-to-cart, .product-mamatini .add-n-out', { fontFamily: 'Avenir LT', hover: true });
	Cufon.replace('#menu>ol>li', { fontFamily: 'Avenir LT Medium', hover: true, hoverables: { a:false, li:true } });

	// menu
	$j('#menu .has-submenu, #top .account > ul > li').hover(
		function(){
			$j(this).addClass('has-submenu-hover');
			$j('ul', this).show();
			//Cufon.refresh();
			//setTimeout(function(){ Cufon.refresh(); }, 0);
		},
		function(){
			$j(this).removeClass('has-submenu-hover');
			$j('ul', this).hide();
			Cufon.refresh();
			//setTimeout(function(){ Cufon.refresh(); }, 0);
		}
	);

	$j('#sarp-calendar .cal tbody td').live('mouseenter', function() {
		$j(this).find('b').css({'backgroundColor': '#E50078'});
	});
	
	$j('#sarp-calendar .cal tbody td').live('mouseleave', function() {
		$j(this).find('b').css({'backgroundColor': '#0099FF'});
	});
	
	// lightbox
	$j('.lightbox1').colorbox({
		iframe:			true,
		initialWidth:	100,
		initialHeight:	100,
		innerWidth:		460,
		innerHeight:	775,
		opacity:		0
	});
	
	// lightbox
	$j('.lightbox2').colorbox({
		iframe:			true,
		initialWidth:	100,
		initialHeight:	100,
		innerWidth:		640,
		innerHeight:	390,
		opacity:		0
	});
	
	// cycle
	$j('.slideshow .slides').cycle({
		fx:			'fade',
		timeout:	6000,
		pager:		'.slideshow .nav',
		pauseOnPagerHover: true
	});
	
	// sarp date	
	function resetStartDate(date) {
		$j('#mw-sarp-start-m').val(date.getMonth()+1);
		$j('#mw-sarp-start-d').val(date.getDate());
		$j('#mw-sarp-start-y').val(date.getFullYear());
		updateStartDate();
	}
	
	function updateStartDate() {
		$j('#aw_sarp_subscription_start').val(
			$j('#mw-sarp-start-m').val()
			+ '/' +
			$j('#mw-sarp-start-d').val()
			+ '/' +
			$j('#mw-sarp-start-y').val()
		);
		
		renderSchedule(new Date($j('#aw_sarp_subscription_start').val()));
	}

	var tomorrow = new Date();
	tomorrow.setTime(tomorrow.getTime() + (3600*24*1000));
	$j('#mw-sarp-start select').change(function(){
		var start = new Date(
			$j('#mw-sarp-start-y').val(),
			$j('#mw-sarp-start-m').val()-1,
			$j('#mw-sarp-start-d').val()
		);
		
		if (tomorrow <= start) {
			updateStartDate();
		} else {
			alert('Invalid shipment date.');
			resetStartDate();
		}
	});
	if ($j('#mw-sarp-start').length) resetStartDate(tomorrow);
	
	function renderCalendar(d) {		
		var months = ['January','February','March','April','May','June','July','August','September','October','November','December'];
		var sarpCalendar = $j('#sarp-calendar .cals');
		var cal = $j('<div class="cal"></div>').appendTo(sarpCalendar),
			daysInMonth = 32 - new Date(d.getFullYear(), d.getMonth(), 32).getDate(),
			firstOfMonth = new Date(d.getFullYear(), d.getMonth(), 1).getDay(),
			day = 1;
				
		calTable = '<table border="0" cellspacing="0" cellpadding="0">';

		calTable += '<thead><tr class="my"><td colspan="7">';
		calTable += months[d.getMonth()] + ' ' + d.getFullYear();
		calTable += '</td></tr><tr><th class="first">Sun</th><th>Mon</th><th>Tue</th><th>Wed</th><th>Thu</th><th>Fri</th><th class="last">Sat</th></tr></thead>';
		
		for (var r = 0; r < 6; r++) {
			calTable += '<tr>';
			
			for (var c = 0; c < 7; c++) {
				
				if ((r == 0) && (firstOfMonth == c)) {
					calTable += '<td data-date="' + (d.getMonth()+1) + '/' + day + '/' + d.getFullYear() + '">';
					calTable += '1';
					day++;
				} else if ((day > 1) && (day <= daysInMonth)) {
					calTable += '<td data-date="' + (d.getMonth()+1) + '/' + day + '/' + d.getFullYear() + '">';
					calTable += day++;
				} else {
					calTable += '<td>&nbsp;';
				}
				
				calTable += '</td>';
			}
			
			calTable += '</tr>';
		}
		
		calTable += '<tbody>';
		calTable += '</tbody></table>';
		
		cal.append(calTable);
	}

	function renderSchedule(start) {
		$j('#sarp-calendar .cal').remove();
		
		for (var i = 0; i < 3; i++) {
			renderCalendar(new Date(start.getFullYear(), start.getMonth()+i, 1));
		}
		
		$j('td[data-date]').css('cursor', 'pointer');
		
		renderSubscription();
	}
	
	function renderSubscription() {
		var start = new Date($j('#aw_sarp_subscription_start').val()),
			until = new Date($j('td[data-date]:last').attr('data-date'));
			
		if (sarpSubscription > 0) {
			until = new Date(start.getFullYear(), start.getMonth(), start.getDate()+sarpSubscription)
		}

		//alert('start: ' + start);
		//alert('until: ' + until);
			
		// loop thru dates and highlight
		var hited = 0;
		$j('td[data-date]').each(function(){
			var date = new Date($j(this).attr('data-date'));
			
			if (date >= start) {
				if (sarpSubscription > 0) {
					// limited subscription
					if (date < until) {
						$j(this).wrapInner('<b />');
						
						if ((hited % 6) === 0) {
							$j('b', this).append('<span class="cc"></span>');
						}
						
						hited++;
					}
				} else {
					// on-going subscription
					$j(this).wrapInner('<b />');
					
					if ((hited % 6) === 0) {
						$j('b', this).append('<span class="cc"></span>');
					}
					
					hited++;
				}
			}
		});
	}
	
	if ($j('#mw-sarp-start').length) {
		renderSchedule(tomorrow);
		renderSubscription();
		$j('td[data-date]').live('click', function(){
			resetStartDate(new Date($j(this).attr('data-date')));
		});
	}
		
	// textarea max length counter
	$j('textarea.capped').maxlength({
		events: [],                                       // Array of events to be triggerd
		maxCharacters: 500,                               // Characters limit
		status: true,                                     // True to show status indicator bewlow the element
		statusClass: "status",                            // The class on the status div
		statusText: "characters left",                    // The status text
		notificationClass: "notification",                // Will be added when maxlength is reached
		showAlert: false,                                 // True to show a regular alert message
		alertText: "You have typed too many characters.", // Text in alert message
		slider: false                                     // True Use counter slider
	});
	
	// blog sidebar
	if ($j('.is-blog').length) {
		var marginT = 0;
		if ($j('.is-blog .post-list').length > 0) {
			marginT = $j('.is-blog .post-list').offset().top - $j('.is-blog .sidebar').offset().top;
		} else if ($j('.is-blog .entry').length > 0) {
			marginT = $j('.is-blog .entry').offset().top - $j('.is-blog .sidebar').offset().top;
		}
		$j('.is-blog .sidebar').css('margin-top', marginT);
	}
	
	// blog ticker
	$j('#feed .ticker .list').vTicker({ 
		speed: 500,
		pause: 10000,
		animation: false,
		mousePause: true,
		showItems: 1
	});

	// out of stock
	if ($j('body.cms-index-index').length) {
		$j.colorbox({ href: '/skin/frontend/mamatini/default/images/out_of_stock_lightbox.png' });
	}

}); // ready()



