// =====================
// = Frogzog. Pond JS. =
// =====================

// ie6?
var ie6 = false;

// 88 miles per hour
var timeCode    = '';
var timeCurrent = 0;
var timeMachine = 'DELOREAN'; // can you fix the flux capacitor?
var timeZones   = ['morning', 'day', 'evening', 'night'];
var bubbleHint  = true;

// *************************** ON DOCUMENT READY *************************** //
$(document).ready(function() {
	// determine if we have to dumb everything down for that pile of shit known as ie6 or not.
	if ($.browser.msie && $.browser.version < 7) ie6 = 'unfortunately, yes'; 
	
	// merchant body class by time of day
	timeCurrent = fzp_timeCurrent();
	$('#pond').removeClass('morning day evening night').addClass(timeZones[timeCurrent]);
	
	// merchant controls
	$('#merchantControls').css("top", "-50px");
	$('#merchantControlsToggle').toggle( 
		function() { $(this).blur(); $('#merchantControls').animate({top: '0px'}, 650, 'easeOutBounce'); return false; }, 
		function() { $(this).blur(); $('#merchantControls').animate({top: '-50px'}, 250, 'jswing'); return false; }
	);

	// ! **** BUBBLE HINTS ****
	$('.bubbleHintTrigger').focus(function() { if (!bubbleHint) { bubbleHint = true; return; } fzp_bubbleHintShow($(this)); }).blur(function() { setTimeout('fzp_bubbleHintHide()', 100); }); // Show-Hide Suggestion Bubble
	$('.suggestions li').click(function() { bubbleHint = false; var pieces = $(this).parent('ul').attr('id').split('Suggestions'); var target = $('#' + pieces[0]); target.attr('value', $(this).text()).removeClass('shadowInputTextHint').focus();; }); // clickable suggestions
	$('.browseLink').click(function() { bubbleHint = false; $('#keyword').attr('value', 'Browse').removeClass('shadowInputTextHint').focus(); }); // clickable browse link

	// ! **** SEARCH BAR STUFF ****
	// If search is submitted with hints in inputs, remove them
	$('#fz_search').submit( function(){
		if ( $('#keyword').val() == $('#keyword').attr('title') ) { $('#keyword').val('');  } // check keyword field
		if ( $('#location').val() == $('#location').attr('title') ) { $('#location').val('');  } // check location field
	});
	// find it button rollover
	if (!ie6){ // ignore for ie6.. cause it sucks.
		$('#searchBar .submit').mouseover( function(){ $('#searchBar').addClass("mouseover"); } ).mouseout( function(){ $('#searchBar').removeClass("mouseover"); } );
	}
	$('.autoShadowInputTextHint').each(function() { // hint form fields
		if (!$(this).val()) $(this).val($(this).attr('title')).addClass('shadowInputTextHint').focus(function() { if ($(this).val() == $(this).attr('title')) $(this).val('').removeClass('shadowInputTextHint'); }).blur(function() { if (!$(this).val()) $(this).val($(this).attr('title')).addClass('shadowInputTextHint'); })
	});

	// ! **** MODAL WINDOWS ****
	$("a.modal").click(function() { $.fzModalShow($(this).attr('href')); return false; });
	$("input.modal").click(function() { $.fzModalShow($(this).parents("form").attr("action")); return false; });

	// ! **** OFFERS ****
	if (!ie6){
		if ($(".offer").size() > 1) {
			fzp_closeOffers();
			$(".offerTitle").css("cursor", "pointer").click( function(){ // make clickable
				fzp_animateOffer( $(this).parent() );
				// fzp_openOffer( $(this).parent() ); // for ie6 - not working, ie6 layout bug, title wont move when offer opens... fix later.
			});
		}
	}

	// MERCHANT_INFO FEATURES PAGE
	if ($('.featureNav').length) fzp_merchantFeaturesSetup('#mobile'); // set up sliding panels, accepts the ID of the feature to select by default (include # selector)
	$(document).keyup(function(e) { fzp_timeTravel(String.fromCharCode(e.keyCode)) }); // 88 miles per hour!
		
	//jcorners
	$.jcorners($('.pageIndex'));

}); // *************************** END ON DOCUMENT READY *************************** //

// ! ****  BUBBLE HINT FUNCTIONS ****
function fzp_bubbleHintHide() { // hide a bubble hint
	// hide (ie6)
	if (ie6) $('.bubbleHintShown').animate({top: '-=' + 10 + 'px'}, 200, function() { $('.bubbleHint').css('display', 'none'); });
	// hide (everybody else)
	else $('.bubbleHintShown').animate({opacity: 0, top: '-=' + 10 + 'px'}, 200, function() { $('.bubbleHint').css('display', 'none'); });
}
function fzp_bubbleHintShow(trigger) { // show a bubble hint
	// position and show
	var target          = $('#' + trigger.attr('id') + 'BubbleHint');
	var trigger_pos     = trigger.offset();
	var target_pos_top  = trigger_pos.top - target.height() + 12;
	var target_pos_left = trigger_pos.left + ((trigger.innerWidth() / 2) - (target.width() / 2));
	target.addClass('bubbleHintShown');
	if (ie6) target.css({display: 'block', left: target_pos_left + 'px', top: target_pos_top + 'px' }).animate({top: '-=' + 10 + 'px'}, 250); // show (ie6)
	else target.css({display: 'block', left: target_pos_left + 'px', opacity: 0, top: target_pos_top + 'px' }).animate({opacity: 1, top: '-=' + 10 + 'px'}, 250); // show (everybody else)
}


