/* 
	Thrifty Foods - jQuery Site Enhancements
	September 2008
	Atomic Crayon / karen@atomiccrayon.com / SMRT
*/

/* Sections flagged with =
	BODY CLASSES
	WARNINGS
	LINKS
	ROUND CORNERS
	LISTINGS & TABLES
	PRINT LINKS
	RECIPE PAGE LINKS & FORM FIELDS
	MY ACCOUNT FORMS
	NAVIGATION
	TRANSPARENT PNGS
	FONT RESIZE
	COMMUNITY SIDEBAR
	POLLS

*/

/* function to parse query strings - http://stilbuero.de/demo/jquery/query.html */
jQuery.query = function(s) {
                var r = {};
                if (s) {
                    var q = s.substring(s.indexOf('?') + 1); // remove everything up to the ?
                    q = q.replace(/\&$/, ''); // remove the trailing &
                    jQuery.each(q.split('&'), function() {
                        var splitted = this.split('=');
                        var key = splitted[0];
                        var val = splitted[1];
                        // convert numbers
                        if (/^[0-9.]+$/.test(val)) val = parseFloat(val);
                        // convert booleans
                        if (val == 'true') val = true;
                        if (val == 'false') val = false;
                        // ignore empty values
                        if (typeof val == 'number' || typeof val == 'boolean' || val.length > 0) r[key] = val;
                    });
                }
                return r;
            };



