// Resize Menus
function size_menu() {

	// top menu numbers
	var top_num = $$("div.hor_menu li").size();
	var top_menu_size = $$('div.hor_menu').first().getWidth();

	top_w = Math.floor(top_menu_size/top_num)-1;
	top_last = 1 + top_w + top_menu_size%top_num;

	// botom menu numbers
	var bottom_num = $$("div.bottom li").size();
	var bottom_menu_size = $$('div.bottom').first().getWidth();
	bottom_w = Math.floor(bottom_menu_size/bottom_num)-1;
	bottom_last = bottom_w + bottom_menu_size%bottom_num;

	// top menu
	$$("div.hor_menu li").each(function(n) {
		n.setStyle({
		  width: top_w+'px',
		  textAlign: 'center'
		});

	});

	// bottom menu
	$$("div.bottom li").each(function(n) {
		n.setStyle({
		  width: bottom_w+'px',
		  textAlign: 'center'
		});

	});

	$$("div.hor_menu li a, div.bottom li a").each(function(n) {
		n.setStyle({
		  width: '100%'
		});

	});

	// top
	$$("div.hor_menu li").last().setStyle({	  width: top_last+'px', background: 'none'	});
	// bottom
	$$("div.bottom li").last().setStyle({	  width: bottom_last+'px', background: 'none'	});

}

function load_slider() {

	if($('slider')) {

		$('slider').innerHTML='\
\
		<div class="scroller">\
			<div class="gcontent">\
				<div class="section" id="bed-linen">\
					<a href="bed-linen"><img src="images/slider/bed-linen.jpg" alt="Bed Linen"></a>\
				</div>\
				<div class="section" id="quilts">\
					<a href="quilts"><img src="images/slider/quilts.jpg" alt="Quilts"></a>\
				</div>\
				<div class="section" id="throws">\
					<a href="blankets-throws"><img src="images/slider/throws.jpg" alt="Blankets &amp; Throws"></a>\
				</div>\
				<div class="section" id="cushions">\
					<a href="cushions"><img src="images/slider/cushions.jpg" alt="Cushions"></a>\
				</div>\
				<div class="section" id="table-linen">\
					<a href="table-linen"><img src="images/slider/table-linen.jpg" alt="Table Linen"></a>\
				</div>\
				<div class="section" id="hand-towels">\
					<a href="guest-kitchen-towels"><img src="images/slider/hand-towels.jpg" alt="Hand &amp; Kitchen Towels"></a>\
				</div>\
				<div class="section" id="cool-galah">\
					<a href="cool-galah-towels"><img src="images/slider/cool-galah.jpg" alt="Cool Galah Towels"></a>\
				</div>\
			</div>\
		</div>\
		<div class="controls">\
			<a href="#bed-linen" class="active">1</a>\
			<a href="#quilts">2</a>\
			<a href="#throws">3</a>\
			<a href="#cushions">4</a>\
			<a href="#table-linen">5</a>\
			<a href="#hand-towels">6</a>\
			<a href="#cool-galah">7</a>\
		</div>\
		';
	}
}

// ------------------------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------------------------
// Show Info Message
function info_msg(text, buttons) {
	// if (typeof buttons === 'undefined') buttons = true;

	top_offset=document.viewport.getScrollOffsets()['top'];
	screen_height=document.viewport.getHeight();

	var x=Builder.node('div', { className:'info_msg', id:'info_msg'}, [
		Builder.node('div', { className:'info_msg_box' }, [
			Builder.node('div', { className:'topline' }, [ ] ),
			Builder.node('p')
		])
	]);

	// extend this element for Prototype
	x=$(x);

	// set height & top offset
	x.setStyle({height:screen_height+'px', marginTop:top_offset+'px'});
	x.down('div.info_msg_box').setStyle( { width:'450px' } );

	// insert text
	x.down('p').update(text);

	// add buttons
	if(buttons) {
		// insert supplied button(s)
		x.down('p').insert({after: buttons});
	} else {
		// default "OK" button
		x.down('p').insert({after: Builder.node('button', { className:'close', onclick:'$("info_msg").remove();' }, 'OK')});
	}

	var body = document.getElementsByTagName("body").item(0);
	new Insertion.Bottom(body, x);

}

// -------------------------------------------------------------------------------------------------------------------

// Set Coupon -----------------------------------------------
function set_coupon(coupon,ss) {

	new Ajax.Request('ajax_functions.php', { 
		method: 'get',
		parameters: {
			action: 'apply_coupon',
			code: coupon,
			ss: ss
		},
		onSuccess: function(transport) {
			if(transport.responseText=='success') {
				// all good
				window.location.reload();
			} else {
				// show error
				info_msg(transport.responseText);
			}
		}

	});
}
// ----------------------------------------------------------


Event.observe(window, 'load', function() {
	// resize menus
	size_menu();

	// Load slider
	if($('slider')) {
		load_slider();
		my_glider = new Glider('slider', {duration:0.5, autoGlide:true, frequency: 6});
	}

	// tab boxes
	if($('tnav')) {
		boxes = $$('div.content div.tab').invoke('hide');
		boxes.first().show();
		$$('#tnav span').first().toggleClassName('active');

		$$('#tnav span').each(function(n, index) {

			Event.observe(n, 'click', function(event) {
				n.blur();
				Event.stop(event);

				// change active 
				$$('#tnav span').invoke('removeClassName', 'active');
				n.toggleClassName('active');

				//show corresponding box
				boxes.invoke('hide');
				boxes[index].show();

			});
		});
	}





});
