jQuery.noConflict();

var s = {

    // get results through ajax
    gr: function(loc) {

	// make sure loc is correct
	if(loc != 'top') loc = 'bottom';

	// set extra parameters
	var p = new Array();
	p[p.length] = 'action=gr';
	p[p.length] = 'aid=' + jQuery('.filter-'+loc+' input[name="aid"]').val();
	p[p.length] = 'pid=' + jQuery('.filter-'+loc+' input[name="pid"]').val();
	p[p.length] = 'cp=' + jQuery('.filter-'+loc+' input[name="cp"]').val();
	p[p.length] = 'sf=' + jQuery('.filter-'+loc+' select[name="sf"]').val();
	p[p.length] = 'so=' + jQuery('.filter-'+loc+' select[name="so"]').val();
	p[p.length] = 'pr=' + jQuery('.filter-'+loc+' select[name="pr"]').val();
	p[p.length] = 'q=' + encodeURIComponent(jQuery('.filter-'+loc+' input[name="q"]').val());

	// ajax
	jQuery.ajax({
		type: 'GET',
		url: '/assets/inc/bin/services.php',
		data: p.join('&'),
		success: function(html) { jQuery('div#links-wrapper').html(html); },
		error: function(XMLHttpRequest, textStatus, errorThrown) {}
	});

    },

    // set page order
    sp: function(loc, dir) {

	// make sure loc is correct
	if(loc != 'top') loc = 'bottom';

	// make sure dir is correct
	if((dir != 'back') && (dir != 'jump')) dir = 'forward';

	// set new page number
	if(dir == 'jump') {
	    jQuery('.filter-'+loc+' input[name="cp"]').attr('value', jQuery('.filter-'+loc+' select[name="p"]').val());
	} else {
	    jQuery('.filter-'+loc+' input[name="cp"]').attr('value', ((dir == 'back') ? parseInt(jQuery('.filter-'+loc+' input[name="cp"]').val()) - 1 : parseInt(jQuery('.filter-'+loc+' input[name="cp"]').val()) + 1));
	}

	// get results
	s.gr(loc);

    },

    // did we hit enter in our search field?
    en: function(e, loc) {

	// make sure loc is correct
	if(loc != 'top') loc = 'bottom';

	// if we hit enter, get results
	if(e.which == 13) s.gr(loc);

    },

    // search bar
    // do we have to hide or show our default text (or maybe empty it?)
    s: function(q, s) {

	if(q == 'focus') {

	    if(jQuery.trim(jQuery('#top-search input[name="q"]').val()) == s) jQuery('#top-search input[name="q"]').attr('value', '');
	    if(jQuery.trim(jQuery('#bottom input[name="q"]').val()) == s) jQuery('#bottom input[name="q"]').attr('value', '');

	} else {

	    if(jQuery.trim(jQuery('#top-search input[name="q"]').val()) == '') jQuery('#top-search input[name="q"]').attr('value', s);
	    if(jQuery.trim(jQuery('#bottom input[name="q"]').val()) == '') jQuery('#bottom input[name="q"]').attr('value', s);

	}

    }

}

// when document is loaded, do some stuff
jQuery(document).ready(function() {

    // in header - bind hover of top menus so it will display submenus
    jQuery('#top-submenu li.sub').hover(
	function() { jQuery('div', this).show(); },
	function() { jQuery('div', this).hide(); }
    );

    // make height of titles in .subpages .promotions equal
    // bugfix: settimeout, else sometimes it won't run
    setTimeout("jQuery('.col3 .promotions .title').css('height', jQuery('.col2 .subpages .title').css('height'))", 10);

    // set default keyword in searchbar
    jQuery('#top-search input[name="q"]').bind('focus', function() { s.s('focus', jQuery('input[name="search_default_keyword"]').val()); });
    jQuery('#top-search input[name="q"]').bind('blur', function() { s.s('blur', jQuery('input[name="search_default_keyword"]').val()); });
    jQuery('#bottom input[name="q"]').bind('focus', function() { s.s('focus', jQuery('input[name="search_default_keyword"]').val()); });
    jQuery('#bottom input[name="q"]').bind('blur', function() { s.s('blur', jQuery('input[name="search_default_keyword"]').val()); });

    // bind event to scroll event (for our go-to-top button)
    jQuery(window).scroll(function() {

	// define some vars
	var height = 19;
	var padding = 10;
	var top = 275;

	if(jQuery(window).height() >= height) {

	    var offset = jQuery('#go-to-top').offset();

	    if(jQuery(document).height() - padding < jQuery(window).scrollTop() + height) {

		jQuery('#go-to-top').attr('style','position:absolute;top:'+(jQuery(document).height() - height - padding)+'px;');

	    } else if(jQuery(window).scrollTop() + padding > top) {

		jQuery('#go-to-top').attr('style','position:fixed;top:'+padding+'px;');

	    } else {

		jQuery('#go-to-top').attr('style','position:absolute;margin-left:-34px;');

	    }

	}

    });

});