$(document).ready(function(){

/* =BODY CLASSES
----------------------------------------------------*/
		
// add class to body tag to indicate JS is on

//$('body').addClass('jquery');

// check for Safari

if(window.devicePixelRatio) {
	$('body').addClass('safari');
}

/* =WARNIINGS
----------------------------------------------------*/

$('#javascript-warning').remove();

/* =LINKS
----------------------------------------------------*/

// open rel="external" links in new windows

$("a[@rel~='external']").click(function(){
	window.open($(this).attr('href'));
	return false;
});


/* =LISTINGS & TABLES
----------------------------------------------------*/

// add odd classes to listings

$('ul.alt li:odd').addClass('even');

// make boxes clickable

$("#new-recipes ul li").addClass('click-box').click(function(){
	  window.location=$(this).find("a").attr("href"); return false;
});


$('#recipe-reviews #recipe-photo').addClass('click-box').click(function(){
	  window.location=$(this).find("a").attr("href"); return false;
});

$('#recipe-listing #recipe-photo').addClass('click-box').click(function(){
	  window.location=$(this).find("a").attr("href"); return false;
});

$('#recipe-listing #reviews-listing tr[class!=header]').addClass('click-box').click(function(){
	  window.location=$(this).find("a").attr("href"); return false;
});

$(".listing-platters li").hover(
	function() {
		$(this).addClass('hover');
	},
	function() {
		$(this).removeClass('hover');
	}
);

$('.sub-page-listing .col:first').addClass('col-first');
$('.sub-page-listing .col:last').addClass('col-last');

// stripe tables

$('.table-striped tr:even').addClass('even'); 


/* =PRINT LINKS
----------------------------------------------------*/

$('#buttons #save').after(' <a href="#" id="print" class="button-light" onClick="window.print(); return(false);">Print</a>');

/* =RECIPE PAGE LINKS & FORM FIELDS
----------------------------------------------------*/

// add hidden submit button

$('#hidden').append('<input id="btnSubmit" type="submit" class="button-light" style="display:none;" />');

// check boxes for ingredients
/*
$(".ingredients-list label").each(function(){		
		inputID = $(this).attr("for");
		inputNumber = inputID.substring(11);
		$(this).prepend('<input type="checkbox" name="html_items[' + inputNumber + '][add]" id="ingredient-' + inputNumber + '" class="checkbox" />');
});		
*/
// add buttons

$('#add-buttons').append('<a href="javascript:document.getElementById(\'buttonclick\').value=\'add_to_list\';document.getElementById(\'btnSubmit\').click();" id="add-selected">Add selected to shopping list</a> <a href="javascript:document.getElementById(\'buttonclick\').value=\'add_all\';document.getElementById(\'btnSubmit\').click();" id="add-all">Add all ingredients to shopping list</a>');

// action buttons

$('#action-buttons').prepend('<a href="javascript: void(0)" id="print-recipe-single">Print on Full Page</a>  <a href="javascript: void(0)" id="print-recipe-3x5">Print on 3x5 Cards</a> <label id="nutrition-label" for="add-nutrition-check"><input type="checkbox" id="add-nutrition-check" /> Print Nutritional Information</label> <a href="javascript:document.getElementById(\'buttonclick\').value=\'add_to_box\';document.getElementById(\'btnSubmit\').click();" id="add-recipe">Add to recipe box</a> <a href="javascript:document.getElementById(\'buttonclick\').value=\'note_add\';document.getElementById(\'btnSubmit\').click();" id="add-note">Add note</a>');

// recipe email button
qs = location.search;
var query = $.query(qs);

var recipeTitle = $('#content h1#headline').text();

$('#action-buttons').append('<a href="../../../../common/email-recipe-form.html?keepThis=true&TB_iframe=true&height=420&width=550&rid=' + query['rid'] + '" id="email-recipe" class="thickbox" title="' + recipeTitle +  '">Email this recipe</a>');


// notes buttons

$('#notes-buttons .update-note').after('<a href="javascript:document.getElementById(\'buttonclick\').value=\'note_update\';document.getElementById(\'btnSubmit\').click();" id="update-note" class="button-light">Save note</a>');


$('#notes-buttons .edit-note').after('<a href="javascript:document.getElementById(\'buttonclick\').value=\'note_edit\';document.getElementById(\'btnSubmit\').click();" id="edit-note" class="button-light">Edit note</a> ');

$('#notes-buttons .delete-note').after('<a href="javascript:document.getElementById(\'buttonclick\').value=\'note_delete\';document.getElementById(\'btnSubmit\').click();" id="delete-note" class="button-light">Delete note</a> ');

// shopping cart email button
$('.email-shopping-list-button').append('<a href="../../../common/email-shopping-form.html?keepThis=true&TB_iframe=true" id="email-shoppinglist" class="thickbox" title="Email this shopping list">Email this shopping list</a>');


/* =MY ACCOUNT FORMS
----------------------------------------------------*/

// account form -- hide/show forms

$('#my-account div.choose').html('<form action="" id="choose"><p><label for="employee-query">Are You an Employee?</label><span class="radio-wrapper"><label for="employee-no"><input type="radio" name="employee-query" id="user-yes" value="user" class="radio" /> No</label> <label for="employee-yes"><input type="radio" name="employee-query" id="employee-yes" value="employee" class="radio" /> Yes</label></span></p><div class="clear"></div></form>');

// hide both forms

$('#my-account form.user-form').hide();
$('#my-account form.employee-form').hide();

// check for anchors in URL

var myFile = document.location.toString();
if (myFile.match('#')) { // the URL contains an anchor
	// show the form matching the anchor
	var myAnchor = myFile.split('#')[1];
	$('#my-account form.' + myAnchor + '-form').show();
	$('#my-account div.choose #' + myAnchor + '-yes').attr("checked","checked");
} else {
	// select the user form
	$('#my-account form.user-form').show();
	$('#my-account div.choose #user-yes').attr("checked","checked");
}

$('#my-account div.choose #user-yes').click(
	function() {
		$('#my-account form.employee-form').hide();
		$('#my-account form.user-form').show();
	}
);

$('#my-account div.choose #employee-yes').click(
	function() {
		$('#my-account form.employee-form').show();
		$('#my-account form.user-form').hide();
	}
);


// show checkboxes

$('#my-account .check-all').replaceWith('<div class="col"><ul><li><label for="checkStores"><input type=\'checkbox\' id="checkStores" name=\'checkStores\' onclick=\'javascript: check_Stores(this.form);\' />   All Locations </label>                                         </li> <li><label for="checkJobs"> <input type=\'checkbox\' id="checkJobs" name=\'checkJobs\' onclick=\'javascript: check_Jobs(this.form);\' />All Job Areas</label>                                                   </li> <li><label for="checkAll"> <input type=\'checkbox\' id="checkAll" name=\'checkAll\' onclick=\'javascript: checkAllAreas(this.form);\' />All Job Areas and Locations </label>                                              </li></ul></div>');


/* =NAVIGATION
----------------------------------------------------*/

// add sfhover class for dropdowns

$('#main-navigation ul li').hover(function() {
		$(this).addClass('sfhover');
	}, function() {
		$(this).removeClass('sfhover');
});

$('#main-navigation ul li ul li.parent').hover(function() {
		$(this).addClass('sfhover-parent');
	}, function() {
		$(this).removeClass('sfhover-parent');
});


$('#minor-navigation ul li').hover(function() {
		$(this).addClass('sfhover');
	}, function() {
		$(this).removeClass('sfhover');
});

$('#minor-navigation ul li ul li.parent').hover(function() {
		$(this).addClass('sfhover-parent');
	}, function() {
		$(this).removeClass('sfhover-parent');
});


/* =SECTION-NAVIGATION
----------------------------------------------------*/


/* =TRANSPARENT PNGS
----------------------------------------------------*/

// find path to images for this server

var logoPath = $('#inside #brand #print-logo').attr('src');

var imagePath;
var x = logoPath.length;
while((logoPath.substring(x,x-1)) != "/"){ x--; } 
var clipstop = x;
imagePath = logoPath.substring(0,clipstop);

$.ifixpng(imagePath + 'pixel.gif');

$('.smile').ifixpng();
$('.blank').ifixpng();


/* =FONT RESIZE
----------------------------------------------------*/


// add code

$('#font-size').after('<p id="text-size"> <!-- open TEXT-SIZE -->				&nbsp;<span class="separator">|</span> <strong>Text size:</strong> <a href="#" class="size smaller" title="Decrease Font Size">A-</a> <a href="#" class="size larger" title="Increase Font Size">A+</a></p> <!-- close TEXT-SIZE -->');

$('#font-size-footer').after('<p id="text-size-footer"> <!-- open TEXT-SIZE FOOTER-->	<strong>Text size:</strong> <a href="#" class="size smaller" title="Decrease Font Size">A-</a> <a href="#" class="size larger" title="Increase Font Size">A+</a></p> <!-- close TEXT-SIZE FOOTER-->');


var CookieName = 'textSize';

// Font Size
var fontSize;
var textBase = 76;
var increment = 10;

// Check for cookie

var cookieSize = $.cookie(CookieName);

if (cookieSize > 0) {
	fontSize = Number(cookieSize);
	$('body').css('font-size', fontSize + '%');
}
else {
	fontSize = textBase;
	$('body').css('font-size', fontSize + '%');
}

// Check for clicks

$('.smaller').click(function(){
	fontSize = fontSize - increment;
	$('body').css('font-size', fontSize + '%');
	$.cookie(CookieName, fontSize, { path: '/', expires: 365 });
	return false;
});

$('.larger').click(function(){
	fontSize = fontSize + increment;
	$('body').css('font-size', fontSize + '%');
	$.cookie(CookieName, fontSize, { path: '/', expires: 365 });
	return false;
});

var sifrSize = Math.round((40 * fontSize) / 100);





});