$ = jQuery;

//Inpu tips function
function InputHelperIn(obj, text) {
  if (obj.value == text) {
    $(obj).val('');
  }
}
function InputHelperOut(obj, text) {
  if (obj.value == '' || obj.value == text) {
    $(obj).val(text);
  }
}
function InputHelperCreate(obj, text) {
  $(obj).bind('focus',
  function() {
    InputHelperIn(this, text);
  }).bind('blur',
  function() {
    InputHelperOut(this, text);
  });
  InputHelperOut(obj, text);
}
 //togle color function
  gr_func = function(c, r, r1) {
	gr_src = $li.eq(c).find('img').attr('src');
	gr_rep = gr_src.replace(r, r1);
	gr_img = $li.eq(c).find('img').attr('src', gr_rep);
  }
$(document).ready(function() {
  //Partners greyscale images
  $('.view-partners-page_1 .views-field-field-photo-1').hover(function() {
	$(this).find('.gr').hide();
	$(this).find('.color').show();
  },
  function() {
	$(this).find('.color').hide();
	$(this).find('.gr').show();
  });

  //ROTATOR
 
//  $il = $('#rotator-wrap .view-slideshow .views-field-field-photo-crop .item-list');
//  $il.append('<a class="sl-pr" /><a class="sl-next" />');
//  $ul = $('#rotator-wrap .view-slideshow .views-field-field-photo-crop ul');
//  $li = $('#rotator-wrap .view-slideshow .views-field-field-photo-crop ul li');
//  w = $li.outerWidth();
//  lil = $li.length;
//  ulw = w * lil;
//  w2 = w / 2;
//  $ul.width(ulw);
//  var m;
//  ost = lil % 2;
//  if (ost == 0) {
//	m = (ulw / 2) - w2;
//  } else {
//	m = ulw / 2;
//  }
//  $ul.css({
//	'margin-left': -m
//  });
//  i = Math.round(m / w);
//  curr = (i - 1);
//  $li.eq(curr).addClass('act');
//  $pr = $('#rotator-wrap .sl-pr');
//  $next = $('#rotator-wrap .sl-next');
//  $li.not(':eq('+curr+')').each(function() {
//	gr_src = $(this).find('img').attr('src');
//	gr_rep = gr_src.replace(/900x405sc/g, '900x405sc_gr');
//	gr_img = $(this).find('img').attr('src', gr_rep);
//  })
  //ml = $li.eq(curr).find('img').attr('src');
  //rep = ml.replace(/900x405sc_gr/g, '900x405sc');
  //$li.eq(curr).find('img').attr('src', rep);
//  $pr.click(function() {
//
//	m = m - w;
//	if (m >= w2) {
//	  gr_func(curr, /900x405sc/g, '900x405sc_gr');
//	  curr--;
//	  gr_func(curr, /900x405sc_gr/g, '900x405sc');
//	  $li.removeClass('act');
//	  $li.eq(curr).addClass('act');
//	  $ul.animate({
//		'margin-left': -m
//	  },
//	  1000);
//	} else {
//	  m = w2
//	}
//        dir = 'b';
//  });
//  $next.click(function() {
//	m = m + w;
//	if (m <= ulw - w2) {
//	  $ul.animate({
//		'margin-left': -m
//	  },
//	  1000);
//	  gr_func(curr, /900x405sc/g, '900x405sc_gr');
//	  curr++;
//	  gr_func(curr, /900x405sc_gr/g, '900x405sc');
//	  $li.removeClass('act');
//	  $li.eq(curr).addClass('act');
//	} else {
//	  m = ulw - w2;
//	}
//        dir = 'f';
//  });

//Input tips
$name = $('.webform-client-form #webform-component-name input');
$email = $('.webform-client-form #webform-component-email input');
$phone = $('.webform-client-form #webform-component-phone input');
$comp = $('.webform-client-form #webform-component-company input');
$enq = $('.webform-client-form #webform-component-enquiry input');

$name.val(Drupal.t('Name:'));
InputHelperCreate($name, Drupal.t('Name:'));
$email.val(Drupal.t('Email address:'));
InputHelperCreate($email, Drupal.t('Email address:'));
$phone.val(Drupal.t('Telephone number:'));
InputHelperCreate($phone, Drupal.t('Telephone number:'));
$comp.val(Drupal.t('Company:'));
InputHelperCreate($comp, Drupal.t('Company:'));
$enq.val(Drupal.t('How can we help?:'));
InputHelperCreate($enq, Drupal.t('How can we help?:'));


//Submit by enter
$('#webform-client-form-11 input').keypress(function(e){
      if(e.which == 13){
       $('form#webform-client-form-11').submit();
       }
    });

// Display corousel navigation on mouse over.
$('#rotator-wrap').hover(
    function(){
      $('#rotator-wrap .jcarousel-prev').css('display', 'block');
      $('#rotator-wrap .jcarousel-next').css('display', 'block');
    },

    function(){
      $('#rotator-wrap .jcarousel-prev').css('display', 'none');
      $('#rotator-wrap .jcarousel-next').css('display', 'none');
    }
);

$('#block-block-4 a.description').click(function(){
  $('#block-block-4 a.description').css('display', 'none');
  $('#block-block-9 .content').slideDown();
  return false;
});

$('#block-block-9 a.description').click(function(){
  $('#block-block-9 .content').slideUp();
  $('#block-block-4 a.description').css('display', 'block');
  return false;
});

//if($('#rotator-wrap .view-slideshow .views-field-field-photo-crop ul')){
//  dir = 'f';
//  timeMsg();
//}

  $('.view-slideshow ul').jcarousel({
      auto: 6,
      scroll: 1,
      wrap: 'circular',
      animation: 'slow',
      itemVisibleInCallback: {
        onBeforeAnimation:  image_in_before,
        onAfterAnimation: image_in_after
      },
      itemVisibleOutCallback: {
        onBeforeAnimation: image_out_before,
        onAfterAnimation: image_out_before
      }
  });

  $('#rotator-wrap .jcarousel-prev').css('display', 'none');
  $('#rotator-wrap .jcarousel-next').css('display', 'none');

  function image_in_before(first, second, third, fourth){
    $(second).addClass('active');

    if($('.view-slideshow ul li.colour').length){
      //changetogc($('.view-slideshow ul li.colour'))
      $('.view-slideshow ul li.colour').removeClass('colour');
    }

     
  }

  function image_in_after(first, second, third, fourth){
    
    var i = 0;
    $('.view-slideshow ul li.active').each(function(){
        i++;
        if(i==2){
          if(($(this).hasClass('colour'))){
            
          }else{
            $(this).addClass('colour');
            //changetocolour($(this));
          }
        }
    })
  }

  function image_out_before(first, second, third, fourth){
    $(second).removeClass('active');
  }

  function changetocolour(item){
    var img = $(item).find('img')
    var source = $(img).attr('src');
    var newsource = source.replace('900x405sc_gr', '900x405sc')
    $(img).attr('src', newsource);
  }

  function changetogc(item){
    var img = $(item).find('img')
    var source = $(img).attr('src');
    var newsource = source.replace('900x405sc', '900x405sc_gr')
    $(img).attr('src', newsource);
  }


});



//function timeMsg(){
//    setTimeout("alertMsg()",6000);
//}
//
//function alertMsg()
//  {
//    if(dir=='f'){
//      if(curr < ($('#rotator-wrap .view-slideshow .views-field-field-photo-crop ul li').size()-1)){
//          $next.click();
//      }else{
//         dir = 'b';
//         $pr.click();
//      }
//    }else if(dir=='b'){
//      if(curr != 0){
//          $pr.click();
//      }else{
//         dir = 'f';
//         $next.click();
//      }
//    }
//    timeMsg();
//  }

;

