//alert('coffee.js loaded');

// load the id/serial images
jQuery(document).ready(function() {
  //jQuery('div#product-id div.example').css('outline','1px solid red');
  //jQuery('div#product-serial div.example').css('outline','1px solid yellow');
  
  var url = window.location.href;
  var url_query = url.split('?');
  var query = url_query[1];
  //alert(query);
  
  
   var lightboxHTML = '<div id="overlay" ></div>' +
  '<div id="lightbox" title="click anywhere to close" >' +
  '<p>click to close</p>' +
  '<img id="example" src="../../sites/all/themes/coffee/images/id_example.jpg" >' +
  '</div>';
  
  //jQuery(lightboxHTML).appendTo('div#product-id div.example').hide();
  jQuery(lightboxHTML).appendTo('div#content-area').hide();
  
  jQuery('div#product-id div.example').click(function(e){
    e.preventDefault();
    jQuery('#lightbox').show();    
  });
  
  jQuery('div#product-serial div.example').click(function(e){
    e.preventDefault();
    jQuery('#lightbox img#example').attr('src','../../sites/all/themes/coffee/images/serial_example.jpg');
    jQuery('#lightbox').show();    
  });  
  
  jQuery('#lightbox').click(function(e){ e.preventDefault(); jQuery('#lightbox').hide(); });

  jQuery('a#password').click(function(e){
    e.preventDefault();
    //alert("xxxx");
    jQuery('form#coffeeclub-user-password-retrieval-form').css('display','block');
    // show password form
  });
  
    // user canceled passwords request box
  jQuery('a#pwd_cancel').click(function(e){
    e.preventDefault();
    jQuery('form#coffeeclub-user-password-retrieval-form').css('display','none');
  });  
  // show password retrieval aknowedgement
  if(query == 'pwr=akw'){
      //jQuery('form#coffeeclub-user-password-retrieval-form').css('display','block');
      var msg = 'Thank you! A temporary password and instructions have been sent to your registered email address.'
      jQuery('form#coffeeclub-user-password-retrieval-form').replaceWith('<div id="pws-akw" >'+msg+'<div><a href="login" >close</a></div></div>');
      

      //
  }
  
  // email not recognised
  if(query == 'pwr=ukw'){
        jQuery('form#coffeeclub-user-password-retrieval-form').css('display','block');
        jQuery('form#coffeeclub-user-password-retrieval-form div#not-recognised').css('display','block');
  }



});



// detect users model selection and reveal the correct product number type (id or serial)
jQuery(document).ready(function() {
  var choice = "";
  var pass = false;
  var url = window.location.href;
  var url_query = url.split('?');
 //alert(url_query[0]);  
  var url_parts = url_query[0].split('/');
 //alert(url_parts);    
 //alert(url_parts[url_parts.length-2]);
 
 var url_sig = url_parts[url_parts.length-2]; // significalt part of the url

  // function
  function check_product_code(){
    
    // serial
    if(choice=="serial"){
      var val = jQuery('#edit-product-serial').val();
      var len = val.length;
      val = val-0;
      if(len == 5 && !isNaN(val)){pass = true;}else{ pass = false; }
    }
    
    // id
    if (choice=="id"){
      var id="";
      for( var i=1; i<20; i++){
         p = jQuery('#edit-product-id-'+i).val();
         id = id+p+"";
      }
      
      if(id.length==19){ //it is the right length
         var a = id.substring(0,6); a = a-0;
         var b = id.substring(6,8); b = b+"";        
         var c = id.substring(8,19); c = c-0;        
         if( !isNaN(a) && !isNaN(c) && isNaN(b) ){ pass = true; }else{ pass = false;  }
      }
      
      
    }
    
    if(pass == false){ alert('You have an incorrect serial number. '); }
    
    //alert(pass);
    return pass;
  }
  // end function

  jQuery('input#edit-product-submit').click(function(){ return check_product_code(); });   


    jQuery('div#product-id').css('display','none');
  //if( url_sig!='account' ){   
    jQuery('div#product-serial').css('display','none');
  //} 
  
  jQuery('select#edit-model').change(function(){
   // this is repeated incase the user changes their mind and re-selects
   
  //if( url_sig!='account' ){    
      jQuery('div#product-id').css('display','none');
  //}       
      jQuery('div#product-serial').css('display','none');  
 
    product = jQuery('OPTION:selected',this).val();
    values = product.split('|');

   var btc = 4; // 8 dev, 4 for live site 
   
  //if( url_sig!='account' ){ 
   if(values[1]==btc){ jQuery('div#product-id').css('display','block'); choice = "id"; }
  //} 
        
   else{ jQuery('div#product-serial').css('display','block'); choice = "serial"; }
    
   
   
    });  
  
});


