//Equal height boxes
jQuery(document).ready(function($){ 	
	function equalHeight(group) {
		tallest = 0;
		group.each(function() {
			thisHeight = $(this).height();
			if(thisHeight > tallest) {
				tallest = thisHeight;
			}
		});
		group.height(tallest);
	}

	equalHeight($ ( ".focus_boxes .box" ) );
	
});

//Alpha sort
$(function() { $('#alpha_sort > .alpha').tabs();});

//Tweets
function twitterCallback2(twitters) {
  var statusHTML = [];
  for (var i=0; i<twitters.length; i++){
    var username = twitters[i].user.screen_name;
    var status = twitters[i].text.replace(/((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g, function(url) {
      return '<a href="'+url+'">'+url+'</a>';
    }).replace(/\B@([_a-z0-9]+)/ig, function(reply) {
      return  reply.charAt(0)+'<a href="http://twitter.com/'+reply.substring(1)+'">'+reply.substring(1)+'</a>';
    });
    statusHTML.push('<li><span>'+status+'</span> <a href="http://twitter.com/'+username+'/statuses/'+twitters[i].id+'">'+relative_time(twitters[i].created_at)+'</a></li>');
  }
  document.getElementById('twitter_update_list').innerHTML = statusHTML.join('');
}

function relative_time(time_value) {
  var values = time_value.split(" ");
  time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
  var parsed_date = Date.parse(time_value);
  var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
  var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
  delta = delta + (relative_to.getTimezoneOffset() * 60);

  if (delta < 60) {
    return 'less than a minute ago';
  } else if(delta < 120) {
    return 'about a minute ago';
  } else if(delta < (60*60)) {
    return (parseInt(delta / 60)).toString() + ' minutes ago';
  } else if(delta < (120*60)) {
    return 'about an hour ago';
  } else if(delta < (24*60*60)) {
    return 'about ' + (parseInt(delta / 3600)).toString() + ' hours ago';
  } else if(delta < (48*60*60)) {
    return '1 day ago';
  } else {
    return (parseInt(delta / 86400)).toString() + ' days ago';
  }
}




//Older jquery

function newVoucherMouseOver(promotion_pk){
    $("#voucher_code_"+promotion_pk).fadeIn("normal");
    return true;
}

function newVoucherMouseOut(promotion_pk){
    $("#voucher_code_"+promotion_pk).fadeOut("fast");
    return true;
}

function newVoucherClick(promotion_pk) {
    $("#voucher_code_"+promotion_pk).fadeIn("normal");
    $("#voucher_button_"+promotion_pk).attr('onmouseout', '');
    $("#voucher_button_"+promotion_pk).attr('onmouseover','');
    $("#voucher_button_"+promotion_pk).attr('onclick','');
    $("#voucher_title_"+promotion_pk).attr('onmouseout', '');
    $("#voucher_title_"+promotion_pk).attr('onmouseover', '');
    $("#voucher_title_"+promotion_pk).attr('onclick', '');
    $("#voucher_buttoncode_"+promotion_pk).attr('onclick', '');
    $("#voucher_post_"+promotion_pk).fadeIn("normal");
    var link = $("#voucher_button_"+promotion_pk).attr('href'), 
    opt = 'toolbar=1,location=1,directories=1,scrollbars=1,resizable=1,status=1,menubar=1', 
    new_window = window.open(link, '_blank', opt);
    new_window.blur();
    return true;
}


function listingVoucherClick(slugged_merchant, url) {        
        var NewWin = window.open(url, '_blank')
        NewWin.blur();
        $("span.top_"+slugged_merchant).hide();
        $("span.btm_"+slugged_merchant).show();
}


function descriptionMore()
// Handles the "more" description function
{
    //Remove plus image
    $('p#description a').remove();
    //Remove hellip (3 dots)
    $('p#description em').remove();
    //Hide the whole thing
    $('p#description').hide();
    //Save content (the hidden more text)
    var more_content     = jQuery.trim($('p#description .description_more_text').html());
    //Then remove before saving existing content
    $('p#description .description_more_text').remove();    
    var existing_content = jQuery.trim($('p#description').html());
    //Update the content
    $('p#description').html(existing_content+' '+more_content);    
    //Fade back in
    $('p#description').fadeIn();
}

function voucherMore(pk)
// Handles the "more" voucher description function
// Fairly similar to descriptionMore() above except takes a pk as a parameter
{
    var id_string = "voucher_description_"+pk;
    $('span#'+id_string).hide();
    $('span#'+id_string+' a.voucher_more').remove();
    $('span#'+id_string+' em').remove();
    var more_content = jQuery.trim($('#'+id_string+' cite.voucher_more_text').html());
    $('#'+id_string+' cite.voucher_more_text').remove();
    var existing_content = jQuery.trim($('#'+id_string).html());
    $('span#'+id_string).html(existing_content+' '+more_content);
    $('span#'+id_string).fadeIn();
}

//Search re-direct I think
function redirectSearchURL(frm)
{
    var url = '';
    var URLString = "";
    $(':input', '#'+frm).each(function() {
        if (this.name=='by_category' && this.value!='') url = '/'+$('#'+this.id+' :selected').text().toLowerCase().replace(/\s+/g,'-').replace(/[^a-z0-9-]/g,'').replace(/--/g,'-')+'/';
        var type = this.type;
        var tag = this.tagName.toLowerCase();
        if ((type == 'text' || type == 'hidden' || tag == 'select') && this.name!='by_category' && this.value != "") URLString += this.name+"="+this.value.replace(/ /g,"+")+"&";
    });
    
    window.location.href=url+"?"+URLString.substr(0,URLString.length-1);
}


var timeout    = 800;
var closetimer = 0;
var ddmenuitem = 0;

function jsddm_open()
{  jsddm_canceltimer();
   jsddm_close();
   ddmenuitem = $(this).find('ul').css('visibility', 'visible');}

function jsddm_close()
{  if(ddmenuitem) ddmenuitem.css('visibility', 'hidden');}

function jsddm_timer()
{  closetimer = window.setTimeout(jsddm_close, timeout);}

function jsddm_canceltimer() {
    if(closetimer) {
        window.clearTimeout(closetimer);
        closetimer = null;
    }
}

document.onclick = jsddm_close;

function submitSort(sort_order) {
    document.frm_sort_order.sort_order.value = sort_order;
    document.frm_sort_order.submit();
}

//Open code in new window
function voucherClick(url) {
    var NewWin = window.open(url, '_blank')
    NewWin.blur();
    $("span.code_top").hide();
    $("span.code_bottom").show();
}

//Drop downs
$(document).ready(function() {
    $('div.sub_navigation #cssdropdown > li').bind('mouseover', jsddm_open);
    $('div.sub_navigation #cssdropdown > li').bind('mouseout',  jsddm_timer);
});

//Fade menu
$(document).ready(function(){
	$("#merchanty #container #nav li a").fadeTo("slow", 0.6); // This sets the opacity of the thumbs to fade down to 30% when the page loads
	$("#merchanty #container #nav li a").hover(function(){
	$(this).fadeTo("slow", 1.0); // This should set the opacity to 100% on hover
},function(){
	$(this).fadeTo("slow", 0.6); // This should set the opacity back to 30% on mouseout
   	});
});
