(function($) {

	window.app = {
	
		init: function() {

                    //$('.image_container').each(function() {
                    //    if( $('img', this).length == 1 ) {
                    //        $(this).css('background-color','#fff');
                    //    }
                    //});

		},
	
	    quicksearch: {
	    
	        init: function() {
	        
	            $('#quicksearch_field').click(function() {
	            	if ($(this).attr('value') == 'Search...') {
						$(this).val('');
					}
					$(this).addClass('active');
	            });
	        
	            $('#quicksearch_btn').click(function() {
	                $('#quicksearch_form')[0].submit();
	            });
	        }
	    
	    },
		
		quiz: {
		
			init: function() {
			
				$('.quiz .button a').click(function() {
				
					var this_quiz_id = $(this).closest('.quiz').attr('id');
				
					var answers = [];
					var loopcount = 0;
					
					$('.quiz .item').each(function() {
						loopcount ++;
						var v = $('input.q[name=q-' + loopcount + ']:checked').val();
						answers[answers.length] = v;
					});
				
					var url = '/custom/quiz';
					var params = {
						'quiz_id': this_quiz_id,
						'answers': answers.join(',')
					}
					$.ajax({
					  url: url,
					  type: "POST",
					  data: (params),
					  success: function(msg){
						 h.alert(msg);
					  }
				   });
				   
				   return false;
				
				});
				
			}		
		
		},
		
		vote: {
		
		    init: function() {
		    	$('#already_submitted').each(function() {
		    		var expired = 0
		    		if ($('#vote_expired').val() == '1') {
		    			var expired = 1
		    		}
		    		var url = '/custom/vote';
					var params = {
						vote_id: $('#vote_id').val(),
						vote: 0,
						expired: expired
					   }
					$.ajax({
					  url: url,
					  type: "POST",
					  data: (params),
					  success: function(msg){
						  $('#vote_form_container').hide();
						  $('#vote_results_container .results').html(msg);
						  $('#vote_results_container').show();
					  }
					});
		    	});
		        $('#vote_now_btn').click(function() {
		            // get radio button value
		            var v = $('#vote_form #chooseButton').val();
		            if (!v) {
		                h.alert("To vote, click one of the 'tick' symbols to the left of each line, then click 'Vote now'.");
		            } else {
    					var url = '/custom/vote';
    					var params = {
    						vote_id: $('#vote_id').val(),
    						vote: v
    					   }
    					$.ajax({
    					  url: url,
    					  type: "POST",
    					  data: (params),
    					  success: function(msg){
    					      $('#vote_form_container').hide();
    					      $('#vote_results_container .results').html(msg);
    					      $('#vote_results_container').show();
    					  }
    				    });
		            }
		        });
		    }
		    
		},
		
		slideshow: {
		
			init: function() {

				$('#slideshow').cycle({
					fx:      'fade',
					speed:    1500,
					timeout:  7000,
					next:   '#slideshow', 
					pause:   0,
                                        pager:  '#slideshow_buttons_container'
				});
				$('#slideshow.paused')
					.each(function () {
						pausePlay();
					})
				;

			}
		
		},
		
		panels: {
		
			init: function() {

				function setNav() {
					$('#panelsNav td').removeClass('active');
					$('#' + this.id + '_button')
						.addClass('active')
					;
				}
				var starting_slide = $('#panels').attr('title');
				$('#panels')
					.cycle({
						fx:      'scrollLeft', 
						speed:    600, 
						timeout:  0,
						before: setNav,
						startingSlide: parseInt(starting_slide)
					})
				;
				$('#panels').removeAttr('title');

			}
		
		},
		
		slidingLinks: {
			
			init: function() {
			
				$('#links .item:first h2 a')
					.each(function () {
						$(this).addClass('active');
						$('#' + this.id + '_container').slideDown(0);
					})
				;
				$('#links .item h2 a')
					.each(function () {
						if ($(this).hasClass('active')) {
							$('#links .item .description').slideUp(0);
							$(this).addClass('active');
							$('#' + this.id + '_container').slideDown(0);
						} else {
							$('#' + this.id + '_container').hide();
						}
					})
					.click(function () {
						$('#links .item h2 a').removeClass('active');
						$(this).addClass('active');
						$('#links .item .description').slideUp(500);
						$('#' + this.id + '_container').slideDown(1000);
						return false;
					})
				;
			
			}
			
		},
		
		jfontsizer: {
			
			init: function(o) {
				
				// Cookie functions
				function setCookie(c_name,value,expiredays){
				var exdate=new Date();
				exdate.setDate(exdate.getDate()+expiredays);
				document.cookie=c_name+ "=" +escape(value)+
				((expiredays==null) ? "" : ";expires="+exdate.toGMTString())+";path=/";
				}
			
				function getCookie(c_name){
				if (document.cookie.length>0){
				  c_start=document.cookie.indexOf(c_name + "=");
				  if (c_start!=-1){
					c_start=c_start + c_name.length+1;
					c_end=document.cookie.indexOf(";",c_start);
					if (c_end==-1) c_end=document.cookie.length;
					return unescape(document.cookie.substring(c_start,c_end));
					}
				  }
				return "";
				}
			
			
				// Defaults
				var o = jQuery.extend( {
					applyTo: '#wrapper',
					changesmall: '1',
					changelarge: '2',
					expire: 90
				},o);
			
				var s = '';
				var m = '';
				var l = '';
			
				// Current
				var c = 'font_sml';
			
				// Check cookie
				if (getCookie('font_size') != "") {
					var c = getCookie('font_size');
					switch (c) {
						case 'font_sml':
							$('#fontSizer .icons a#font_sml').addClass('active');
						$(o.applyTo).css('font-size','1em');
							break;
						case 'font_med':
							$('#fontSizer .icons a#font_med').addClass('active');
						$(o.applyTo).css('font-size','1.'+o.changesmall+'em');
							break;
						case 'font_lrg':
							$('#fontSizer .icons a#font_lrg').addClass('active');
						$(o.applyTo).css('font-size','1.'+o.changelarge+'em');
							break;
					}
				}
				else {
					$('#fontSizer .icons a#font_sml').addClass('active');
				}
			
				$('#fontSizer .icons a').click(function(){
			
					var t = $(this).attr('id');
			
					setCookie('font_size',t,o.expire);
			
					$('#fontSizer .icons a').removeClass('active');
					$(this).addClass('active');
			
					var f = $(o.applyTo).css('font-size');
			
					switch(t){
						case 'font_sml':
							$(o.applyTo).css('font-size','1em');
							break;
						case 'font_med':
							$(o.applyTo).css('font-size','1.'+o.changesmall+'em');
							break;
						case 'font_lrg':
							$(o.applyTo).css('font-size','1.'+o.changelarge+'em');
							break;
					}
					
					return false;
					
				});
				
			}
		
		},
		
		styleSwitch: {
			
			init: function(o) {
				
				// Cookie functions
				function setCookie(c_name,value,expiredays){
				var exdate=new Date();
				exdate.setDate(exdate.getDate()+expiredays);
				document.cookie=c_name+ "=" +escape(value)+
				((expiredays==null) ? "" : ";expires="+exdate.toGMTString())+";path=/";
				}
			
				function getCookie(c_name){
				if (document.cookie.length>0){
				  c_start=document.cookie.indexOf(c_name + "=");
				  if (c_start!=-1){
					c_start=c_start + c_name.length+1;
					c_end=document.cookie.indexOf(";",c_start);
					if (c_end==-1) c_end=document.cookie.length;
					return unescape(document.cookie.substring(c_start,c_end));
					}
				  }
				return "";
				}
			
			
				// Defaults
				var o = jQuery.extend( {
					applyTo: 'body',
					changesmall: '1',
					changelarge: '2',
					expire: 90
				},o);
			
				var s = '';
				var m = '';
				var l = '';
			
				// Current
				var c = 'theme1';
			
				// Check cookie
				if (getCookie('contrast') != "") {
					var c = getCookie('contrast');
					switch (c) {
						case 'theme1':
							$('#contrast .icons a#font_sml').addClass('active');
						$(o.applyTo).addClass('');
							break;
						case 'theme2':
							$('#contrast .icons a#font_med').addClass('active');
						$(o.applyTo).addClass('siteTheme2');
							break;
						case 'theme3':
							$('#contrast .icons a#font_lrg').addClass('active');
						$(o.applyTo).addClass('siteTheme3');
							break;
					}
				}
				else {
					$('#contrast .icons a#theme1').addClass('active');
				}
			
				$('#contrast .icons a').click(function(){
			
					var t = $(this).attr('id');
			
					setCookie('contrast',t,o.expire);
			
					$('#contrast .icons a').removeClass('active');
					$(this).addClass('active');
			
					var f = $(o.applyTo).css('font-size');
			
					switch(t){
						case 'theme1':
							$(o.applyTo).removeClass('siteTheme2');
							$(o.applyTo).removeClass('siteTheme3');
							break;
						case 'theme2':
							$(o.applyTo).removeClass('siteTheme3');
							$(o.applyTo).addClass('siteTheme2');
							break;
						case 'theme3':
							$(o.applyTo).removeClass('siteTheme2');
							$(o.applyTo).addClass('siteTheme3');
							break;
					}
					
					return false;
					
				});
				
			}
		
		}
	
	};
	
	
	$(document).ready(function() {

		window.app.init();
		window.app.styleSwitch.init();
		window.app.jfontsizer.init();
		window.app.quicksearch.init();
		window.app.quiz.init();
		window.app.vote.init();
		window.app.slideshow.init();


            // select the span which has the following text, and item down and then up with a setTimeout to delay the up animation



           // if( !$('span#delivery_msg').is(':animated') ) {
//
//                $('span:contains("Add to Basket")').stop(true,true).click(function(){
//
//                    $('span#delivery_msg').stop(true,true).animate({'top': '+=52px'}, 'slow');
//
//                    setTimeout(function(){
//
//                        $('span#delivery_msg').stop(true,true).animate({'top': '-=52px'}, 'slow');
//
//                   }, 2000)
//                });
//
//            }
            

            $('span.view_addresses_btn').click(function() {                
                $('#addresses_'+$(this).attr('id')).slideToggle("slow");
            });

	});




        //


	

})(jQuery);


/* Dropdown menu */

/*
$(function(){

    var el_width = $("li.dropdown_menu ul").width();

    $("li.dropdown_menu").hover(function(){

        
        //$(this).addClass("hover");
        $('ul', this).width(el_width).stop(true, true).fadeIn(300);

    }, function(){

        
       $('ul' ,this).stop(true, true).fadeOut(300);

    });

    //$("ul.dropdown li ul li:has(ul)").find("a:first").append(" &raquo; ");

});
*/

// slideshow test



// Addresses slideshow test