(function(jQuery){
    jQuery.fn.collapser= function(options, beforeCallback, afterCallback) {
        
        var defaults = {
            target: 'next',
			targetOnly: null,
            effect: 'slide',
			changeText: true,
			expandHtml: 'Expand',
			collapseHtml: 'Collapse',
			expandClass: '',
			collapseClass:''
        };
        
        var options = jQuery.extend(defaults, options);
		
		var expHtml,collHtml, effectShow, effectHide;
		
		if(options.effect == 'slide'){
			effectShow = 'slideDown';
			effectHide = 'slideUp';
		}else{
			effectShow = 'fadeIn';
			effectHide = 'fadeOut';
		}
		
		if(options.changeText == true){
			expHtml = options.expandHtml;
			collHtml = options.collapseHtml;
		}
		
		function callBeforeCallback(obj){
			if(beforeCallback !== undefined){
				beforeCallback.apply(obj);
			}
		}
		
		function callAfterCallback(obj){
			if(afterCallback !== undefined){
				afterCallback.apply(obj);
			}
		}
		
		function hideElement(obj, method){
			callBeforeCallback(obj);
			if(method == 1){
				obj[options.target](options.targetOnly)[effectHide]();
				obj.html(expHtml);
				obj.removeClass(options.collapseClass);
				obj.addClass(options.expandClass);
			}else{
				jQuery(document).find(options.target)[effectHide]();
				obj.html(expHtml);
				obj.removeClass(options.collapseClass);
				obj.addClass(options.expandClass);
			}
			callAfterCallback(obj);
		}
		
		function showElement(obj, method){
			callBeforeCallback(obj)
			if(method == 1){
				obj[options.target](options.targetOnly)[effectShow]();
				obj.html(collHtml);
				obj.removeClass(options.expandClass);
				obj.addClass(options.collapseClass);
			}else{
				jQuery(document).find(options.target)[effectShow]();
				obj.html(collHtml);
				obj.removeClass(options.expandClass);
				obj.addClass(options.collapseClass);
			}
			callAfterCallback(obj);
		}
		
		function toggleElement(obj, method){
			if(method == 1){
				if(obj[options.target](options.targetOnly).is(':visible')){
					hideElement(obj, 1);
				}else{
					showElement(obj, 1);
				}
			}else{
				if(jQuery(document).find(options.target).is(':visible')){
					hideElement(obj, 2);
				}else{
					showElement(obj, 2);
				}
			}
		}
		
		return this.each(function(){
		   
		   if(jQuery.fn[options.target] && jQuery(this)[options.target]()){
				jQuery(this).toggle(function(){		
					toggleElement(jQuery(this), 1);
				},function(){
					toggleElement(jQuery(this), 1);
				});	
				
		   }else{
			   
			   jQuery(this).toggle(function(){
					toggleElement(jQuery(this), 2);
				},function(){
					toggleElement(jQuery(this), 2);
				});
		   }
		   
		   // Initialize  
		   if(jQuery.fn[options.target] && jQuery(this)[options.target]()){
				if(jQuery(this)[options.target]().is(':hidden')){
					jQuery(this).html(expHtml);
					jQuery(this).removeClass(options.collapseClass);
					jQuery(this).addClass(options.expandClass);
				}else{
					jQuery(this).html(collHtml);
					jQuery(this).removeClass(options.expandClass);
					jQuery(this).addClass(options.collapseClass);
				}
			}else{
				if(jQuery(document).find(options.target).is(':hidden')){
					jQuery(this).html(expHtml);
				}else{
					jQuery(this).html(collHtml);
				}
			}
		   
        });
    };
    
})(jQuery);


$(function() {
	$('.lightbox a').lightBox();
});


