$(document).ready(function(){
  
  valid_postcodes = ['AB10','DD4','EH26','EH99','G4','G84','IV4','KA23','KY16','PA22','PA66','PH32','AB11','DD5','EH27','FK1','G40','G9','IV40','KA24','KY2','PA23','PA67','PH33','AB12','DD6','EH28','FK10','G41','GIR','IV41','KA25','KY3','PA24','PA68','PH34','AB13','DD7','EH29','FK11','G42','HS1','IV42','KA26','KY4','PA25','PA69','PH35','AB14','DD8','EH3','FK12','G43','HS2','IV43','KA27','KY5','PA26','PA7','PH36','AB15','DD9','EH30','FK13','G44','HS3','IV44','KA28','KY6','PA27','PA70','PH37','AB16','DG1','EH31','FK14','G45','HS4','IV45','KA29','KY7','PA28','PA71','PH38','AB21','DG10','EH32','FK15','G46','HS5','IV46','KA3','KY8','PA29','PA72','PH39','AB22','DG11','EH33','FK16','G5','HS6','IV47','KA30','KY9','PA3','PA73','PH4','AB23','DG12','EH34','FK17','G51','HS7','IV48','KA4','KY99','PA30','PA74','PH40','AB24','DG13','EH35','FK18','G52','HS8','IV49','KA5','ML1','PA31','PA75','PH41','AB25','DG14','EH36','FK19','G53','HS9','IV5','KA6','ML10','PA32','PA76','PH42','AB30','DG2','EH37','FK2','G58','IV1','IV51','KA7','ML11','PA33','PA77','PH43','AB31','DG3','EH38','FK20','G59','IV10','IV52','KA8','ML12','PA34','PA78','PH44','AB32','DG4','EH39','FK21','G60','IV11','IV53','KA9','ML2','PA35','PA8','PH49','AB33','DG5','EH4','FK3','G61','IV12','IV54','KW1','ML3','PA36','PA9','PH5','AB34','DG6','EH40','FK4','G62','IV13','IV55','KW10','ML4','PA37','PH1','PH50','AB35','DG7','EH41','FK5','G63','IV14','IV56','KW11','ML5','PA38','PH10','PH6','AB36','DG8','EH42','FK6','G64','IV15','IV6','KW12','ML6','PA39','PH11','PH7','AB37','DG9','EH43','FK7','G65','IV16','IV63','KW13','ML7','PA4','PH12','PH8','AB38','EH1','EH44','FK8','G66','IV17','IV7','KW14','ML8','PA40','PH13','PH9','AB39','EH10','EH45','FK9','G67','IV18','IV8','KW15','ML9','PA41','PH14','TD1','AB41','EH11','EH46','G1','G68','IV19','IV9','KW16','NE70','PA42','PH15','TD10','AB42','EH12','EH47','G11','G69','IV2','KA1','KW17','NE71','PA43','PH16','TD11','AB43','EH13','EH48','G12','G70','IV20','KA10','KW2','PA1','PA44','PH17','TD12','AB44','EH14','EH49','G13','G71','IV21','KA11','KW3','PA10','PA45','PH18','TD13','AB45','EH15','EH5','G14','G72','IV22','KA12','KW5','PA11','PA46','PH19','TD14','AB51','EH16','EH51','G15','G73','IV23','KA13','KW6','PA12','PA47','PH2','TD15','AB52','EH17','EH52','G2','G74','IV24','KA14','KW7','PA13','PA48','PH20','TD2','AB53','EH18','EH53','G20','G75','IV25','KA15','KW8','PA14','PA49','PH21','TD3','AB54','EH19','EH54','G21','G76','IV26','KA16','KW9','PA15','PA5','PH22','TD4','AB55','EH2','EH55','G22','G77','IV27','KA17','KY1','PA16','PA6','PH23','TD5','AB56','EH20','EH6','G23','G78','IV28','KA18','KY10','PA17','PA60','PH24','TD6','DD1','EH21','EH7','G3','G79','IV3','KA19','KY11','PA18','PA61','PH25','TD7','DD10','EH22','EH8','G31','G80','IV30','KA2','KY12','PA19','PA62','PH26','TD8','DD11','EH23','EH80','G32','G81','IV31','KA20','KY13','PA2','PA63','PH3','TD9','DD2','EH24','EH9','G33','G82','IV32','KA21','KY14','PA20','PA64','PH30','ZE1','DD3','EH25','EH95','G34','G83','IV36','KA22','KY15','PA21','PA65','PH31','ZE2','ZE3']

  
  // verify forms
  $('form.verify').listenForChange().submit(function(e){
    has_error = false;
    $(this).find('.required input, .required textarea').each(function(e){
      value = $.trim($(this).val());
      if (value == '') {
        has_error = true;
        $(this).parents('.required').addClass('error');
      }
      else  {
        $(this).parents('li.required').removeClass('error');
      }
    })
    // verify order postcode
    if ($(this).hasClass('order')) {
      postcode_input = $(this).find('input[name=postcode]');
      postcode = jQuery.trim(postcode_input.val());
      
      if (postcode != '') {
        $('#postcode_error').remove();
        postcode_input.parents('.required').removeClass('error');

        valid_postcode = false;
        for (var i = valid_postcodes.length - 1; i >= 0; i--){
          re = new RegExp('^'+valid_postcodes[i],'ig');
          if (re.test(postcode)) { valid_postcode = true };
        };
        if (!valid_postcode) {
          postcode_input.parents('.required').addClass('error');
          postcode_input.after('<span class="error" id="postcode_error">We can only deliver printed materials to Scottish postcodes.</span>');
          has_error = true;
        }
      };
    };
    
    $(this).find('p.errors').remove();
    if (has_error) {
        $(this).find('h3.legend, p.legend').after('<p id="error_message" class="errors">We\'ve found errors in the form below, please correct these to send the form.</p>');
        $.scrollTo('#error_message',500);
    }
    
    return !has_error;
  });
  
  // prettyPhoto
  $("a[rel^='prettyPhoto']").prettyPhoto({
    hideflash: true,
    theme: 'facebook' /* light_rounded / dark_rounded / light_square / dark_square / facebook */
  });

  
// main navigation animation
  $('#c li:not(.here) a').css({
    backgroundPosition: "0 0"
  }).mouseover(function(){
    $(this).stop().animate({backgroundPosition:"(0 -246px)"}, {duration:300})
  }).mouseout(function(){
    $(this).stop().animate({backgroundPosition:"(0 0)"}, {duration:300})
  });
  
// Write class into markup for Drugs A-Z List 
  $("#alphabet ol").addClass("letters") 
  
// Add close link into Drugs A-Z drop-down list markup      
 // $("#alphabet ol ul").prepend("<li class='close' title='Close this panel'>close</li>") 
  
// Show/hide secondary nav
/*
$("#alphabet ol>li>a").hover(function(){
	$(this).siblings('ul').fadeIn('fast');
	$(this).siblings('ul').appendTo($(this));
},function(event){ // hide balloon again
	$("#alphabet ol ul").fadeOut('slow');
	$("#alphabet ol li a ul").appendTo($(this).parent());
});*/

/*
	Bubble popup
*/

    $(function () {
        $('.bubbleInfo').each(function () {
            var distance = 10;
            var time = 100;
            var hideDelay = 100;

            var hideDelayTimer = null;

            var beingShown = false;
            var shown = false;
            var trigger = $('.trigger', this);
            var info = $('.popup', this).css('display', 'none');


            $([trigger.get(0), info.get(0)]).mouseover(function () {
		info.css('display', 'block');
                return false;
            }).mouseout(function () {
            	info.css('display', 'none');
                return false;
            });
        });
    });



  
//close Drugs A-Z list on event 
  //$("#alphabet ol ul li.close").click(function(){ 
  //  $("#alphabet ol ul").slideUp()  
  //});

  
// clears text input box/textareas of initial text  
  $(".clearInput").focus(function() { 
    $(this).attr("value","");
  });
  
// apply "here" style to news section if active
  if (document.location.href.match(/talk\/news/)) {
    $("a[href='/talk/news/']").parents('li.sub_level_0').addClass('here');
  };

// disable link to url for Primary nav  
   //    $("#home #c>li>a").click(function(event){ 
   //        event.preventDefault();  
   // });

// hide the secondary nav initially
   // $("#c ul").hide()
 //  $("#c li.here ul").show()
  $("#a_z").addClass("noScipt")
  
// show/hide the secondary nav on event 
   // $("#c li").click(function(){
   //  $("#c li").children('ul').hide()
   //  $(this).children('ul').show()
   //  // slide the alphabet panel down to accommodate secondary nav
   //  $("#a_z").animate({ 
   //     marginTop: "4.5em"
   //    }, 500 ); 
   //  });

//  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
  
// vertical scrolling for left-hand column Drugs A-List 
  $(".vertical").addClass("scrollable"); 
  $("div.scrollable").scrollable({
    vertical:true, 
    size: 3   
  }).mousewheel(); // use mousewheel plugin
  
// overlay
  $(".vid").colorbox({iframe:true, innerWidth:410, innerHeight:238}); 
	
	
//apply styling to odd-numbered rows in all tables
	//$("tr:odd").css("background-color", "#eeeeee");
//style text in first column of all tables
	// $("tr td:first-child").css("font-size", "3em");
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
// CV ------------------------------------------------------------------------
	 
	 
	 
	 
	 
	 
	 


// Point 3, drugs layout --------------------------------------------

    // so degrades gracefully
    $('#drugsAZ #wrapper .drug_details .grid_5 .drug_line').css('width', '610px');

    if ($('#drugsAZ #wrapper .drug_details .grid_5 h2, #drugsAZ #wrapper .drug_details .grid_3 h2:first').length) {
        var pos = $('#drugsAZ #wrapper .drug_details .grid_5 h2, #drugsAZ #wrapper .drug_details .grid_3 h2:first').position();
        var pos2 = $('#content.drug_details').position();
        //alert(co(pos));


        if ($.browser.msie && $.browser.version < 8)$('#drugsAZ #wrapper .drug_details .grid_3#sidebar').css('padding-top', (pos.top - pos2.top + 13));
        else $('#drugsAZ #wrapper .drug_details .grid_3#sidebar').css('padding-top', (pos.top - pos2.top - 7));
    }

     // Point 4, drugs a-z sidebar nav --------------------------------------------
         if ($('#drugsAZ').length) { // check right section, might avoid errors elsewhere

             var url, url_array, drug, subsection, this_li, dazv_hi, href;

             // just scrolling
             $('.nextPage').click(function(){
                 $('#vert_az').scrollTo('+=265px', 500);
             });



             $('.prevPage').click(function(){
                 $('#vert_az').scrollTo('-=265px', 500);
             });

             // just for hover, keeps highlight if set already
             $('#scrollWrapper #vert_az ol.items li').hover(function() { // doubt that level of spec needed
                 $(this).css('background-color', '#eeeeee'); // just add class doesn't add style?
             }, function() {
                 if (!$(this).hasClass('dazv_hi')) $(this).css('background-color', 'transparent');
             });

             //store last clicked choice in cookie
             li = null;
             li = $('.items li').click(function() {
                 $.cookie("dazv_hi", li.index(this));
             });

             // get broken-down url info - strip trailing slash or will mess up url_array
             url = document.location.href;
             if (url.charAt(url.length - 1) == '/') url = url.substr(0, url.length - 1);
             var url_array = url.split("/").slice(-2);

             // Business end. Cookie to highlight. Read cookie as quick as poss then use var by design
             if (url_array.length == 2) { // just checking
                 if (url_array[0] == 'drugs_a-z') { // in drugs page
                     subsection = url_array[0]
                     drug = url_array[1];

                     // dazv_hi = $.cookie("dazv_hi");
                     // $.cookie("dazv_hi", null);

                     if ($.cookie("dazv_hi")) { // User coming from menu choice. != null for 0 index not needed unlike php?). 
                         //console.log('a');
                         dazv_hi = $.cookie("dazv_hi");
                         $.cookie("dazv_hi", null);
                         $.cookie("dazv_refresh", dazv_hi);
                         $.cookie("dazv_refresh_url", url);
                     } else if (($.cookie("dazv_refresh") && $.cookie("dazv_refresh_url") == url)) { // highlight persists with refresh
                         //console.log('b');
                         dazv_hi = $.cookie("dazv_refresh");
                     } else { // cold-calling - first in list that mtaches
                         href = '/learn/'+url_array[0]+'/'+url_array[1];

                         var synonyms = Array();

                         var synonym, link_href;
                         var synonym_found = false;
                         $('ol.items').find('a').each(function(i) {
                             link_href = $(this).attr('href');

                             // Find canonical entry given synonyms - even if only works in most cases, still OK
                             synonym = $(this).text();
                             synonym = synonym.toLowerCase();
                             synonym = synonym.replace(/ /g,"_");
                             if (href == link_href) synonyms.push(i);
                             if (synonym == drug) {
                                 synonym_found = true;
                                 dazv_hi = i;
                                 return false;
                             } 
                         });

                         if (!synonym_found) { // just go for first in list, doesn't really matter
                             dazv_hi = synonyms[0];
                         }

                         //console.log(synonyms);
                     }

                     if (($.cookie("dazv_refresh") && $.cookie("dazv_refresh_url") != url)) { // just to be tidy
                         //console.log('c');
                         $.cookie("dazv_refresh", null);
                         $.cookie("dazv_refresh_url", null);
                     }

                 } else if (url_array[1] == 'drugs_a-z') { // in drugs a-z homepage
                     $.cookie("dazv_hi", null);
                     $.cookie("dazv_refresh", null);
                     $.cookie("dazv_refresh_url", null);
                     subsection = url_array[1];
                     drug = null;
                 }
             }

             // actually highlight and move
             this_li = $('.items li')[dazv_hi];
             $(this_li).addClass('dazv_hi');
             $('#vert_az').scrollTo('.dazv_hi', 500);
             $(this_li).css('background-color', '#eeeeee'); // just add class doesn't add style?   
         }



     // Point 5, order process ----------------------------------------------------

         if ($('#basket_count').length) {
             var bs = $('#basket_count').html();
             //$('#nav_sub li.sub_level_0.last > a').appendTo($('#basket_count'));
             $('#basket_count').appendTo($('#nav_sub li.sub_level_0.last > a'))
             $('#basket_count').show();


             // var c = $('#basket_count').text();
             // var l = $('#nav_sub li.sub_level_0.last > a')[0];
             // var lt = $('#nav_sub li.sub_level_0.last > a:first').text();
             // var n = lt + c;

             //$('#b').show();
             //bs.show();
             //$(bs).appendTo('#nav_sub li.sub_level_0.last > a');
             //$(l).(text(function(i, t) {return t + n}));

             // $('#nav_sub li.sub_level_0.last').css('margin-bottom', '-5px')
             //bc = $('#basket_count')[0];
             //bc = $(bc).html();
             //console.log(bc);
             //$('#nav_sub li.sub_level_0.last > a')[0].html() += bc;
             //console.log($('#nav_sub li.sub_level_0.last > a').html());
         }



     // Point 7, tidy listing -----------------------------------------------------
         if ('body#get_help') {
             $(".toggle_container").hide(); 

         	$("h2.trigger").toggle(function() {
         		$(this).addClass("active");
         		}, function () {
         		$(this).removeClass("active");
         	});

         	$("h2.trigger").click(function() {
         		$(this).next(".toggle_container").slideToggle("slow");
         	});
             // above moved from page itself

             // just can't implement in css quick enough, so js it is just now
             $('#get_help h2.trigger').css('margin-top', '7px');
             $('#get_help h2.trigger:first').css('margin-top', '-4px');
             $('#get_help h2.trigger').css('margin-bottom', '7px');
         }




     // Point 8, nice table enhancements, -----------------------------------------
         $(".nice_table tr td:first-child").css("vertical-align", "top");
         $(".nice_table tr td:first-child").css("padding-top", "5px"); // compromise FF liked 4, everyone else 6
         $(".nice_table tr td:first-child").css("text-align", "center");

         // for IE6/7
         $(".nice_table tr td").css("vertical-align", "top");    
         $(".nice_table tr:odd td").css("background-color", "#eeeeee");


	 
	 
	 
	 

})

