$(document).ready(function(){
	customize_navigation();
	customize_resellers_list();
	navigation();
	ajaxChb();
	imageZoom();
});
// image zoom ******************************************************************
function imageZoom(_obj) {
	if (/i-love/.test(window.location) || /postcards/.test(window.location)
			|| /publications/.test(window.location) || /shimmering-facts/.test(window.location)) {
		var width = 167;
		var height = 140;
		var margLeft = 0;
	} else {
		var width = 250;
		var height = 250;
		var margLeft = 13;
	}
	if (!_obj) _obj = document;
	var _imageHolder = $('div.image-holder', _obj);
	_imageHolder.each(function(){
		var _img = $('.image img',this);
		var _maxZoom = {
			'marginTop': -3 + 'px',
			'marginLeft': 3 + 'px',
			'width': (width + 20) + 'px',
			'height': (height + 20) + 'px'
		};
		_normZoom = {
			'marginTop': 0,
			'marginLeft': margLeft + 'px',
			'width': width + 'px',
			'height': height + 'px'
		};
		$(this).hover(function(){
			_img.animate(_maxZoom, {duration:200, queue:false});
		}, function(){
			_img.animate(_normZoom, {duration:200, queue:false});
		});
	});
}

// navigation ******************************************************************
function navigation(){
	var _navEl = $('ul.nav li > div.drop');
	var _durationSlide = 300;
	
	_navEl.each(function(i, _el){
		_el = $(_el);
		var _ul = $('div.slide', _el);
		var _parent = _ul.parent();
		_el.h = _ul.height();
		
		_parent.css({
			'height':0,
			'overflow':'hidden'
		});
		_ul.css({
			'marginTop':-_el.h,
			'overflow':'hidden'
		});
		
		_el.mouseenter(function(){
			this.hoverEl = true;
			$(this).addClass('hover');
			_parent.animate({height:_el.h},{queue:false,duration:_durationSlide});
			_ul.animate({marginTop:0},{queue:false,duration:_durationSlide});
		}).mouseleave(function(){
			this.hoverEl = false;
			var _this = this;
			_parent.animate({height:0},{queue:false,duration:_durationSlide});
			_ul.animate({marginTop:-_el.h},{queue:false,duration:_durationSlide, complete:function(){
				if (!_this.hoverEl)
					$(_this).removeClass('hover');
			}});
		});
	});
}

// ajax checkbox ***************************************************************
function ajaxChb(){
	var _form = $('.checkbox-list');
	var _checkboxList = $('.checkbox-list input[type="checkbox"]');
	var _ajaxHolder = $('div.ajaxHolder');
	var _ajaxTimer = false;
	
	_checkboxList.click(function(){
		if (_ajaxTimer) clearTimeout(_ajaxTimer);
		var _url = _form.attr('action');
		_ajaxTimer = setTimeout(function(){
			_ajaxHolder.css('opacity',0.3)
			$.ajax({
				url: _url,
				data: _form.serialize(),
				success: function(msg){
					_ajaxHolder.html(msg);
					Cufon.replace('.item h3 ', { fontFamily: 'Baskerville'});
					Cufon.replace('.paging ', { fontFamily: 'Baskerville', hover:true });
					Cufon.replace('.list-overview .info strong', { fontFamily: 'Baskerville'});
					Cufon.replace('.list-overview .info .more', {fontFamily: 'EngraversGothic BT', hover:true});
					_ajaxHolder.fadeTo(500, 1);
					imageZoom(_ajaxHolder);

					$('.open-lightbox').simpleLightbox({
						lightboxContentBlock: '.lightbox',
						faderOpacity: 0.5,
						faderBackground: '#000000',
						closeLink:'a.close',
						href:true,
						onClick: null
					});
				}
			});
		}, 1000);
	});
	_ajaxHolder.find('ul.paging a').live('click', function(){
		var _this = $(this);
		if (!_this.hasClass('active')) {
			_ajaxHolder.find('ul.paging a').removeClass('active');
			_this.addClass('active');
			Cufon.refresh('.paging');
			var _holder = _ajaxHolder.find('div.list-overview, div.items');
			_holder.css('opacity',0.3)
			
			_formdata = _form.serialize();
			if (_formdata.length) _formdata += "&";
			else _formdata = "";
			
			$.ajax({
				url: _this.attr('href'),
				data: _formdata+'paged='+_this.attr('rel'),
				success: function(msg){
					_holder.html(msg);
					Cufon.refresh('.item h3 ');
					Cufon.refresh('.list-overview .info strong');
					Cufon.refresh('.list-overview .info .more');
					_holder.fadeTo(500, 1);
					imageZoom(_ajaxHolder);
					
					$('.open-lightbox').simpleLightbox({
						lightboxContentBlock: '.lightbox',
						faderOpacity: 0.5,
						faderBackground: '#000000',
						closeLink:'a.close',
						href:true,
						onClick: null
					});
				}
			});
		}
		return false;
	});
}

function customize_navigation() {
	$('ul.nav > li.nosubmenu ul').remove();
	
	//customize dropdown
	$('ul.nav > li > ul').each(function(){
		$(this).find('a:first').addClass('parent');
		$(this).find('ul:first').wrap('<div class="drop"></div>').wrap('<div class="slide"></div>').before('<div class="drop-t"></div>').after('<div class="drop-b">&nbsp;</div>').wrap('<div class="drop-m"></div>').wrap('<div class="drop-content"></div>');
	});
	
	//customize 3rd level
	$('ul.nav > li > div > div > div > div > ul > li').each(function(){
		var children_count = $(this).find('ul:first > li').length;
		if (!children_count || (children_count <= 4)) return;
		$(this).addClass('two-column');
		var item_per_col = Math.round(children_count / 2);
		$(this).find('ul:first').wrap('<div></div>').after('<ul></ul>');
		$(this).find('ul:first > li').each(function(i,el){
			if (i >= item_per_col) {
				$(this).parent().parent().find('ul:last').append('<li>'+$(this).html()+'</li>');
				$(this).remove();
			}
		});
		
	});
	
	$('ul.notloaded').removeClass('notloaded');
}

