$.cookies = function(name) {
  var curCookie = String(document.cookie);
  if (!window.__cookie || (window.__cookie != curCookie)) {
    window.__cookie = curCookie;
    window.__cookie_hash = {};
    $.each(curCookie.split(';'), function(i, cookie) {
      var c = $.trim(cookie), kv = c.split('=');
      window.__cookie_hash[kv[0]] = decodeURIComponent(kv[1]);
    });
  }
  if (name)
    return window.__cookie_hash[name];
  else
    return window.__cookie_hash;
};

$.setCookie = function(name, value, options) {
  options = options || {};
  var expires = '';
  if (options.expires && 
    (typeof options.expires == 'number' || options.expires.toGMTString)) {
    var date;
    if (typeof options.expires == 'number') {
      date = new Date();
      date.setTime(date.getTime() + (options.expires * 1000));
    } else {
      date = options.expires;
    }
    expires = '; expires=' + date.toGMTString();
    // use expires attribute, max-age is not supported by IE
  }
  var path = options.path ? '; path=' + options.path : '';
  var domain = options.domain ? '; domain=' + options.domain : '';
  var secure = options.secure ? '; secure' : '';
  document.cookie = [name, '=', encodeURIComponent(value), expires, path,
    domain, secure].join('');
};

$.deleteCookie = function(name) {
  document.cookie = name + "=;domain=" + document.domain + 
    ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
};

$.validateEmail = function(email) {
  return /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i.test(email);
}

$.validateWebsite = function(email) {
  return /^http:\/\/[A-Za-z0-9\.-]{3,}\.[A-Za-z]{2,}/.test(email);
}

function commentData() {
  var data = {
    author: $('#comment_author').val(),
    email: $('#comment_email').val(),
    website: $('#comment_website').val(),
    content: $('#comment_content').val()
  };
  return data;
}

function validateComment(data) {
  //validate author name
  if (data.author.length < 3) {
    alert('אנא הכנס שם בן 3 אותיות לפחות!');
    $('#comment_author').get(0).focus();
    return false;
  }
  
  //validate email (optional)
  if ((data.email != '') && !$.validateEmail(data.email)) {
    alert('אנא הכנס כתובת דואל חוקית!');
    $('#comment_email').get(0).focus();
    return false;
  }
  
  //validate website (optional)
  if ((data.website != '') && !$.validateWebsite(data.website)) {
    alert('אנא הכנס כתובת אתר חוקית!');
    $('#comment_website').get(0).focus();
    return false;
  }
  
  //validate content
  if (data.content.length < 3) {
    alert('אנא הכנס תוכן');
    $('#comment_content').get(0).focus();
    return false;
  }

  if (/\<[A-Za-z0-9\.-]+\</.test(data.content)) {
    alert('אנא הכנס תוכן ללא קודים או תגים');
    $('#comment_content').get(0).focus();
    return false;
  }
  
  return true;
};

function openWindow(url, opts) {
  opts.x = opts.x || ((window.screen.width/2) - (opts.width/2));
  opts.y = opts.y || ((window.screen.height/2) - (opts.height/2));
	var win = window.open(
	  url, 
	  opts.name,
	  "width="+         opts.width + 
	  ",height="+       opts.height + 
	  ",left=" +        opts.x + 
	  ",top=" +         opts.y + 
	  ",screenX=" +     opts.x + 
	  ",screenY=" +     opts.y + 
	  ",resizable=" +   ((opts.resizable == false)   ? 'no' : 'yes') + 
	  ",scrollbars=" +  ((opts.scrollbars == false)  ? 'no' : 'yes') + 
	  ",status=" +      ((opts.statusbar == false)   ? 'no' : 'yes') +
	  ',menubar=' +     ((opts.menubar == false)     ? 'no' : 'yes') +
	  ',toolbar=' +     ((opts.toolbar == false)     ? 'no' : 'yes') +
	  ',location=' +    ((opts.location == false)    ? 'no' : 'yes')
	);

	win.focus();
	return false;
}

function trackEvent(a, b, c, d) {
//  pageTracker._trackEvent(a, b, c, d);
  _gaq.push(['_trackEvent', a, b, c, d]);
}

//venue popup

$(document).ready(function() {
  $('.concert_date').hover(function() {
    //mouse in
    if (this.mout_timer) {
      clearTimeout(this.mout_timer);
      this.mout_timer = null;
    }
    
    var obj = this;
    obj.min_timer = setTimeout(function() {
      $(obj).children('.venue_popup').fadeIn(300);
      obj.min_timer = null;
    }, 150);
    
    // $(this).children('.venue_popup').fadeIn(500);
  }, function() {
    if (this.min_timer) {
      clearTimeout(this.min_timer);
      this.min_timer = null;
    }
    
    //mouse out
    var obj = this;
    obj.mout_timer = setTimeout(function () {
      $(obj).children('.venue_popup').fadeOut(100);
      obj.mout_timer = null;
    }, 250);
  });

  $('.fb_share_link').click(function() {
    var t = $(this).parents('h2').find('a:eq(1), span').text();
    var url = $(this).attr('href') + '&t=' + encodeURIComponent(t);;
    openWindow(url, {
      width: 626,
      height: 436,
      toolbar: false,
      status: false
    });
    return false;
  });
  
  $('.radio_link').click(function() {
    openWindow('/sounds/radio', {
      width: 480,
      height: 356,
      resizable: false,
      status: false,
      scrollbars: false,
      menubar: false,
      toolbar: false,
      location: false
    });
    return false;
  });
  
  $('.kartisan-link').click(function() {
    var show_id = $(this).attr('show_id');
	  pageTracker._trackEvent('Kartisan-links', 'Click', show_id);
    openWindow('https://kartisan.com/shows/' + show_id, {
      width: 700,
      height: 620,
      resizable: true,
      status: true,
      scrollbars: true,
      menubar: false,
      toolbar: false,
      location: false
    });
    return false;
  });
});
