function externalLinks() {
	if (!document.getElementsByTagName){
        return;
    }
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++) {
		var anchor = anchors[i];
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external"){
            anchor.target = "_blank";
        }
	}
}
function navigate(theUrl){
    document.location.href = theUrl;
}

function addToField(field, code, sdefault)
{
	if($('#'+field).val() == sdefault){
		$('#'+field).val('');
	}
	$('#'+field).val($('#'+field).val()+code+ ' ');
}

function showImage(object, source){
	$('#'+object).attr("src", source);
}

function confirmDelete(){
	if(confirm('Are you sure that you want to delete this item?')){
		return true;
	}
	return false;
}

function onEnterSubmit(e, form){
    var keycode;
	if(e){
		e = e 
	} 
	else {
		e = window.event
	} 
	if(e.which){ 
		keycode = e.which
	} 
	else {
		keycode = e.keyCode 
	}
	if(keycode == 13) {
		form.submit();
	}
}

function open_url(page,target, reformurl){
	var firstpageload=false;
	$(document).ready(function() {
		if(firstpageload){
			setup_urlcontent(page, target,reformurl);
		}
		else{
			$('#'+target).fadeOut('fast', function(){setup_urlcontent(page, target,reformurl);});
		}
	});
}

function load_content(page, target, reformurl){
	$("#content").css('cursor', 'wait');
	
	$.ajax({
		type: "GET",
		url: page,
		cache: false,
		dataType: 'json',
		success: function(html){
			// New content
	    	$("#"+target).html(html.content);
	    	// External links
	    	externalLinks();
	    	fleXenv.fleXcrollMain("contentscroll");
	    	$("#content").css('cursor', 'default');
	  	},
	  	error: function(){
	  		$("#content").css('cursor', 'default');
	  		if(page == 'error/inline'){
	  			alert("You request ended up in an endless loop\n Please try again later");
	  		}
	  		else{
	  			open_url('error/inline', 'content');
	  		}
	  	}
	});
}

function setup_urlcontent(page, target, reformurl){	
	$.ajax({
		type: "GET",
		url: page,
		cache: false,
		dataType: 'json',
		success: function(html){
			// New content
	    	$("#"+target).html(html.content);
	    	// Page Url
	    	if(reformurl !== false){
	    		setPageUrl(page);
	    	}
	    	// External links
	    	externalLinks();
	    	$('#'+target).fadeIn('fast', function(){
	    		fleXenv.fleXcrollMain("contentscroll");
	    	});
	  	},
	  	error: function(){
	  		if(page == 'error/inline'){
	  			alert("You request ended up in an endless loop\n Please try again later");
	  		}
	  		else{
	  			open_url('error/inline', 'content');
	  		}
	  	}
	});
}

var firstPage = true;
function focusContent() {
	if (!firstPage) {
		$('html, body').animate({
			scrollTop: ($("#content").offset().top - 100)
		}, 200);
	}
	
	firstPage = false;
}

function setPageUrl(strPage){
	window.location.hash = strPage;
}

