/**
 * ########################################################
 *	Brianza in Rete
 *	Copyright 2010 Web Art Studio
 * ########################################################
 */
$(document).ready(function() {
	
	/**
	 * Banners
	 */
	$('div.banner a,div.ads_img a').each(function(i, a) {
		
		// handle target
		var a = $(a);										  
		var selfHref = ( a.attr('href').indexOf('brianzainrete.it') != -1 ) ? true : false;
		if( selfHref ) a.removeAttr('target');
		
		var img = a.find('img');
		
		// handle fade
		img.hover(
			function() {
				$(this).fadeTo(300, 0.9);	
			},
			function() {
				$(this).fadeTo(100, 1);	
			}
		);
		
	});
	
	/**
	 * Hoverings
	 */
	hovering('div.events div.event', 'span.event-title a');
	hovering('div#randitems div.item', 'span.item_name a');
	
	/**
	 * Drop Down Menù
	 */
	var sublist = $('ul.sublist');
	
	sublist.each(function(i, ul) {
		
		var ul = $(ul);
		var li = ul.parent();
		
		li.hover(
			function() {
				ul.css('display', 'block');
			},
			function() {
				ul.css('display', 'none');
			}
		);
		
	});
	
	/**
	 * Sidebar Services
	 */
	$('div#sidebar_service').click(function(event) {
		var href = $(this).find('div.sidebar_service_txt a').attr('href');
		window.location = href;
	});
	
	$('div#sidebar_service').cycle({
		fx: 'fade',
		timeout: 6000
	});
	
	/**
	 * Random cycling banner top, banner footer
	 */
	$('div.banner_top').cycle({
		fx: 'fade',
		timeout: 10000,
		prev: 'a#banner_top_nav_prev',
		next: 'a#banner_top_nav_next'
	});
	
	$('div.banner_footer').cycle({
		fx: 'fade',
		timeout: 10000
	});
	
	/**
	 * Ads
	 */
	$('div.ads_sky').each(function(i, el) {
		var self = $(el);
		var link = self.find('div.ads_img a');
		var adid = self.find('span.ads_id').html();
		
		if( link.length ) { 
			self.css('cursor', 'pointer');
			
			self.click(function(event) {
				event.preventDefault();
				window.open(link.attr('href'));
				update_adscounter(adid);
			});
			/*link.click(function(event) {
				event.preventDefault();
				window.open($(this).attr('href'));
				update_adscounter(adid);
			});
			
			self.click(function(event) {
				if( $(event.target) == link ) return;
				link.triggerHandler('click');					
			});*/
		}
	});
	
	/**
	 * Ads Top e Footer
	 */
	$('div.banner_top,div.banner_footer').each(function(i, el) {
		var self = $(el);
		var link = self.find('a');
		link.click(function(event) {
			//event.preventDefault();
			var adid = self.find('span.ads_id').html();
			update_adscounter(adid);
		});
	});
	
	/**
	 * Header Bar
	 */
	$('form#header_search_form').submit(function(event) {
		var key = $('input#header_search_q').val();
		if( key == '' ) { 
			event.preventDefault();
			return false;
		}
	});
	
	$( "a#event_search_date" ).click(function(event) {
		event.preventDefault();
		var p = $('div#pickevent');
		
		p.datepicker($.extend(datepickerOpts, {
			dateFormat: 'yy-mm-dd',
			onSelect: function(dateText, inst) {
				window.location = '/eventi/cerca?filter=by_date&city=no_value&prov=no_value&type=no_value&order_by=desc&per_page=20&date=' + dateText;
			}
		}));
	});
	
	$('div.ui-datepicker-inline').live('mouseleave', function() {
		$('div#pickevent').datepicker('destroy');												 
	});
	
	/**
	 * Sidebar height
	 */
	var wh = parseInt( $('div#wrapper').css('height') );
	var sh = parseInt( $('div#sidebar').css('height') );
	
	if( wh > sh )
		$('div#sidebar').css('height', wh + 'px');
	
});

/**
 * Hovering eventi
 */
 function hovering(elements, links) {
	var prevcolors = [];
	var urls = [];
	
	$(elements).each(function(i, el) {
		
		$this = $(el);
		urls[i] = $this.find(links).attr('href');
		
		if(!prevcolors[i]) prevcolors[i] = $this.css('backgroundColor'); 
		
		$(el)
		.css('cursor', 'pointer')
		.hover(
			function() {
				$(this).animate(
					{'backgroundColor': '#E9E9E9'},
					{duration: 800, queue: false}
				);
				window.status = urls[i];
			},
			function() {
				$(this).animate(
					{'backgroundColor': prevcolors[i]},
					{duration: 800, queue: false}
				);	
			}
		).click(function() {
			//$url = ( $url.indexOf('http://') == -1 ) ? 'http://' + $url : $url;
			window.location = urls[i];
		});
	
	});
	
	/**
	 * Newsletter
	 */
	 var newsletter_text = 'Inserisci il tuo indirizzo e-mail';
	 $('input#i_newsletter')
	 	.attr('value', newsletter_text)
		.focus(function() {
			$(this).attr('value', '');
		})
		.blur(function() {
			if( $(this).attr('value') == '' )
				$(this).attr('value', newsletter_text);
		});
}

/**
 * Update Ads Counter
 */
function update_adscounter(adid) {

	$.post(
		'/ajax/ads_counter',
		{'adid': adid},
		function(data) {
			var txt = ( data == 'true' ) ? 'Operazione completata' : 'Errore';
		}
	);
	
}

/**
 * checkAnchor (ajax pagination)
 */
var currentAnchor = null;  
  
function checkAnchor(){  
    //Check if it has changes  
    if(currentAnchor != document.location.hash){  
        currentAnchor = document.location.hash;
        //if there is not anchor, the loads the default section  
        if( !currentAnchor ) {
            section = 1;  
		}
        else {  
            //Creates the  string callback. This converts the url URL/#main&id=2 in URL/?section=main&id=2  
            var splits = currentAnchor.split('#'); 
            //Get the section  
            var section = splits[1];
			
            delete splits[0]; 
        }  
        
		$('ul#pagination_tp li a.pagelink_' + section).click();
    }  
}
