$(document).ready(function(){//start ready		
	global.innit();
});//end ready function

var global = 
{
	innit: function() {
		this.effects();
	}
	,
	effects: function() {
		//Category hover states
		$('#pd-cat li.pd-listitem:odd .pd-item').css('border-right', 'none');

		$('.pd-cat-ct1 .info').toggle( 
			function() {
				$(this).addClass('close').attr('title', 'Click here to close the description').parent().find('img').fadeTo("fast", 0.1).end().parent().find('.desc').fadeIn('fast');
			},
			function() {
				$(this).removeClass('close').attr('title', 'Click here to see a brief description').parent().find("img").fadeTo("fast", 1).end().parent().find('.desc').fadeOut('fast');		
			}
		);
		
		//back button
		$('a.previous').click( function(e) {
			history.go(-1);
			e.preventDefault();
		}); 
		
		// vertical positioning in the viewport
		(function($){
		  $.fn.vCenter = function(options) {
			var pos = {
			  sTop : function() {
				return window.pageYOffset || document.documentElement && document.documentElement.scrollTop ||	document.body.scrollTop;
			  },
			  wHeight : function() { 
				return window.innerHeight || document.documentElement && document.documentElement.clientHeight || document.body.clientHeight;
			  }
			};
			return this.each(function(index) {
			  if (index == 0) {
				var $this = $(this);
				var elHeight = $this.height();
					var elTop = pos.sTop() + (pos.wHeight() / 2) - (elHeight / 2);
				$this.css({
				  position: 'absolute',
				  marginTop: '0',
				  top: elTop
				});	
			  }
			});
		  };
		
		})(jQuery);
		//help overlays
		$('.show-help').click( function(e) {
			if ($(this).attr('name') != "undefined") { 
				var hlpDim = $(this).attr('name').split('-');
			} else {
				var hlpDim = $(this).parent().attr('name').split('-');
			}
			var helpID = "#hlp-" + hlpDim[1];
			var helpW = parseFloat(hlpDim[2]);
			var helpH = parseFloat(hlpDim[3]);
			var marginLeft = '-' + (helpW/2) + 'px';
			var innaH = helpH - 100;
			var innaW = helpW - 24;
			$(helpID).width(helpW).height(helpH).vCenter().css({marginLeft: marginLeft});
			$('.hlp-pnl-inna').height(innaH).width(innaW);
			var pageHeight = $('body').height();
			$('#hlp-overlay').fadeIn('fast').css('height', pageHeight);
			$(".hlp-pnl img.dt-lrg").attr('width',helpW);
			setTimeout("$('" + helpID +"').fadeIn('fast')", 400);
			e.preventDefault();
		});
		$('.hlp-close').click( function() {
			$('div.hlp-pnl').hide(); $('#hlp-overlay').hide();
		});
		$('#hlp-overlay').bind("click", function(e){
			$('div.hlp-pnl').hide(); $(this).hide();
		});
		
		//sign up email to user
		$('#email').blur(function() {
			$('#user').val($(this).val());
		});
		
		//listens out for clicked link with openlink class (new window...)
		$('.openlink').click(
			function() {
				window.open(this.href);
				return false;
			}
		);
		
		//POPUP sample usage:  class="popup" name="pop-{$productTitle}-700-500"
		//you need to specify an href too. If for example its an image to be enlarged, 
		//the <img> needs to be enclosed by an <a class="popup" name="pop-{$productTitle}-700-500">
		var popup = {
			init: function(h,w) {
					$('.popup').click( function(e) {
						if ($(this).attr('name') != "undefined") { 
							var popDim = 	$(this).attr('name').split('-');
						} else {
							var popDim = 	$(this).parent().attr('name').split('-');
						}
						window.open($(this).attr('href'), 'GlassesDirect' + popDim[1],'menubar=0,resizable=1,scrollbars=1,width=' + popDim[2] +',height=' + popDim[3] + '');
						e.preventDefault();
					});
					$('.close-win').click( function(e) {window.close(); e.preventDefault();});
			}
		}
		popup.init();
		
		//prep elements
		$('#pre_wearer').hide();
		$('.complicated, #pre-pnl1').show();
		
		 //The Togglator
		function togglator (toggler, elem) {
			$(toggler).click( function(e) {
				$(elem).toggle();
				e.preventDefault();
			});
		}
	
		//just add a call to the function, as below
		togglator('.toggler', '.toggled');
		togglator('#define_wearer', '#pre_wearer');
		togglator('.complicated', '.togglator');
		
		cata();//ini the ordercatalogue
		$("#ord-pnl1 input:radio").click(function(){cata();});//set function in order catalogue page to hide show friends/myself option
		
		//Edit correct prescription on change of select in basket
		$('.pres-sel select').change( function() {
			var thisVal = $(this).val();
			$('.pres-sel p.edit-pres-link a').attr('href', '/prescriptions?action=edit&itemID=' +thisVal+ '&url=/basket' );
		})

		
	}//effects

}//global
function cata(){//function for order catalogue
	var c = $("#ord-pnl1 input:checked");var c2 = c.val();			
	if(c2 =="myself" || c2 == "") {
		$("#ord-myself-cont").show();$("#ord-friend-cont").hide();
	}
	else{
		$("#ord-myself-cont").hide();$("#ord-friend-cont").show();
	}
};