function customize_resellers_list() {
	$('.list-holder').each(function(){
		var children_count = $(this).find('ul > li').length;
		if (!children_count) return;
		var item_per_col = Math.round(children_count / 2);
		$(this).find('ul').after('<ul></ul>');
		$(this).find('ul:first > li').each(function(i,el){
			if (i >= item_per_col) {
				$(this).parent().parent().find('ul:last').append('<li>'+$(this).html()+'</li>');
				$(this).remove();
			}
		});
		
	});
}
// shopping cart  step 2 ***************************************************************
function checkstep2() {
	
  var x = false; //if true proceed to step 3
	var shipTo = $('input[name=shipTo]:checked').val();
	
	if (!shipTo) {
		alert('Please choose a shipping option');
		return false;
	}
	
  var title = $("#title").val();
  if (typeof(title) != 'undefined' && title != '') {
    x=true;
  } else {
    alert('Please, fill all required fields!');
    $("select#title").focus();
    return false;
  }
  
  // invoice address
  var firstname = $("#firstname").val();
  if (typeof(firstname) != 'undefined' && firstname != '') {
    x=true;
  } else {
    alert('Please, fill all required fields!');
    $("input#firstname").focus();
    return false;
  }
  
  var lastname = $("#lastname").val();
  if (typeof(lastname) != 'undefined' && lastname != '') {
    x=true;
  } else {
    alert('Please, fill all required fields!');
    $("input#lastname").focus();
    return false;
  }
  
  var street = $("#street").val();
  if (typeof(street) != 'undefined' && street != '') {
    x=true;
  } else {
    alert('Please, fill all required fields!');
    $("input#street").focus();
    return false;
  }
  
  var zipcode = $("#postcode").val();
  if (typeof(zipcode) != 'undefined' && zipcode != '') {
    x=true;
  } else {
    alert('Please, fill all required fields!');
    $("input#zipcode").focus();
    return false;
  }
  
  var city = $("#city").val();
	var lettersOnly  = /^[a-zA-Z\u00C0-\u00ff\s\-]+$/;
  if (typeof(city) != 'undefined' && lettersOnly.test(city) && city != '') {
    x=true;
  } else {
    alert('Please, fill a valid city!');
    $("input#city").focus();
    return false;
  }
  
  var email = $("#email").val();
  var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
  if (reg.test(email) != false && email != '') {
    x=true;
   } else {
    alert('Please, enter valid email address!');
    $("input#email").focus();
    return false;
  }
	
	var phone = $('#telephone').val();
	if (phone) {
		var reg = /^\+[0-9]{2}\([0-9]{1}\)([0-9 ])*$/;
		if (reg.test(phone)) {
			x = true;
		} else {
			alert('Please, enter valid phone');
			return false;
		}
	}
	
  if ( shipTo == 'specified' ) {
    
    //////////////////////////////delivery address////////////
    
    var title2 = $("#title2").val();
    if (typeof(title2) != 'undefined' && title2 != '') {
      x=true;
    } else {
      alert('Please, fill all required fields!');
      $("select#title2").focus();
      return false;
    }
  
  
    var firstname2 = $("#firstname2").val();
    if (typeof(firstname2) != 'undefined' && firstname2 != '') {
      x=true;
    } else {
      alert('Please, fill all required fields!');
      $("input#firstname2").focus();
      return false;
    }
  
    var lastname2 = $("#lastname2").val();
    if (typeof(lastname2) != 'undefined' && lastname2 != '') {
      x=true;
    } else {
      alert('Please, fill all required fields!');
      $("input#lastname2").focus();
      return false;
    }
  
    var street2 = $("#street2").val();
    if (typeof(street2) != 'undefined' && street2 != '') {
      x=true;
    } else {
      alert('Please, fill all required fields!');
      $("input#street2").focus();
      return false;
    }
  
    var zipcode2 = $("#postcode2").val();
    if (typeof(zipcode2) != 'undefined' && zipcode2 != '') {
      x=true;
    } else {
      alert('Please, fill all required fields!');
      $("input#zipcode2").focus();
      return false;
    }
  
    var city2 = $("#city2").val();
		var lettersOnly  = /^[a-zA-Z\u00C0-\u00ff\s\-]+$/;
		if (typeof(city2) != 'undefined' && lettersOnly.test(city2) && city2 != '') {
      x=true;
    } else {
      alert('Please, fill all required fields!');
      $("input#city2").focus();
      return false;
    }
  
    var email2 = $("#email2").val();
		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    if(reg.test(email2) != false) {
      x=true;
    } else {
      alert('Please, enter valid email address!');
      $("input#email2").focus();
      return false;
    }
		
		var phone2 = $('#telephone2').val();
		if (phone2) {
			var reg = /^\+[0-9]{2}\([0-9]{1}\)([0-9 ])*$/;
			if (reg.test(phone2)) {
				x = true;
			} else {
				alert('Please, enter valid phone');
				return false;
			}
		}
    
  }
  
  if(x) {
		$('#addressForm').submit();
  }
	return false;
}

