/**
 * @author: Jobzippers Ltd.
 **/

registerNamespace = function(space) {

    var parts = space.split('.');
    var root = window;

    for (var i = 0; i < parts.length; ++i) {

        if (typeof root[parts[i]] == 'undefined') {
        	root[parts[i]] = new Object();
        }

        root = root[parts[i]];

    }

    return root;
};


$.ajaxSetup({
    async: true/*,
    beforeSend: overlay,
    complete: overlay
*/
});



jQuery.fn.modifyLinksForStudentPreview = function(){

	$(this).tooltip({

      	track: true,
        delay: 0,
        showURL: false,
        bodyHandler: function() {

            $(this).css('cursor', 'help');

            return student_preview_not_available_text;
        }
    });
    

    $(this).click(function(){
        return false;
    });
	
};

jQuery.fn.CV_Advisor = function(width){

 $(this).attr('href', '#');
 $(this).bind('click', function(){

    var popID = $(this).attr('rel'); // popup_name
    //var popID = 'popup_name';
    var popWidth = width;

    $('#' + popID).fadeIn().css({ 'width': Number( popWidth ) }).prepend('<a href="" class="close"><img src="/media/img/popup/close_pop.png" class="btn_close" title="Close Window" alt="Close" border="0" /></a>');
    var popMargTop = ($('#' + popID).height() + 80) / 2;
    var popMargLeft = ($('#' + popID).width() + 80) / 2;
    $('#' + popID).css({
            'margin-top' : -popMargTop,
            'margin-left' : -popMargLeft,
            'height' : '400'
    });
    
    $('body').append('<div id="fade"></div>');
    $('#fade').css({'filter' : 'alpha(opacity=80)'}).fadeIn();
    return false;

 });
};

jQuery.fn.modifyLinksForPreview = function(){

 $(this).attr('href', '#');
 $(this).attr('rel', 'popup_name');
 $(this).bind('click', function(){
                            
    var popID = $(this).attr('rel'); // popup_name
    //var popID = 'popup_name';
    var popWidth = 400;

    $('#' + popID).fadeIn().css({ 'width': Number( popWidth ) }).prepend('<a href="" class="close"><img src="/media/img/popup/close_pop.png" class="btn_close" title="Close Window" alt="Close" border="0" /></a>');
    var popMargTop = ($('#' + popID).height() + 80) / 2;
    var popMargLeft = ($('#' + popID).width() + 80) / 2;
    $('#' + popID).css({
            'margin-top' : -popMargTop,
            'margin-left' : -popMargLeft
    });

   /* $('body').append('<div>Hallo,Sie m&uuml;ssen sich erst einloggen!</div>'); */

    $('body').append('<div id="fade"></div>');
    $('#fade').css({'filter' : 'alpha(opacity=80)'}).fadeIn();
    return false;

 });
 //$(this).addClass('popup_name');
 /*
 $("div.hoverError.hidden").removeClass('hidden');

 setTimeout(function(){
  $("div.hoverError").fadeOut("Slow", function(){
   $(this).addClass('hidden').attr('style', '');
  });
 }, 3000);*/
};
$(document).ready(function(){

		
                        $('a.close, #fade').live('click', function() {
				$('#fade , .popup_block').fadeOut(function() {
					$('#fade, a.close').remove();
				});
				return false;
			});
});



jQuery.fn.addUniversity = function(){


    //var popID = $(this).attr('rel'); // popup_name
    var popID = 'popup_name';
    var popWidth = 700;

    var country = $(this).parent().parent().find("select[rel='addUniversitySourceCountry']").val();

    var $what = $('#' + popID);
    $('#' + popID).fadeIn().css({ 'width': Number( popWidth ) }).prepend('<a href="" class="close"><img src="/media/img/popup/close_pop.png" class="btn_close" title="Close Window" alt="Close" border="0" /></a>');
    var popMargTop = ($('#' + popID).height() + 80) / 2;
    var popMargLeft = ($('#' + popID).width() + 80) / 2;
    $('#' + popID).css({
            'margin-top' : -popMargTop,
            'margin-left' : -popMargLeft
    });

    var $select = $what.find('select');
    $select.val(country);

    $what.jNice();

    $('body').append('<div id="fade"></div>');
    $('#fade').css({'filter' : 'alpha(opacity=80)'}).fadeIn();

    return false;

};



