/* SCROLL TO FUNCTION
-------------------------------------------------------------------------------- */
$.fn.scrollTo = function(offset,speed) {
    return this.each(function() {
	if ($(this).offset()) {
	    if (offset==null) {
		offset=0;
	    }
	    if (speed==null) {
		speed = 2000;
	    }
	    var targetOffset = $(this).offset().top + offset;
	    $('html,body').animate({
		scrollTop: targetOffset
	    }, speed);
	}
    });
}

/* CLASS TOGGLE
-------------------------------------------------------------------------------- */
$.fn.hoverClass = function(className) {
    return this.each(function() {
	$(this).hover(
	    function() {
		$(this).addClass(className);
	    },
	    function() {
		$(this).removeClass(className);
	    });
    });
}
$.fn.hoverSwapClass = function(class1,class2) {
    return this.each(function() {
	$(this).hover(
	    function() {
		$(this).removeClass(class1); $(this).addClass(class2);
	    },
	    function() {
		$(this).removeClass(class2); $(this).addClass(class1);
	    }
	    );
    });
}


/* INPUT TEXT CHANGE
-------------------------------------------------------------------------------- */
$.fn.inputFocus = function(text) {
    return this.each(function() {
	$(this).val(text).focus(function() {
	    if($(this).val()==text) {
		$(this).val('');
	    }
	}).blur(function() {
	    if($(this).val()=='') {
		$(this).val(text);
	    }
	});
    });
}



/* RELOAD IMAGES
-------------------------------------------------------------------------------- */
$.preloadImages = function() {
    var a = (typeof arguments[0] == 'object')? arguments[0] : arguments;
    for(var i = a.length -1; i > 0; i--) {
	jQuery('<img/>').attr('src', a[i]);
    }
}
/* Loading
-------------------------------------------------------------------------------- */
function loading(size) {
    if ($('div.loading').offset()) { }
    else {
	$('#container').append('<div class="loading"><img src="/images/loading_160x50.gif"/></div>');
    }
    $('div.loading').lightbox();
}

$.fn.lightbox = function() {
    return this.each(function() {
	var pw = $(document).width();
	var ph = $(document).height();
	var ww = $(window).width();
	var wh = $(window).height();
	var w = $(this).width();
	var h = $(this).height();

	if ($('div.lightbox').offset()) { }
	else {
	    $('body').append('<div class="lightbox"></div>');
	}
	$('div.lightbox').width(pw);
	$('div.lightbox').height(ph);
	$('div.lightbox').show();

	$(this).css('width',w+'px');
	$(this).css('top',(wh-h)/2+'px');
	$(this).css('left',(ww-w)/2+'px');
	$(this).css('z-index','1000');
	$(this).css('position','absolute');
	$(this).show();
    });
}

$.fn.unlightbox = function() {
    return this.each(function() {
	$(this).hide();
	$('div.lightbox').hide();
    });
}

/* Console
-------------------------------------------------------------------------------- */
function addConsole() {
    $('#header').after('<div id="console"></div>');
    $('#console').load('/console',function() {
	$(this).slideDown('slow');
    });
}
function showConsole() {
    if ($('#console').html()!=null) {
	$('#console').slideDown('slow');
    } else {
	addConsole();
    }
}
function hideConsole() {
    $('#console').slideUp('slow');
}
function toggleConsole() {
    if($('#console').css('display')=='block') {
	hideConsole();
	return;
    }
    showConsole();
}

/* TOGGLE WYSIWYG Editor
-------------------------------------------------------------------------------- */
function addWYSIWYGEditor(id) {
    tinyMCE.execCommand('mceAddControl', true, id);
}
function removeWYSIWYGEditor(id) {
    tinyMCE.execCommand('mceRemoveControl', true, id);
}
function toggleWYSIWYGEditor(id) {
    tinyMCE.execCommand('mceToggleEditor', true, id);
}


/* SELECT CHANGE
-------------------------------------------------------------------------------- */
function updateSelect(json,target,default_option) {
    if (default_option == null) {
	default_option = '--- Select One ---';
    }
    if (json == null) {
	$(target).attr('disabled',true);
	var options = '<option value="">'+default_option+'</option>';
    } else {
	$(target).attr('disabled',false);
	var options = '<option value="">'+default_option+'</option>';
	for(var i=0; i<json.length; i++) {
	    options += '<option value="' + json[i].value + '">' + json[i].display + '</option>';
	}
    }
    $(target).html(options);
}

/* MENU
-------------------------------------------------------------------------------- */
function toggleMenu(menu,index) {
    $(menu + ' #menu_'+index+' #indicator').toggleClass('hide');
    $(menu + ' #menu_'+index+' #menu_body').slideToggle("slow");
}

/* TABS
-------------------------------------------------------------------------------- */
function activateTab(tab,tab_body) {
    $(tab+' ul > li:first-child').addClass('active');

    $(tab+' ul li').not('li.active').hover(
	function() {
	    $(this).addClass('hover');
	},
	function() {
	    $(this).removeClass('hover');
	}
	);

    $(tab_body+' > *').hide();
    $(tab_body+' div:first-child').show();
}

function showTab(tab,index,parent,select) {
    $(tab+' ul > li').removeClass('active').removeClass('hover').hover(
	function() {
	    $(this).addClass('hover');
	},
	function() {
	    $(this).removeClass('hover');
	}
	).end();
    $(tab+' ul > li:nth-child('+index+')').addClass('active').unbind().end();

    $(parent+' > *').hide();
    $(select).slideDown();
}

/* SLIDES
-------------------------------------------------------------------------------- */
var slideTotal = 0;
var slideIndex = 0;

function activateSlider(slider) {
    slideTotal = $(slider + ' > div.slide').size();
    slideIndex = 0;

    $(slider + ' .slider_bar li').hover(
	function() {
	    $(this).addClass('active');
	},
	function() {
	    $(this).removeClass('active');
	}
	);
    showSlide(slider,slideIndex);
}
function prevSlide(slider) {
    if (slideIndex-1<0) {
	slideIndex = slideTotal-1;
    } else {
	slideIndex--;
    }
    showSlide(slider,slideIndex);
}
function nextSlide(slider) {
    if (slideIndex+1>=slideTotal) {
	slideIndex = 0;
    } else {
	slideIndex++;
    }
    showSlide(slider,slideIndex);
}
function showSlide(slider,index) {
    $(slider + '> div.slide').hide();
    slideIndex = index;
    $(slider + ' .slider_bar li').filter('.active').removeClass('active').hover(
	function() {
	    $(this).addClass('active');
	},
	function() {
	    $(this).removeClass('active');
	}
	).end();
    $(slider + ' .slider_bar li:eq('+(slideIndex+1)+')').addClass('active').unbind().end();
    $(slider + ' > div.slide:eq('+(slideIndex)+')').show();
}

/* READY
-------------------------------------------------------------------------------- */
$(document).ready(function() {
    $('#search_field').inputFocus('Search Dyalogues');

});