/* Author: Joey Yax

*/

function findoutmoreform() {
	$.fancybox({
		'href': '/wp-content/themes/kryptiq2011/form-salesforce.php',
		'transitionIn'	:	'elastic',
		'transitionOut'	:	'elastic',
		'speedIn'	:	600, 
		'speedOut'	:	200, 
		'overlayShow'	:	true,
		'scrolling'	:	false
	});
}

$(function() {
	
	$( 'a.popup, a[rel*="attachment"]' ).fancybox({
		'transitionIn'	:	'elastic',
		'transitionOut'	:	'elastic',
		'speedIn'	:	600, 
		'speedOut'	:	200, 
		'overlayShow'	:	true,
		'scrolling'	:	false
	});
	
	// remove branding from google search field
	$( '#s' ).css('background-image', '').removeAttr( 'style' );
	$( '#s' ).focus( function() {
		//console.log( 'focus' );
		$( this ).removeAttr( 'style' );
	})
	$( '#s' ).blur( function() {
		//console.log( 'blur' );
		$( this ).css( 'background-image', '' );
	});
	
	// text resizer
	if($.cookie('text_size')) {
		$('body').addClass($.cookie('text_size')); 
	}
	else {
		$('body').addClass($.cookie('text-small')); 
	}
	
	$('.textsizer a').click(function() {
		var text_size = 'text-' + $(this).attr('class');
		$('body').removeClass('text-small text-medium text-large').addClass(text_size);
		$.cookie('text_size',text_size, { path: '/', expires: 10000 });
		
		if ( $( '.solution_products' ).length > 0 ) adjust_height();
		return false;
	});
	
	// solutions page
	if ( $( '.solution_products' ).length > 0 ) { // check if we're on a solutions page
		
		var products = $( '.solution_products .solution_products_list li a' ); // products list
		var detail_container = $( '.solution_products .solution_product_detail' ); // product detail container
		var solution_overview = $( '.solution_overview' );
		var solution_products = $( '.solution_products .solution_product_detail' );
		var solutions_count = products.length; // count how many products we have
		
		if ( solutions_count > 0 ) { // if we have at least one product, continue...
			 
			//$( detail_container ).load( $( products ).eq( 0 ).attr( 'href' ) + ' #hentry' );
			
			for( var i = 0; i < solutions_count; i++) {
				var href = $( products ).eq( i ).attr( 'href' );
				var href_arr = href.split( '/' );
				var post_name = href_arr[ href_arr.length-2 ];
				//console.log( href + ": " + post_name ); 
				$( products ).eq( i ).attr( 'postname', post_name );
			}
			
				
			if ( window.location.hash != '' ) {
				var post_name = window.location.hash.split( '/' );
				var post_name = post_name[ post_name.length-1 ];
				get_tab( $( "a[postname='" + post_name + "']" ) );
			}
			else {
				get_tab( $( products ).eq( 0 ) )
			}
			
			products.unbind('click').bind( 'click', function() {
				
				window.location.hash = '!/' + $( this ).attr( 'postname' );
				
				//alert('click');
				get_tab( $( this ) );
				
				return false;
				
			});
			
			
		}
		
	}
	
	function get_tab( tab ) {
				
		adjust_height();
		
		var href = $( tab ).attr( 'href' ) + '?get=intro';
		
		if ( $(tab).hasClass('active') ) {
			// ignore (added because ie was repeatedly calling the function)
		}
		else {
			
			$( '.solution_products_list li a' ).removeClass( 'active' );
			$( tab ).addClass( 'active' );
			
			detail_container.html( '' ); // clear contents
			
			$.ajax({
				url: href,
				type: 'get',
				cache: false,
				beforeSend: function() {
					
					detail_container.html( '<span class="loading"><img src="/wp-content/themes/kryptiq2011/img/loading-circle.gif" alt="" /><br />Loading&hellip;</span>' );
					
				},
				success: function( data ) {
					var tab_title, tab_content;
					
					detail_container.html( '' ); // clear contents
					
					//tab_title = 'test';
					//tab_content = 'test';
						
					detail_container.html( data );
					
					// console.log( 'title: ' + title );
					// console.log( 'content: ' + content );
				
					adjust_height(); 
				}
			})
		}
		
			
	}
	
	function adjust_height() {
	
		$( solution_products ).height('');
		
		var detail_height = $( solution_overview ).height();
		var products_height = $( solution_products ).height();
		
		// console.log( 'detail height: ' + detail_height );
		// console.log( 'products height: ' + products_height );
		
		$( solution_products ).css( 'min-height', parseInt(detail_height) );
		
		if ( parseInt(detail_height+50) > products_height ) {
			$( solution_products ).height( parseInt(detail_height) );
			// console.log( 'updated products height to: ' + parseInt( detail_height) );
		}
	}
});






