$(document).ready(function(){

	
	$("a.closeHoverError").click(function(){
		$(this).parent().fadeOut("fast", function(){
			$(this).addClass('hidden').attr('style', '')
		});
	});

    // ie submit-button fix
    $("input[type='submit']").focus(function(){
        this.blur;
        return false;
    });


    // iconbox fading
    $("div.iconbox a").hover(function () {

		$(this).prevAll().dequeue().fadeTo(400,0.4);

		$(this).nextAll().dequeue().fadeTo(400,0.4);

    },function() {

		$(this).prevAll().dequeue().fadeTo(400,1,function(){if ($.browser.msie) this.style.removeAttribute('filter')});

		$(this).nextAll().dequeue().fadeTo(400,1,function(){if ($.browser.msie) this.style.removeAttribute('filter')});

	});


    // bookmarkBar
    $("a.bookmark_bar").click(
        function(){
            if($("ul.bookmark_bar").hasClass("hidden"))
            {
                $("ul.bookmark_bar").fadeIn(75);
                $("ul.bookmark_bar").removeClass("hidden");
                $("a.bookmark_bar").css("background-image", "url('/media/img/site/arrow_up.gif')");
            }
            else
            {
                $("ul.bookmark_bar").fadeOut(75);
                $("ul.bookmark_bar").addClass("hidden");
                $("a.bookmark_bar").css("background-image", "url('/media/img/site/arrow_down.gif')");
            }

            return false;
        }
    );




	// remove links to home/error and add tooltip

    $("a[href='"+baseurl+"home/error.html']").tooltip({

      	track: true,
        delay: 0,
        showURL: false,
        bodyHandler: function() {

            $(this).css('cursor', 'default');

            return under_construction_text;
            /*return "Diese Seite befindet sich momentan noch im Aufbau.";*/
        }
    });


    $("a[href='"+baseurl+"home/error.html']").click(function(event){
		event.preventDefault();

        //return false;
    });
	
	
	
	
	// student preview for companies
	if(student_preview)
	{
		$("a.link_disabled_in_preview").modifyLinksForStudentPreview();
		$("a[href^='/applications']").modifyLinksForStudentPreview();
		$("a[href^='/home/promotion_form']").modifyLinksForStudentPreview();
		$("a[href^='"+baseurl+"jobs/apply']").modifyLinksForStudentPreview();	
	}
	



    $("img.kununu_rating_bar").tooltip({

       track: true,
        delay: 0,
        showURL: false,
        bodyHandler: function() {

            return rating_description_text;

        }
    });


    $("form[name='cvgen'] .jNiceCheckbox").click(function(){
        JZ.form.toggleDateToSelectVisibility($(this));
    });
	
		

	$('.email').mailto();

});

Cufon.replace('h2.imghl, h2.pi, div.home_col h3, h2.borderhl, h2.pi_large, h2.teaserhl, h3.subteaserhl, #LongSlogan');

var space = registerNamespace('JZ');

space.general = {

    switchLoginBox: function(country)
    {
        $("#countryLogin .specificLogin").addClass("hidden");
        $("#countryLogin .specificLogin#"+country).removeClass("hidden");
        $("select[name='country']").val(country);
        $.jNice.SelectUpdate("select[name='country']");
    },

    animateCompanyBoxes: function(delay, fadingSpeed){

        visible_items = $(".TopCompanies ul").find("li.active");
        invisible_items = $(".TopCompanies ul").find("li.hidden");

        var random_visible = Math.floor(Math.random()*visible_items.length);
        var random_invisible = Math.floor(Math.random()*invisible_items.length);

        // item to replace: visible_items[random_active_items]
        var $el_old = $(visible_items[random_visible]);
        var $el_new = $(invisible_items[random_invisible]);


        $old_data = Array();
        $old_data['index'] = $el_old.attr('id').substr(4);
        $old_data['company_title'] = $el_old.find('img').attr('alt');
        $old_data['href'] = $el_old.find('a').attr('href');
        $old_data['img_src'] = $el_old.find('img').attr('src');
        $old_data['img'] = $el_old.find('img');

        $new_data = Array();
        $new_data['index'] = $el_new.attr('id').substr(4);
        $new_data['company_title'] = $el_new.find('img').attr('alt');
        $new_data['href'] = $el_new.find('a').attr('href');
        $new_data['img_src'] = $el_new.find('img').attr('src');
        $new_data['img'] = $el_new.find('img');

        // now swap both items
        $el_old.find('img').fadeOut(fadingSpeed, function(){
            $clone = $new_data['img'].clone();
            $clone.addClass('hidden');
            $(this).after($clone);
            $clone.fadeIn(fadingSpeed);
            $(this).parent().parent('a').attr('href', $new_data['href']);
            $(this).parent().parent().parent('li').attr('id', 'item' + $new_data['index']);
            $(this).remove();

            $el_new.attr('id', 'item' + $old_data['index']);
            $el_new.find('img').attr('alt', $old_data['company_title']);
            $el_new.find('a').attr('href', $old_data['href']);
            $el_new.find('img').attr('src', $old_data['img_src']);

            window.setTimeout(function(){JZ.general.animateCompanyBoxes(delay, fadingSpeed);}, delay);
        });

    }

}