/**
 * external_links()
 * ---------------
 * This functions iteraters through links that are external (include the rel="external" tag)
 * and inserts atribute target="_blank" to make them open in new window.
 * Real reason? To keep our XHTML valid.
 *
 * @author     Miha Hribar <miha@hribar.info>
 */
function external_links() {
    $("a[@rel=external]").attr({ target: "_blank"});
    // find links inside content, that start with http and make them open in new window
    $('a[@href^="http://"]').attr({ target: "_blank"});
}

jQuery.fn.loading = function () {
    var div = jQuery(this);
    var height = $(this).height();
    div.attr({ innerHTML: '<div class="loading" style="height: '+ height +'px;"></div>' });
    return(this);
}

function menu_hover () {
    $("#nav a[class='video']").mouseout(function(){
        $("#navigation_bar").hide();
    }).mouseover(function(){
        var id_selected = $("#nav a[class='video']").attr("id");
        if (id_selected != "selected") {
            $('#navigation_bar').loading().load('default.html?ajax=2&type=videos', function() {
                 $('#navigation_bar').html($(this).html());
            });
            $("#navigation_bar").fadeIn("slow");;
        }
    });

    $("#nav a[class='images']").mouseout(function(){
        $("#navigation_bar").hide();
    }).mouseover(function(){
        var id_selected = $("#nav a[class='images']").attr("id");
        if (id_selected != "selected") {
            $('#navigation_bar').loading().load('default.html?ajax=2&type=images', function() {
                $('#navigation_bar').html($(this).html());
            });
            $("#navigation_bar").fadeIn("slow");;
        }
    });

    $("#navigation_bar").mouseout(function(){
        $("#navigation_bar").hide();
    }).mouseover(function(){
        $("#navigation_bar").show();
    });


}

function AutocompleteInit () {
	$("#q").autocomplete(
		"/en/?ajax=1&autocomplete=ha&",
		{
			delay:10,
			width:309,
			matchSubset:0,
			matchContains:0,
			cacheLength:1,
			autoFill:false
		}
	);
}

function copyIt() {
    $("#clip").click(function(){
        copy(document.getElementById('video_copy'));
        return false;
    });
}

function copy(inElement) {
    if (navigator.appName == "Microsoft Internet Explorer") {
        window.clipboardData.setData('Text',$("#video_copy").val());
    } else {
        if (inElement.createTextRange) {
            var range = inElement.createTextRange();
            if (range && BodyLoaded==1)
                range.execCommand('Copy');
        } else {
            var flashcopier = 'flashcopier';
            if(!document.getElementById(flashcopier)) {
                var divholder = document.createElement('div');
                divholder.id = flashcopier;
                document.body.appendChild(divholder);
            }
            document.getElementById(flashcopier).innerHTML = '';
            var divinfo = '<embed src="/static/swf/_clipboard.swf" FlashVars="clipboard='+escape(inElement.value)+'" width="0" height="0" type="application/x-shockwave-flash"></embed>';
            document.getElementById(flashcopier).innerHTML = divinfo;
            $("#clipcopy").attr('innerHTML', 'URL copied');
        }
    }
}

/* on document ready do the following */
$(document).ready(function(){
    AutocompleteInit();
    external_links();
    /*menu_hover();*/
    copyIt();
    $('#ex2').jqm({
        ajax: '/en/pageinfo/contact',
        trigger: 'a.ex2trigger',
        modal: 'true'
    });


});
