
$(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>');
        }
      };
    };
    
    $(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 ul").hide()
  $("#alphabet ol>li>a").click(function(event){
    event.preventDefault();
    $("#alphabet ol ul").slideUp()
    $(this).siblings('ul').slideDown()
  });
  
//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: 10
    //onSeek: function(){console.log('x');}
  }).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 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 (due to editor-caused errors) - hence if which might avoid errors, prob ok without tho
        
        // 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");
    
    
    
    
    //$('#scrollWrapper .scrollable ol.items')
    
    // var orig_sh = $('.scrollable')[0].scrollHeight;
    // var orig_list_height = $('.items')[0].scrollHeight;
    // $('#items_wrapper').css('height', orig_list_height);
    //$('.items').css('height', '2000em');
    //if (orig_sh > orig_sh/2) $('.scrollable')[0].scrollHeight = orig_sh/2;
    
    // console.log($('.scrollable')[0].scrollHeight);
    // console.log($('.scrollable').css('height'));
    //var h;
    // $('.items li').each(function(){
    //     h += $(this).css('height');
    // 
    // });
    // console.log(h);
    // $('#drugsAZ .nextPage, #drugsAZ .prevPage').click(function(){
    //     if ($('.scrollable')[0].scrollTop > orig_sh) $('.scrollable')[0].scrollTop = orig_sh;
        
        
    //     //$('.scrollable')[0].scrollHeight = $('.scrollable')[0].scrollHeight/2;
    //     var st = $('.scrollable')[0].scrollTop;
    //     var new_sh = $('.scrollable')[0].scrollHeight;
    //     var diff = orig_sh - new_sh;
        //
    //     // off = $('.scrollable').offset();
    //     // pos = $('.scrollable').position();
    //     // console.log(off.top);
    //     // console.log(pos.top);
    //     
    //     var new_sh = $('.scrollable ').attr("scrollHeight");
    //     //console.log('new_sh:' + new_sh);
    //     var diff = orig_sh - new_sh;
    //     console.log('new_sh'+new_sh);
    //     console.log('d'+diff);
    //     console.log('st'+$('.scrollable')[0].scrollTop);
    //     $('.scrollable').attr('scrollTop', diff);
    //     
    //     //if (new_sh < orig_sh/2) 
    //     
    //     //console.log($('.scrollable').attr('scrollTop'));
    //     //console.log($('.scrollable').attr('scrollTop'));
    //     //console.log($('ol.items').position());
    //     //alert($('.scrollable').length);
    //     //console.log($('ol.items').height());
    //     // console.log($('.scrollable').height());
    //     // console.log($('.scrollable').height());
    //});  
   
   
   
   
   
   
   
   
   
   
   
    // jQuery.fn.extend({
    //   scrollTo : function(speed, easing) {
    //     return this.each(function() {
    //       var targetOffset = $(this).offset().top;
    //       $('html,body').animate({scrollTop: targetOffset}, speed, easing);
    //     });
    //   }
    // });
    // 
    // $('#my-el').scrollTo(1000);

   
   
    
    
    if ($('#drugsAZ').length) {
        // $("#vert_az").jScrollPane({ 
        //     scrollbarWidth: 10,
        //     wheelSpeed: 30
        // });
        $('.nextPage').click(function(){
            $('#vert_az').scrollTo('+=265px', 500);
        });
        
        $('.prevPage').click(function(){
            $('#vert_az').scrollTo('-=265px', 500);
        });
            
        $('#scrollWrapper #vert_az ol.items li').hover(function() { // doubt that level of pec needed
            $(this).css('background-color', '#fafafa');
        }, function() {
            if (!$(this).hasClass('drugs_az_vert_high')) $(this).css('background-color', 'transparent');
        });
        
        
        li = $('.items li').click(function(){
            $.cookie("drugs_az_vert_high", li.index(this));
            //return false;
        });
        
        if ($('#vert_az').length) {
            //console.log($.cookie("drugs_az_vert_high"));
        }
        
        if ($.cookie("drugs_az_vert_high")) {
            
            var davh = $.cookie("drugs_az_vert_high");
            //alert(davh);
            var this_li = $('.items li')[davh];
            $(this_li).css('background-color', '#efefef');
            $(this_li).addClass('drugs_az_vert_high');
            $('#vert_az').scrollTo('.drugs_az_vert_high', 500);
            //console.log($('.drugs_az_vert_high')[0].scrollHeight);
        }
        
        
        //
        
        
        
    }

    // if (document.location.href.match(\/drugs_a-z//*\/)) {
    //     alert(document.location.href);
    // }
    
    // url_array = document.location.href.split("/").slice(-2);
    // alert(url_array);
    // if (url_array.length == 2 && url_array[0] == 'drugs_a-z') {
    //     var drug = url_array[1];
    //     alert(drug);
    // }
            
            
            
            
            
            
    
});
