/**
 * Cookie plugin
 *
 * Copyright (c) 2006 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

/**
 * Parasole Open Table Widget plugin
 *
 * Copyright 2011 (c) Intercom Agency.
 *
 */

function otLoad(rqst) {
		
	// for JSON Reference http://www.hunlock.com/blogs/Mastering_JSON_(_JavaScript_Object_Notation_)
	// IE7 JSON Fix: http://dotnetfish.blogspot.com/2007/11/expected-identifier-string-or-number.html
	if(rqst == 'muffuletta') {
		var restaurant = {
		    "name": rqst,
		    "arrowTo" : "305px",
		    "theme" : "dark",
		    "locations": [{
		        "city": "St. Paul",
		        "id": "15844",
		        "opens": 10,
		        "closes": 21
		    }]
		}
	}
	
	if(rqst == 'pittsburghBlue') {
		var restaurant = {
		    "name": rqst,
		    "arrowTo" : "345px",
		    "theme" : "dark",
		    "locations": [{
		        "city": "Maple Grove",
		        "id": "14437",
		        "opens": 10,
		        "closes": 22
		    }, {
		    	"city": "Edina",
		        "id": "70627",
		        "opens": 10,
		        "closes": 21
		    }]
		}
	}
	
	if(rqst == 'cafeteria') {
		var restaurant = {
		    "name": rqst,
		    "arrowTo" : "305px",
		    "theme" : "dark",
		    "locations": [{
		        "city": "Uptown",
		        "id": "45811",
		        "opens": 9,
		        "closes": 23
		    }]
		}
	}
	
	if(rqst == 'chinoLatino') {
		var restaurant = {
		    "name": rqst,
		    "arrowTo" : "325px",
		    "theme" : "dark",
		    "locations": [{
		        "city": "Uptown",
		        "id": "4073",
		        "opens": 17,
		        "closes": 24
		    }]
		}
	}
	
	if(rqst == 'ilGatto') {
		var restaurant = {
		    "name": rqst,
		    "arrowTo" : "295px",
		    "theme" : "light",
		    "locations": [{
		        "city": "Uptown",
		        "id": "4399",
		        "opens": 16,
		        "closes": 22
		    }]
		}
	}
	
	if(rqst == 'mannys') {
		var restaurant = {
		    "name": rqst,
		    "arrowTo" : "300px",
		    "theme" : "light",
		    "locations": [{
		        "city": "Downtown",
		        "id": "3095",
		        "opens": 6,
		        "closes": 21
		    }]
		}
	}
	
	if(rqst == 'mozzaMia') {
		var restaurant = {
		    "name": rqst,
		    "arrowTo" : "320px",
		    "theme" : "dark",
		    "locations": [{
		        "city": "Edina",
		        "id": "52759",
		        "opens": 11,
		        "closes": 22
		    }]
		}
	}
	
	if(rqst == 'goodEarth') {
		var restaurant = {
		    "name": rqst,
		    "arrowTo" : "320px",
		    "theme" : "dark",
		    "locations": [{
		        "city": "Edina",
		        "id": "27313",
		        "opens": 7,
		        "closes": 21
		    }, {
		    	"city": "Roseville",
		        "id": "27355",
		        "opens": 7,
		        "closes": 21
		    }]
		}
	}
	
	if(rqst == 'salut') {
		var restaurant = {
		    "name": rqst,
		    "arrowTo" : "280px",
		    "theme" : "dark",
		    "locations": [{
		        "city": "Edina",
		        "id": "5574",
		        "opens": 10,
		        "closes": 22
		    }, {
		    	"city": "St. Paul",
		        "id": "23257",
		        "opens": 10,
		        "closes": 22
		    }]
		}
	}
	
	if(rqst == 'burgerJones') {
		var restaurant = {
		    "name": rqst,
		    "arrowTo" : "328px",
		    "theme" : "light",
		    "locations": [{
		        "city": "Calhoun",
		        "id": "48379",
		        "opens": 11,
		        "closes": 22
		    }, {
		    	"city": "Burnsville",
		        "id": "61699",
		        "opens": 11,
		        "closes": 22
		    }]
		}
	}
		
	
	$(document).ready(function() {
		var fullDate = new Date()
		var twoDigitMonth = (fullDate.getMonth()+1);
		 
		var currentDate =  twoDigitMonth + "/" + fullDate.getDate() + "/" + fullDate.getFullYear();
		if(restaurant.theme == 'dark') {
			var htmlString = ' \
			<div class="hide"> \
				<div id="widget"> \
					<img src="http://www.parasole.com/otwidget/images/'+restaurant.name+'-logo.png" class="logo" /> \
					<p><img src="http://www.parasole.com/otwidget/images/call-'+restaurant.name+'.png" /></p> \
					<p class="select"><img src="http://www.parasole.com/otwidget/images/select-message-new.png" /></p> \
					<form action="http://www.opentable.com/restaurant-search.aspx" method="POST"> \
						<select name="RestaurantID">  \
							<option disabled="disabled">Location:</option> \
						</select>  \
						<input type="hidden" name="RestaurantReferralID" value="'+restaurant.locations[0].id+'" />  \
						<input type="hidden" name="txtDateFormat" value="MM/dd/yyyy">  \
						<input type="text" name="startDate" value="'+currentDate+'" id="startDate" /> \
						<select name="ResTime"> \
						</select> \
						<select name="PartySize" class="last"> \
							<option value="2">2 people</option> \
							<option value="3">3 people</option> \
							<option value="4">4 people</option> \
							<option value="5">5 people</option> \
							<option value="6">6 people</option> \
							<option value="7">7 people</option> \
							<option value="8">8 people</option> \
							<option value="9">9 people</option> \
							<option value="10">10 people</option> \
						</select> \
						<div class="clear"></div> \
						<input type="submit" value="" id="formSubmit" style="margin-right: 18px;" /> \
					</form> \
					<div class="clear"></div> \
					<img src="http://www.parasole.com/otwidget/images/parasole-logo.png" /> \
				</div> \
			</div> \
			<div id="widget-trigger"> \
				<img src="http://www.parasole.com/otwidget/images/find-a-table-at.png" /> \
				<img src="http://www.parasole.com/otwidget/images/'+restaurant.name+'-tag.png" /> \
				<div id="continue" class="fancy"></div> \
				<div class="clear" style="margin: 0;"></div> \
			</div>  \
			<img src="http://www.parasole.com/otwidget/images/bk_arrow.png" id="arrow" /> \
			<img src="http://www.parasole.com/otwidget/images/arrow.png" class="hide" /> \
			';
		}
		
		if(restaurant.theme == 'light') {
			var htmlString = ' \
			<div class="hide"> \
				<div id="widget" class="light"> \
					<img src="http://www.parasole.com/otwidget/images/'+restaurant.name+'-logo.png" class="logo" /> \
					<p><img src="http://www.parasole.com/otwidget/images/call-'+restaurant.name+'.png" /></p> \
					<p class="select"><img src="http://www.parasole.com/otwidget/images/select-message-light-new.png" /></p> \
					<form action="http://www.opentable.com/restaurant-search.aspx" method="POST"> \
						<select name="RestaurantID">  \
							<option disabled="disabled">Location:</option> \
						</select>  \
						<input type="hidden" name="RestaurantReferralID" value="'+restaurant.locations[0].id+'" />  \
						<input type="hidden" name="txtDateFormat" value="MM/dd/yyyy">  \
						<input type="text" name="startDate" value="'+currentDate+'" id="startDate" /> \
						<select name="ResTime"> \
						</select> \
						<select name="PartySize" class="last"> \
							<option value="2">2 people</option> \
							<option value="3">3 people</option> \
							<option value="4">4 people</option> \
							<option value="5">5 people</option> \
							<option value="6">6 people</option> \
							<option value="7">7 people</option> \
							<option value="8">8 people</option> \
							<option value="9">9 people</option> \
							<option value="10">10 people</option> \
						</select> \
						<div class="clear"></div> \
						<input type="submit" value="" id="formSubmit" style="margin-right: 18px;" /> \
					</form> \
					<div class="clear"></div> \
					<img src="http://www.parasole.com/otwidget/images/parasole-logo.png" /> \
				</div> \
			</div> \
			<div id="widget-trigger" class="light"> \
				<img src="http://www.parasole.com/otwidget/images/find-a-table-at-light.png" /> \
				<img src="http://www.parasole.com/otwidget/images/'+restaurant.name+'-tag.png" /> \
				<div id="continue" class="fancy"></div> \
				<div class="clear" style="margin: 0;"></div> \
			</div>  \
			<img src="http://www.parasole.com/otwidget/images/bk_arrow.png" id="arrow" /> \
			<img src="http://www.parasole.com/otwidget/images/arrow.png" class="hide" /> \
			';
			$('#fancybox-outer').css('background','#f7ebcb');
		}
		
		//Creat container for HTML
		$('body').append('<div id=ot />');
		
		//Add HTML into div#ot
		$('#ot').append(htmlString);
		
		function makeTimes(time, closing) {
			if(time > 12) { var timeAdj = time-12; var ampm = 'PM'; } else { var timeAdj = time; var ampm = 'AM'; }
			if(time ==12) { var timeAdj = time; var ampm = 'PM'; }
			if(restaurant.name != 'mannys') { var menuOptionA = "<option value="+time+':00'+">"+timeAdj+':00 '+ampm+"</option>"; }
			var menuOptionB = "<option value="+time+':30'+">"+timeAdj+':30 '+ampm+"</option>";
			$('form select[name=ResTime]').append(menuOptionA).append(menuOptionB);
			var time = time+1;
			if(time==closing) { return; }
			else { makeTimes(time, closing); } 
		}
		
		//For each restaurant location
		for (var i = 0; i < restaurant.locations.length; i++) {
			var rId = restaurant.locations[i].id;
			var rCity = restaurant.locations[i].city;
			$('select[name=RestaurantID]').append('<option value="'+rId+'">'+rCity+'</option>');
		}
		$('select[name=RestaurantID] option:nth-child(2)').attr('selected','selected');
		makeTimes(restaurant.locations[0].opens, restaurant.locations[0].closes);
		
		//Update times once a new locatoin is selected
		$('select[name=RestaurantID]').change(function(){
			var selectedIndex = $(this).find('option:selected').index('option')-1;
			$('form select[name=ResTime] option').remove();
			makeTimes(restaurant.locations[selectedIndex].opens, restaurant.locations[selectedIndex].closes);
			$('input[name=RestaurantReferralID]').val(restaurant.locations[selectedIndex].id);
		});
		
		if($.browser.msie && $.browser.version<"9.0") {
			$('#arrow').attr('src','http://www.parasole.com/otwidget/images/arrow.png');		
		}
		
    	var triggerWidth = $('#widget-trigger').width();
		$('#arrow').css('left', '520px');
		function widgetSlide() {
    		$('#widget-trigger').delay(200).animate({'bottom':'0px'}, 800);
    		$('#arrow').delay(1200).animate({'left':restaurant.arrowTo, 'opacity':'1.0'}, 600).delay(1000).fadeOut(400);	
    	}
    	
		if($.cookie('hasSeenResPopup') == 'true') {
			//if cookie is set, don't show popup
			$('.fancy').fancybox({
				'scrolling': 'no',
				'overlayOpacity': '0.4',
				'overlayColor': '#000',
				'href': '#widget'
			});
			$('#widget-trigger').css('bottom','0px');
		} 
		if($.cookie('hasSeenResPopup') != 'true') {
			//else, show popup and animation on close
			$('.fancy').fancybox({
				'scrolling': 'no',
				'overlayOpacity': '0.4',
				'overlayColor': '#000',
				'onClosed': widgetSlide,
				'href': '#widget'
			});
			$('#continue').delay(400).trigger('click');
			$.cookie('hasSeenResPopup', 'true', { expires: 1, path: '/'});
		}
		
		$('input#startDate').Zebra_DatePicker({
		    direction: true,
		    format: 'm/d/Y'
		});	
		$('#widget button').css('margin','0').css('marginLeft','-10px').width(0).height(0).text('');    		
		if($.browser.mozilla) {
			$('#widget').css('height','372px');	
			$('#widget form *').css('marginRight','10px');	
			$('#widget form select.last').css('width','90px');
		}
	});
};