// ! **** MERCHANT INFO - FEATURES FUNCTIONS ****
function fzp_merchantFeaturesSetup(selected_id) { // merchant info, features setup
	$('.featureSection').css('display', 'none'); // hide all features
	$('.featureNav a').click(function() { fzp_merchantFeaturesSelect($(this).attr('href'), true); $(this).blur(); return false; }); // setup nav link functionality to select feature, and don't go to anchor
	fzp_merchantFeaturesSelect(selected_id, false); // show selected feature (don't) animate first time around
}
function fzp_merchantFeaturesSelect(target_id, animate) { // merchant info, select a feature
	if ($('a[href="'+target_id+'"]').hasClass('selected')) return false; // if feature is already selected do nothing
	$('.featureNav a.selected').removeClass('selected'); // un-select previously selected feature's nav link
	$('a[href="'+target_id+'"]').addClass('selected'); // select this feature's nav link
	$(target_id).css('display', 'block').css('left', '1000px'); // display target section off-screen
	// vars for transition
	var target_tab_pos_obj = $('a[href="'+target_id+'"]').position(); // position object of selected tab
	var target_tab_x_pos   = target_tab_pos_obj.left + ( $('a[href="'+target_id+'"]').outerWidth() / 2 );
	var target_height      = $(target_id).outerHeight(); // height of target section
	if (animate && !ie6) { // animate transition if not ie6
		var section_width     = $('.featureContainer').outerWidth(); // width of a section in pixels
		var animate_direction = parseInt($('.featureContainer').css('background-position')) + 1000 > target_tab_x_pos ? -1 : 1; // determine direction to animate
		$('.featureContainer').animate({ // animate container
			backgroundPosition: (target_tab_x_pos - 1000)+'px 0',
			height: target_height+'px'
		}, 500, 'jswing');
		$('.featureContainer .selected').removeClass('selected').animate({ left: (animate_direction * section_width * -1)+'px' }, 500, 'jswing', function() { $(this).css('display', 'none') }); // animate old section out
		$(target_id).addClass('selected').css('left', (animate_direction * section_width)+'px').animate({ left: '0px' }, 500, 'jswing'); // animate new section in
		$.scrollTo('#featuresNav', 500, { easing: 'swing'}); // window scrolling
	}
	else { // else ie just switch it out
		$('.featureContainer').css('background-position', (target_tab_x_pos - 1000)+'px 0').css('height', target_height+'px'); // update container
		$('.featureContainer .selected').removeClass('selected').css('display', 'none'); // hide old section
		$(target_id).addClass('selected').css('left', '0px'); // show new section
	}
}

// ! **** TIME MACHINE FUNCTIONS ****
function fzp_timeCurrent() { // return get current time for timeZones
	var hour = new Date().getHours();
	if (hour >= 6 && hour < 10) return 0;
	if (hour >= 10 && hour < 17) return 1;
	if (hour >= 17 && hour < 20) return 2;
	return 3;
}
function fzp_timeTravel(l) { // back to the future
	timeCode += l;
	if (timeCode != timeMachine.substr(0, timeCode.length)) timeCode = '';
	else if (timeCode == timeMachine) {
		timeCurrent++;
		if (timeCurrent >= timeZones.length) timeCurrent = 0;
		$('#pond').removeClass('morning day evening night').addClass(timeZones[timeCurrent]);
		timeCode = '';
	}
}

// ! **** OFFER FUNCTIONS ****
function fzp_closeOffers() { // close all open offers
	$("#offers .offer").each( function() {
		$(this).height( $(this).children(".offerTitle").outerHeight() );
	});
}
function fzp_animateOffer(target) { // animate 
	if (target.outerHeight() < target.children(".offerTitle").outerHeight()+target.children(".offerContent").outerHeight() ) { // if not open already, open offer
		$('.offer').removeClass('activeOffer');
		target.addClass('activeOffer').animate( {height: target.children(".offerTitle").outerHeight()+target.children(".offerContent").outerHeight() }, 250, 'jswing');
		$.scrollTo( $('.activeOffer'), 500 );
	} else { // if open, then close
		target.animate( {height: target.children(".offerTitle").outerHeight() }, 250, 'jswing');
	}
}
function fzp_openOffer(target) { // open offer - no animation - for ie6 cause it sucks
	if (target.outerHeight() < target.children(".offerTitle").outerHeight()+target.children(".offerContent").outerHeight() ) {
		target.height(target.children(".offerTitle").outerHeight()+target.children(".offerContent").outerHeight() );
	} else {
		target.height(target.children(".offerTitle").outerHeight() );
	}
}
function fzp_printOffer(target, offer, locations, commodity) { // print offer
	tabHeight = target.parents(".offer").height();
	target.parents(".offer").css("height", "auto");
	target.parents(".offer").siblings(".offer").addClass("dontPrint");
	window.print();
	$(".dontPrint").removeClass("dontPrint");
	target.parents(".offer").css("height",tabHeight+"px");
	$.ajax({ // record print in stats
		url:'/search',
		timeout: 5000,
		data:{ offerPrintId:offer, locationids:locations, keyword:commodity },
		type: "POST"
	});
}

// strip slashes function
function stripSlashes(str) {
	return str.replace(/\\/g, '');
}