jQuery(document).ready(function() {

  // checks the value of the serial text
  function check_serial_value(obj){
    var val = jQuery(obj).val();
    val = val-0;

    if(!isNaN(val)){
       jQuery(obj).css('outline','none');
    }else{
       jQuery(obj).css('outline','1px solid red');
    }
  
  }
  
  

 // checks the value of the id input boxes
  function check_id_value(obj){
  
    var val = jQuery(obj).val();
    var full_id =  jQuery(obj).attr('id');
    var id = full_id.split('-');
    id = id[3]; id = id-0;
    
    var type;
    if (!isNaN(val)){ type = 'number'; } // if not is not a number then is a number
    else{ type = 'character'; }    

    // only 2 slots are chars
    if( id==7 || id == 8 ){
      if( type == 'character' ){      // alphas
         jQuery(obj).css('outline','none');
      }else{
        jQuery(obj).css('outline','1px solid red');
      }    
    }else{
      if( type == 'number' ){ 
         jQuery(obj).css('outline','none');
      }else{
         jQuery(obj).css('outline','1px solid red');
      }     
    }
        

  }
  

  
  jQuery('#edit-product-serial').keyup(function(){ check_serial_value( this ); });
    
  // load all the product-id boxes
	for (i=1; i<20; i++){
		jQuery("#edit-product-id-"+i).attr("maxlength","1");
		//jQuery("#edit-product-id-"+i).css('border','1px solid red');
  	jQuery("#edit-product-id-"+i).keyup(function(){
  	var id = jQuery(this).attr('id');
    id = id.split('-');
    var j = id[3];
    j = j-0; j = j+1; j = j+"";

		//jQuery("#edit-product-id-"+j).css('border','1px solid green');
    jQuery("#edit-product-id-"+j).focus();
    check_id_value( this );  		
     								 	
  	});		
    				
	}// -- end for
	
	
  
  		

	
	jQuery("#edit-product-id-1").parent().css("margin-left","190px")

	jQuery("#changeTextMorning").stop().animate({ opacity: 0.5 }, 300);
	jQuery("#changeTextAnyTime").stop().animate({ opacity: 0.5 }, 300);
	jQuery("#changeTextEvening").stop().animate({ opacity: 0.5 }, 300);
	jQuery("#changeTextMorning").mouseover(function() {
	jQuery(this).stop().animate({ opacity: 1.0 }, 500);
	jQuery("#changeTextAnyTime").stop().animate({ opacity: 0.5 }, 500);
	jQuery("#changeTextEvening").stop().animate({ opacity: 0.5 }, 500);
	jQuery("#textBox").html("Morning - In the UK, we think nothing of drinking cappuccinos and lattes all day, and yet in Italy they would never dream of drinking a milky coffee such as a cappuccino past lunchtime! It is very much seen as a breakfast drink, but still contains a good shot of espresso to awaken the senses!");
});

	jQuery("#changeTextAnyTime").mouseover(function() {
	jQuery(this).stop().animate({ opacity: 1.0 }, 500);
	jQuery("#changeTextMorning").stop().animate({ opacity: 0.5 }, 500);
	jQuery("#changeTextEvening").stop().animate({ opacity: 0.5 }, 500);
	jQuery("#textBox").html("Any Time - Espresso is the perfect coffee to enjoy at any time, as it's a small yet intense shot of black coffee, meaning that you can enjoy all the flavour and aroma without filling up on vast quantities of coffee. It is also a common misconception that espresso has a larger amount of caffeine but filter coffee contains the most caffeine.");
});

	jQuery("#changeTextEvening").mouseover(function() {
	jQuery("#changeTextAnyTime").stop().animate({ opacity: 0.5 }, 500);
	jQuery("#changeTextMorning").stop().animate({ opacity: 0.5 }, 500);
	jQuery(this).stop().animate({ opacity: 1.0 }, 500);
	jQuery("#textBox").html("Evening - If you really love entertaining, why not invest in a Bean to Cup machine such as PrimaDonna, which enables you to make perfect espresso, lungo, cappuccino, latte macchiato, and caff&eacute; latte at the touch of just one button, leaving you with more time to spend with your guests!");
});

jQuery('.historyCollapse').collapser({
	target: 'prev',
	expandHtml: '<a href="" style="text-decoration:none"><span style="color:#FFFFFF">Click here to discover how Coffee was first made and by whom, from as far back as 600-800AD.</span></a>',
	collapseHtml: '<a href="" style="text-decoration:none">Click here to hide this section.</a>'
});

jQuery('.originsCollapse').collapser({
	target: 'prev',
	expandHtml: '<a href="" style="text-decoration:none"><span style="color:#FFFFFF">Click here to learn about when Coffee first made it\'s way to Europe in the 1600\'s and how it is still one of the most popular drinks today.</span></a>',
	collapseHtml: '<a href="" style="text-decoration:none">Click here to hide this section.</a>'
});

jQuery('.giftCollapse').collapser({
	target: 'prev',
	expandHtml: '<a href="" style="text-decoration:none"><span style="color:#FFFFFF">Click here to learn where coffee comes from and about it\'s natural habitat.</span></a>',
	collapseHtml: '<a href="" style="text-decoration:none">Click here to hide this section.</a>'
});

jQuery('.harvestCollapse').collapser({
	target: 'prev',
	expandHtml: '<a href="" style="text-decoration:none"><span style="color:#FFFFFF">Click here to find out about the picking process and how the beans are harvested.</span></a>',
	collapseHtml: '<a href="" style="text-decoration:none">Click here to hide this section.</a>'
});

jQuery('.aromaCollapse').collapser({
	target: 'prev',
	expandHtml: '<a href="" style="text-decoration:none"><span style="color:#FFFFFF">Click here if you have ever wondered why coffee has that unmistakable colour and specific aroma.</span></a>',
	collapseHtml: '<a href="" style="text-decoration:none">Click here to hide this section.</a>'
});


jQuery("#coffeeclub-enquiry-list-form").change(function (){
	val=jQuery("#coffeeclub-enquiry-list-form option:selected").attr("value");
	jQuery("#coffeeclub-enquiry-list-form li").css("display","block");
	jQuery("#coffeeclub-enquiry-list-form .type-"+val).css("display","none");
	jQuery("#coffeeclub-enquiry-list-form .status-"+val).css("display","none");
	jQuery("#coffeeclub-enquiry-list-form .message-"+val).css("display","none");
	jQuery("#coffeeclub-enquiry-list-form .reply-"+val).css("display","none");
});

jQuery(document).ready(function(){jQuery(".page-thecoffeeclub-limescale-check .limescale_txt").hide()})

jQuery(".page-thecoffeeclub-limescale-check .limescale_paragraph").click(function(){
var src = (jQuery(this).parent().children(".controller").attr("src") === "/sites/default/files/limescale_arrow.jpg")
                    ? "/sites/default/files/limescale_arrow_e.jpg" 
                    : "/sites/default/files/limescale_arrow.jpg";
    jQuery(this).parent().children(".controller").attr("src", src);
	jQuery(this).parent().children(".limescale_txt").slideToggle("slow")
})


jQuery(".page-thecoffeeclub-limescale-check .controller").click(function(){
var src = (jQuery(this).parent().children(".controller").attr("src") === "/sites/default/files/limescale_arrow.jpg")
                    ? "/sites/default/files/limescale_arrow_e.jpg" 
                    : "/sites/default/files/limescale_arrow.jpg";
    jQuery(this).parent().children(".controller").attr("src", src);
	jQuery(this).parent().children(".limescale_txt").slideToggle("slow")
})

jQuery(".videoThumbs").click(function(){
	jQuery(".videoTitle").text(jQuery(this).attr("title"))
})

/*
jQuery(".page-thecoffeeclub-address-product #coffeeclub-user-address #edit-submit-address").click(function(){
	jQuery(".page-thecoffeeclub-address-product #coffeeclub-user-product-register-form").css({
		"-ms-filter":"progid:DXImageTransform.Microsoft.Alpha(Opacity=100)",
		"filter":"alpha(opacity=100)",
		"opacity":"1"
	})
	return false;																	  
})

jQuery(".page-thecoffeeclub-account #coffeeclub-user-address #edit-submit-address").click(function(){
	jQuery(".page-thecoffeeclub-account #coffeeclub-user-registered-products-list-form").css({
		"-ms-filter":"progid:DXImageTransform.Microsoft.Alpha(Opacity=100)",
		"filter":"alpha(opacity=100)",
		"opacity":"1"
	})
	return false;
})

jQuery(".page-thecoffeeclub-membership #coffeeclub-user-membership #edit-submit-membership").click(function(){
	jQuery(".page-thecoffeeclub-membership #coffeeclub-user-product-register-form").css({
		"-ms-filter":"progid:DXImageTransform.Microsoft.Alpha(Opacity=100)",
		"filter":"alpha(opacity=100)",
		"opacity":"1"
	})
	return false;
})


jQuery(".description .example").lightBox({
	overlayBgColor: '#000',
	overlayOpacity: .6,
})
*/

});

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-5421349-1']);
_gaq.push(['_trackPageview']);

(function() {
  var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
  ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
  var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();

function loadVideoGallery(x){
	jQuery(".video-screen").html('<iframe width="380" height="287" src="'+x+'" frameborder="0" allowfullscreen></iframe>')
}

//"<!--[if !IE]>--><object type='application/x-shockwave-flash' data='"+x+"' width='380' height='280'><param name='movie' value='"+x+"' /><param name='quality' value='high' /><param name='bgcolor' value='#ffffff' /><param name='play' value='true' /><param name='loop' value='true' /><param name='wmode' value='opaque' /><param name='scale' value='showall' /><param name='menu' value='true' /><param name='devicefont' value='false' /><param name='salign' value='' /><param name='allowScriptAccess' value='sameDomain' /></object><!--<![endif]--><object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' width='380' height='280' id='sac' align='middle'><param name='movie' value='"+x+"' /><param name='quality' value='high' /><param name='bgcolor' value='#ffffff' /><param name='play' value='true' /><param name='loop' value='true' /><param name='wmode' value='opaque' /><param name='scale' value='showall' /><param name='menu' value='true' /><param name='devicefont' value='false' /><param name='salign' value='' /><param name='allowScriptAccess' value='sameDomain' /></object>"