function MadFileBrowser(field_name, url, type, win) {
  tinyMCE.activeEditor.windowManager.open({
      file : "/plugins/mfm/mfm.php?field=" + field_name + "&url=" + url + "",
      title : 'File Manager',
      width : 640,
      height : 450,
      resizable : "no",
      inline : "yes",
      close_previous : "no"
  }, {
      window : win,
      input : field_name
  });
  return false;
}
function setShortkeyRow(sShortkey){
    sShortkey = (typeof(sShortkey) === 'undefined' ? '' : sShortkey);

    var html = '<div class="rowholder">';
    html += '<div class="row stripe">';
    html += '<div class="colom_20">Shortkey #:</div>';
    html += '<div class="colom_50"><input type="text" name="shortkey[]" size="40" value="'+sShortkey+'" /></div>';
    html += '<div class="colom_30">';
    html += '<img rel="addshortkey" onclick="setShortkeyRow();" src="/cms/images/icons/add.png" style="cursor:pointer;display:none;" /> ';
    html += '<img rel="deleteshortkey" onclick="removeShortkeyRow(this);" src="/cms/images/icons/delete.png" style="cursor:pointer;display:none;" /> ';
    html += '</div>';
    html += '</div>';
    html += '</div>';

    $(html).hide().appendTo($('#shortkeylist')).slideDown('fast', function(){
        initShortkeyIcons();
    });
}
function removeShortkeyRow(obj){
    var row = $(obj).parent().parent();
    $(row).slideUp('fast',function(){
        $(row).remove();
        initShortkeyIcons();
    });
}
function initShortkeyIcons(){
    // Remove all add buttons instead of last one
    $('#shortkeylist img[rel=addshortkey]').hide();
    $('#shortkeylist img[rel=addshortkey]').last().show();
    // Add delete buttons except the first one
    if($('#shortkeylist img[rel=deleteshortkey]').length > 1){
        $('#shortkeylist img[rel=deleteshortkey]').show();
    }
    else{
        $('#shortkeylist img[rel=deleteshortkey]').hide();
    }
}
function setDiscographyRow(sTrack, sArtist, sTitle, sUrl){
    sTrack = (typeof(sTrack) === 'undefined' ? '' : sTrack);
    sArtist = (typeof(sArtist) === 'undefined' ? '' : sArtist);
    sTitle = (typeof(sTitle) === 'undefined' ? '' : sTitle);
    sUrl = (typeof(sUrl) === 'undefined' ? '' : sUrl);

    var html = '<div class="rowholder">';
    html += '<div class="row">';
    html += '<div class="colom_20">Track #:</div>';
    html += '<div class="colom_10"><input type="text" name="prefix[]" size="4" value="'+sTrack+'" /></div>';
    html += '<div class="colom_30"><input type="text" name="trackartist[]" style="width:160px;" value="'+sArtist+'" /></div>';
    html += '<div class="colom_30"><input type="text" name="tracktitle[]" style="width:160px;" value="'+sTitle+'" /></div>';
    html += '<div class="colom_10">';
    html += '<img rel="adddiscography" onclick="setDiscographyRow();" src="/cms/images/icons/add.png" style="cursor:pointer;display:none;" /> ';
    html += '<img rel="deletediscography" onclick="removeDiscographyRow(this);" src="/cms/images/icons/delete.png" style="cursor:pointer;display:none;" /> ';
    html += '</div>';
    html += '</div>';
    html += '<div class="row stripe">';
    html += '<div class="colom_20">Url #:</div>';
    html += '<div class="colom_10">&nbsp;</div>';
    html += '<div class="colom_60"><input type="text" name="trackurl[]" style="width:343px;" value="'+sUrl+'" /></div>';
    html += '<div class="colom_10">&nbsp;</div>';
    html += '</div>';
    html += '</div>';

    $(html).hide().appendTo($('#discographytracklist')).slideDown('fast', function(){
        initDiscographyIcons();
    });
}
function removeDiscographyRow(obj){
    var row = $(obj).parent().parent().parent();
    $(row).slideUp('fast',function(){
        $(row).remove();
        initDiscographyIcons();
    });
}
function initDiscographyIcons(){
    // Remove all add buttons instead of last one
    $('#discographytracklist img[rel=adddiscography]').hide();
    $('#discographytracklist img[rel=adddiscography]').last().show();
    // Add delete buttons except the first one
    if($('#discographytracklist img[rel=deletediscography]').length > 1){
        $('#discographytracklist img[rel=deletediscography]').show();
    }
    else{
        $('#discographytracklist img[rel=deletediscography]').hide();
    }
}
function refreshCaptcha() {
	$('#captcha').attr('src', 'captcha?sid='+ (898989218921 * Math.random()*Math.random()));
}
// Init
var firstpageload = true;
// Setup links
window.onload = externalLinks;

$(document).ready(function() {
	$('.discography-thumb').stop().animate({ opacity: 0.38 }, 200);
	$('.navigation').stop().animate({ opacity: 0.3 }, 200);

	$('.discography-thumb').each(function() {
		$(this).hover(function() {
			$(this).stop().animate({ opacity: 1.0 }, 200);
		},
	   function() {
		   $(this).stop().animate({ opacity: 0.38 }, 200);
	   });
	});
	$('.navigation').each(function() {
		$(this).hover(function() {
			$(this).stop().animate({ opacity: 1.0 }, 200);
		},
	   function() {
		   $(this).stop().animate({ opacity: 0.3 }, 200);
	   });
	});
});

