jQuery.ajaxSetup({ 'beforeSend': function(xhr) {xhr.setRequestHeader("Accept", "text/javascript")} })

function _ajax_request(url, data, callback, type, method) {
    if (jQuery.isFunction(data)) {
        callback = data;
        data = {};
    }
    return jQuery.ajax({
        type: method,
        url: url,
        data: data,
        success: callback,
        dataType: type
        });
}

jQuery.extend({
    put: function(url, data, callback, type) {
        return _ajax_request(url, data, callback, type, 'PUT');
    },
    delete_: function(url, data, callback, type) {
        return _ajax_request(url, data, callback, type, 'DELETE');
    }
});

jQuery.fn.submitWithAjax = function() {
  this.unbind('submit', false);
  this.submit(function() {
    $('#loading').show();
    $.post(this.action, $(this).serialize(), null, "script");
    return false;
  })
  return this;
};

//Send data via get if <acronym title="JavaScript">JS</acronym> enabled
jQuery.fn.getWithAjax = function() {
  this.unbind('click', false);
  this.click(function() {
    $.get($(this).attr("href"), $(this).serialize(), null, "script");
    return false;
  })
  return this;
};

//Send data via Post if <acronym title="JavaScript">JS</acronym> enabled
jQuery.fn.postWithAjax = function() {
  this.unbind('click', false);
  this.click(function() {
    $.post($(this).attr("href"), $(this).serialize(), null, "script");
    return false;
  })
  return this;
};

jQuery.fn.putWithAjax = function() {
  this.unbind('click', false);
  this.click(function() {
    $.put($(this).attr("href"), $(this).serialize(), null, "script");
    return false;
  })
  return this;
};

jQuery.fn.deleteWithAjax = function() {
  this.removeAttr('onclick');
  this.unbind('click', false);
  this.click(function() {
    $.delete_($(this).attr("href"), $(this).serialize(), null, "script");
    return false;
  })
  return this;
};

//This will "ajaxify" the links
function ajaxLinks() {
    $('.ajaxForm').submitWithAjax();
    $('a.get').getWithAjax();
    $('a.post').postWithAjax();
    $('a.put').putWithAjax();
    $('a.delete').deleteWithAjax();
}

function loadScreen(obj,path) {
	$('#tab-2 ul li a').removeClass('active');
	$(obj).addClass("active");
	$('#player').fadeOut(300, function() {
		$('#player').html('<img src="/img/' + path + '" width="630" height="361" />');
	});
	$('#player').fadeIn();
	return false;
}

function loadPlayer(obj,vidPath) {
	$('#tab-1 ul li').removeClass('active');
	$(obj).parent().addClass("active");
	$('#player').fadeOut(300, function() {	
		var so = new SWFObject('/swf/player.swf','mpl','630','361','9');
		so.addParam('allowscriptaccess','always');
		so.addParam('allowfullscreen','true');
		so.addParam('wmode', 'opaque');
		so.addParam('flashvars','&file='+ vidPath +'&skin=swf/stylish_slim.swf&controlbar=over&autostart=true');
		so.write('player');
	});
	$('#player').fadeIn();
	return false;

}

function goto() {
	if (!document.getElementById('url')) return;
		var si = document.getElementById('url').selectedIndex;
		location.href = document.getElementById('url').options[si].value;
}

function notify(flash_message) {
  var flash_div = $("#flash");
  flash_div.html(flash_message);
  flash_div.fadeIn(400);
  setTimeout(function() {
    flash_div.fadeOut(500, function() {
      flash_div.html("");
      flash_div.hide()
    })
  }, 1400);
}

function clearForm(form) {
  $(':input', form).each(function() {
    var type = this.type;
    var tag = this.tagName.toLowerCase();
    if (type == 'text' || type == 'password' || tag == 'textarea') {
      this.value = "";
    } else if (type == 'checkbox' || type == 'radio') {
      this.checked = false;
    } else if (tag == 'select') {
      this.selectedIndex = -1;
    };
  });
}

$(function() {
  
  $('#wtbContent').css('display','none');
  $('#wheretobuy').hoverIntent(
    function() {
      $('#wtbContent').slideDown();
    },
    function() {
      $('#wtbContent').slideUp();
    }
  );
  
	$("#tabs").tabs();
	//$("#tabs").tabs({ fx: { opacity: 'toggle' } });
	
	$('.side li ul').css('display','none');
	$('.side li.active ul').css('display','block');
	
	$('ul.side li:not(.active)').hoverIntent(
		function() { $('ul', this).slideDown(); },
		function() { $('ul', this).slideUp(); }
	);
	
	$('#nav .dropdown').css('display','none');
	$('#nav li').hoverIntent(
		function() { $('ul.dropdown', this).slideDown(300); },
		function() { $('ul.dropdown', this).slideUp(); }
	);
	
	$('a#nav-register').parent().hoverIntent(
		function() { $('#dd-register', this).slideDown(300); },
		function() { $('#dd-register', this).slideUp(); }
	);
	
	$.localScroll();
	
	$("a[rel^='prettyPhoto']").prettyPhoto({
		animationSpeed: 'normal', /* fast/slow/normal */
		padding: 40, /* padding for each side of the picture */
		opacity: 0.35, /* Value betwee 0 and 1 */
		showTitle: true, /* true/false */
		allowresize: true, /* true/false */
		counter_separator_label: '/', /* The separator for the gallery counter 1 "of" 2 */
		theme: 'dark_rounded', /* light_rounded / dark_rounded / light_square / dark_square */
		callback: function(){}
	});
	
	Cufon.replace('h2')('.inner h3')('.num')('.new_user label');
	
	// All non-GET requests will add the authenticity token
  // if not already present in the data packet
  $(document).ajaxSend(function(event, request, settings) {
     if (typeof(window.AUTH_TOKEN) == "undefined") return;
     // <acronym title="Internet Explorer 6">IE6</acronym> fix for http://dev.jquery.com/ticket/3155
     if (settings.type == 'GET' || settings.type == 'get') return;

     settings.data = settings.data || "";
     settings.data += (settings.data ? "&" : "") + "authenticity_token=" + encodeURIComponent(window.AUTH_TOKEN);
  });

  ajaxLinks();
	
});