jQuery.fn.found = function() {
  if(this.length > 0) return true;
  else return false;
};

jQuery.fn.hasAttr = function(attr){
    if(this.length > 1) return false;
    if(this.attr(attr) == undefined) return false;
    return true;
}

jQuery.fn.tabify = function() {
    // containers
    var container = $(this);
    tabs_container = container.children('ul');
    // actual tabs
    var tabs = $(tabs_container.children('li'))
    var tabs_first = $(tabs_container.children('li')[0])
    // content divs
    var content = container.children('div');    
    var content_for_first_tab = $(content[0])
    
    tabs_first.addClass("selected");
    content_for_first_tab.show();
                 
    tabs_container.click(function(evt){
        var receiver = $(evt.target);
        var target_id;
        if(receiver.hasAttr("href")) {
            target_id = receiver.attr("href").split("#").pop();
            target_tab = container.children('#'+target_id);
            
            container.children('div').hide();
            
            tabs.removeClass("selected");
            receiver.parent().addClass("selected");
            target_tab.show();
        }            
        evt.preventDefault();
    });
}

jQuery.fn.tipify = function(img_dir) {
    this.each(function(){
        var item = $(this);
        var title = item.find(".title").html();
        var desc = item.find(".desc").html();
        var screenshot = item.find(".screenshot").html();
        var link = item.find("a");
       
        if(title != null && screenshot != null) {
            /* Build tip */
            var tip = $('<div class="tip"></div>');
            desc = (desc == null) ? '<div class="tip-text"><img src="'+img_dir+screenshot+'" alt="'+title+'"></div>' : '<div class="tip-text"><p>'+desc+'</p> <img src="'+img_dir+screenshot+'" alt="'+title+'"></div>';
            title = '<div class="tip-title">'+title+'</div>';
            tip.append(title).append(desc);            
            $('body').append(tip);
            
            /* Wire up mouseover/mouseout */
            link.mouseover(function(evt){
                tip.css('top',evt.pageY+10).css('left',evt.pageX+10);
                tip.show();
            });
            link.mousemove(function(evt){
                tip.css('top',evt.pageY+10).css('left',evt.pageX+10);
            });
            link.mouseout(function(evt){
                tip.hide();
            });
        }
    });
}


/*
    If current element is set to a key in comparison_key, 
    update associated elements with the appropriate values.
*/
jQuery.fn.switchify = function(options) {
    var elem = this[0]; // only operate on the first form element found
    var current_selected = elem.options[elem.selectedIndex].value;
    // compare current selected option with options ...
    for(var key in options) {
        // if current_selected matches an option ...
        if(current_selected == key) {
            // try to locate and update related elements for option
            var option = options[key];
            for(var elem_id in option) {
                // Doesn't work in FF 3.5 - $('#'+elem_id).attr(option[elem_id].attr, option[elem_id].value);
                var change_elem = $('#'+elem_id);
                if(change_elem.found()) {
                    var attr = option[elem_id].attr;
                    try {
                        change_elem[0][attr] = option[elem_id].value;                        
                    } catch(err) {}
                    
                }
            }
        }
    }
}


/*

29 function clearBox(){
30 document.getElementById('some_name').value = '';
31 document.getElementById('some_name').style.background = '#ffffff';
32 }
33 function addLogo(value){
34 if(!value) {document.getElementById('some_name').style.background = '#ffffff url(/images/google_custom_search_watermark.gif) no-repeat 0 0';}
35 }*/