ksc = {};
ksc.analytics_id = "UA-325355-1";

ksc.init_analytics = function(){
  if (typeof(_gat) != 'undefined'){
    var pageTracker = _gat._getTracker(ksc.analytics_id);
    pageTracker._initData();
    pageTracker._trackPageview();
  	var outgoing_links = $("a[href ^= 'http']");
  	$.each(outgoing_links, function(){
  		var add_outgoing_style = true;    		
  		var _this = $(this);
  		var ahref = _this.attr('href');  		
			if (ahref.indexOf('keene.edu') >= 0 || $('img',_this).length) {add_outgoing_style = false;}  		
  		if (add_outgoing_style == true){$(this).addClass('external')}
  		$(this).click( function() { 
  			var ahref = $(this).attr('href');
  			if (ahref.indexOf('keene.edu') == -1){				
  				var cleaned_href = ahref.replace(/https?:\/\//,'');
  				var analytics_path = '/outgoing/'+cleaned_href;
  	     		pageTracker._trackPageview(analytics_path);
  	     	}
  	    });
  	}); 
  }
}

ksc.init_search = function(){
    
    var sf = $('#main_search_form');
    
    if (sf.length == 1){        
        
        var query_input = $('input.search_text',sf);
        query_input.focus();
        
        // jQuery UI autocomplete if loaded            
        if (typeof(query_input.autocomplete) != 'undefined'){
            query_input.autocomplete({ 
                source: function( request, response ) {
                    var search_type = $('input.search_radio:checked',sf).val();
                    $.ajax({
                        url: "/directories/search/ajax/autocomplete/",
                        dataType: "jsonp",
                        data: {
                            featureClass: "P",
                            style: "full",
                            maxRows: 5,
                            search_type: search_type, 
                            query: request.term
                        },
                        success: function( data ) {
                            response( $.map( data.matches, function( item ) {
                                return {
                                    label: item.name + (item.code ? ' | '+item.code : ''),
                                    value: item.name,
                                    url: item.url
                                }
                            }));
                        }
                    });
                },
                minLength: 3,
                select: function( event, ui ) {
                    // http://stackoverflow.com/questions/5366068/jquery-ui-autocomplete-submit-onclick-result
                    var search_form = $('form.search_form');
                    if(ui.item.url){
                        window.location = ui.item.url;
                    }else if(ui.item){
                        var search_input = $('input[name=query]',search_form);
                        search_input.attr('value', ui.item.value);
                        search_form.submit();
                    }
                    
                },
                open: function() {
                    $( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" );
                },
                close: function() {
                    $( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );
                }
            
            });
        }
        
        $('input.search_radio',sf).change(function(){
            var $this = $(this);
            var query_value = $('input.search_text',sf).val();
            if (query_value){
                sf.submit();
            }else{
                $('div.search_radio_note',sf).addClass('hide');
                $('#'+$this.attr('id').replace('radio','note'),sf).removeClass('hide');
                $(this).removeClass('hide');
                // removed when web support was added
                //query_input.autocomplete( "option", "disabled", $this.val() == 'web' );                
            }
        });   
    }
}


ksc.rotate = function(rotator, timer){
    
    var rotatees = $('div.rotatee', rotator);
    var outgoing_rotatee = rotatees.filter(':visible').first();
    var incoming_rotatee = outgoing_rotatee.next();
    
    if(incoming_rotatee.length == 0){
        incoming_rotatee = rotatees.first();
    }
    
    var incoming_blurb = $('.blurb',incoming_rotatee);
    var outgoing_blurb = $('.blurb',outgoing_rotatee);    
    var incoming_image = $('div.image',incoming_rotatee);
    var outgoing_image = $('div.image',outgoing_rotatee);
    
    incoming_rotatee.css('z-index',2);
    outgoing_rotatee.css('z-index',1);    
    incoming_blurb.css('opacity',0);
    outgoing_blurb.css('opacity',.9);
    incoming_image.css('opacity',0);
    outgoing_image.css('opacity',1);
    
    rotatees.hide();
    //rotatees.removeClass('rounder_4');
    incoming_rotatee.show();//.addClass('rounder_4');
    outgoing_rotatee.show();//.addClass('rounder_4');
    
    outgoing_blurb.fadeTo(1000, 0);
    incoming_blurb.fadeTo(1000, .9);
    
    outgoing_image.delay(500).fadeTo(1500,0, function(){$(this).parent().hide()});
    incoming_image.delay(500).fadeTo(1500,1);
    
    var next_animation = function(){ksc.rotate(rotator, timer)}    
    setTimeout(next_animation, timer );
    
}

ksc.init_rotator = function(){
    
    var rotators = $('div.widget_rotator');
    $.each(rotators, function(){
        var _this = $(this);
  		var rotatees = $('div.rotatee',_this);
  		if(rotatees.length == 0) return;
  		rotatees.hide();
  		$('div.blurb',rotatees).css('opacity',.9);
  		rotatees.first().show();
  		setTimeout(function(){ksc.rotate(_this,12000); }, 5000 );
    });
    
}


$(document).ready(function() {	
	ksc.init_search();
	ksc.init_rotator();
	ksc.init_analytics();
	if ('init_map' in ksc) ksc.init_map();	
});	
