function UW_Slideshow ( object ) {

	this.object = object;
	this.list = null;
	this.list_el = null;
	this.list_name = null;
	this.list_nav = null;
	this.item_current = null;
	this.index_current = 0;
	this.list_length = 0;	
	this.width_window = 0;
	this.interval = null;
	this.slideshow_interval = 8000;

	this.init = function( el_list, el_nav ) {
		
		this.list_name = el_list;
		this.list_el = $( this.list_name );
		this.list = this.list_el.children('.banner');
		this.list_nav = $( el_nav ).children('a');
		this.item_current = this.list.eq( this.index_current );
		
		this.list_length = this.list.length;
		
		this.clone_item();
		this.set_window();
		this.start_slideshow();
		
	}
	
	this.clone_item = function() {
		
		this.item_current.clone().appendTo( this.list_name );
		
	}
	
	this.set_window = function() {
		
		this.width_window = $( window ).width();
		$('.banner').css('width',this.width_window);
		
	}
	
	this.go_next = function() {
		
		var new_index = this.index_current + 1;
		
		if ( new_index >= this.list_length ) {
			this.go_wrap( new_index );
			
		} else {
			this.go_to( new_index );
		}
		
	}
	
	this.go_to = function( index, stop_slideshow ) {
		
		if ( stop_slideshow==null ) stop_slideshow=false;
		
		var new_left = index * this.width_window * -1 ;
		var text_el = this.item_current.find('.banner_text').eq(0);
		var list_el = this.list_el;
		var self = this;
		var text_el_left = text_el.css('left');
		
		this.index_current = index;
		this.item_current = this.list.eq( this.index_current );
		
		// animate text
		text_el.animate({
			left: -200
		}, 600, function() {			
			// animate text back
			text_el.animate({
				left: text_el_left
			}, 2000);			
		});		
		
		// animate slide			
		list_el.animate({
			left: new_left
		}, 2000, function() {
			self.update_nav();
			self.update_highlight();
		} );

		if ( stop_slideshow ) this.stop_slideshow();
		
	}
	
	this.go_wrap = function( index ) {
		
		var new_left = index * this.width_window * -1 ;	
		var list_el = this.list_el;
		var text_el = this.item_current.find('.banner_text').eq(0);
		var self = this;
		var text_el_left = text_el.css('left');
		
		this.index_current = 0;
		this.item_current = this.list.eq( this.index_current );
		
		// animate text
		text_el.animate({
			left: -200
		}, 600, function() {			
			// animate text back
			text_el.animate({
				left: text_el_left
			}, 2000);			
		});
		
		// animate slide
		list_el.animate({
			left: new_left
		}, 2000, function() {
			list_el.css('left',0);
			self.update_nav();
			self.update_highlight();
		});		
		
	}
	
	this.update_nav = function() {
		
		this.list_nav.removeClass('selected');
		this.list_nav.eq( this.index_current ).addClass('selected');
		
	}
	
	this.update_highlight = function() {
		
		if ( this.index_current < 3 ) {
			$('.sub_banner').removeClass('sub_banner_active') ;
			$('#sub_banner_'+this.index_current).addClass( 'sub_banner_active' );
		} else {
			$('.sub_banner').removeClass('sub_banner_active') ;
			this.stop_slideshow();
			$(".sub_banner_popout_list").slideDown("slow","linear");
		}
		
	}
	
	this.start_slideshow = function( ) {
		
		this.interval = setInterval( this.object+".go_next()", this.slideshow_interval );
		
	}
	
	this.stop_slideshow = function() {
		
		clearInterval( this.interval );
		
	}
	
}

function UW_Portfolio() {

	this.show = function( el ) {
		
		var target_offset = $("#"+el).offset();
		var target_top = target_offset.top;

		//goto that anchor by setting the body scroll top to anchor top
		$('html, body').animate({scrollTop:target_top}, 500);
		
	}
	
}

function UW_Util() {
	
	this.showTab = function( list_class, section_class, section_index ) {
		
		var list_items = $( list_class );
		var section_items = $( section_class );
		
		list_items.removeClass('selected');
		list_items.eq(section_index).addClass('selected');
		
		section_items.css('display','none');
		section_items.eq(section_index).css('display','block');
		
	}
	
	this.scrollFromHash = function( hash ) {
		
		hash = hash.replace('#','');
		var section_index = parseInt( hash );
		
		if ( isNaN( section_index ) ) return;
		
		this.showTab( '.content_tab_list li','.content_tab_section',section_index );
		uwp.show('hash_section');
		
	}
	
	this.toggleMore = function( link_el, content_el ) {
		
		var link_obj = $('#'+link_el);
		var content_obj = $('#'+content_el);
		
		if ( content_obj.css('display') == 'none' ) {
			
			content_obj.slideDown();
			link_obj.html('See less');
			
		} else {
			
			content_obj.slideUp();
			link_obj.html('See more');
			
		}
		
	}
	
}

function UW_Request() {
	
	this.dropItem = function ( el_drag, el_drop ) {
		
		// retrieve draggable text
		var new_text = el_drag.text();
		var dragged_parent = el_drag.closest('.request_container');
		
		// hide draggable
		el_drag.css("display","none");
		
		// clone input row
		var clone = $('#droppable_clone')
			.clone()
			.prependTo('#clone_wrapper')
			.attr('id','')
			.css("display","block")
				.find('.droppable_completed')
					.text( new_text )
					.draggable({ 
						revert: "invalid"
					})
					.dblclick(function() {		
						var el_drop = dragged_parent;
						var el_drag = $(this);
						uwr.undoItem( el_drag, el_drop );			
					});
		
		// hide droppable		
		$( el_drop ).css("display","none");
		
		// slide in droppable			
		$( el_drop ).slideDown('slow');
		
	}
	
	this.undoItem = function( el_drag, el_drop ) {
		
		// retrieve draggable text
		var new_text = el_drag.text();
		
		// hide draggable
		el_drag.closest('.droppable_wrapper').css("display","none");
		
		// create dragged element
		var dragged = $('<div>', {  
			'class': 'draggable_box',   
			text: new_text
		}).appendTo( el_drop );		
		// make draggable
		dragged.draggable({ 
			revert: "invalid"
		});
		// add double click
		dragged.dblclick(function() {		
			var el_drop = '#droppable_box';
			var el_drag = $(this);
			uwr.dropItem( el_drag, el_drop );			
		});
		
	}
	
}

jQuery(document).ready(function() {
	
	/* footer pullup effect */
	if ( $('#pullup_toggle_link') ) {
		$('#pullup_toggle_link').click(function() {		
			var wrapper = $('#footer_pullup_wrapper');
			var height = parseInt( wrapper.css('height') );
			var new_height = 90;
			if ( height < 325 ) new_height = 325;		
			wrapper.animate({
				height: new_height
			}, 1000 );
		});
	}
	
	/* home sub-banner popout effect */
	if ( $('#popout_toggle_link') ) {
		$('#popout_toggle_link').click(function() {
			$(".sub_banner_popout_list").slideToggle("slow","linear");
		});
	}
	
	/* navigation effect */
	if ( $('#header_tabs') ) {
		$('.header_tab').mouseenter(function() {
			$(this).children('.header_tab_overlay').slideDown("fast","linear");
		});
		$('.header_tab').mouseleave(function() {
			$(this).children('.header_tab_overlay').slideUp("fast","linear");
		});
	}
	
	/* init slideshow */
	if ( $('#banner_container') ) {
		uws.init('#banner_container','#banner_nav');
	}
	
	/* anchor text */
	$(".scroll").click(function(event){
		//prevent the default action for the click event
		event.preventDefault();

		//get the full url - like mysitecom/index.htm#home
		var full_url = this.href;

		//split the url by # and get the anchor target name - home in mysitecom/index.htm#home
		var parts = full_url.split("#");
		var trgt = parts[1];

		//get the top offset of the target anchor
		var target_offset = $("#"+trgt).offset();
		var target_top = target_offset.top;

		//goto that anchor by setting the body scroll top to anchor top
		$('html, body').animate({scrollTop:target_top}, 500);
	});
	
	// check for hashtag
	if ( window.location.hash ) {	
		uwu.scrollFromHash( window.location.hash );		
	}
	
	// header login forms
	$('.header_link_login').click(function( event ){
		if ( event.target.nodeName != 'INPUT' )
			$(this).children('.header_form_wrapper').slideDown();
	});
	$('.header_link_cancel').click(function( event ){
		$(this).closest('.header_form_wrapper').slideUp();
	});
	
	// retrieve tweets
	$.jTwitter('USAWebsolutions', 3, function(posts){		
		$.each( posts, function(i, post){
            $('#twitter_list').append(
                '<li>'
                +' <a href="http://twitter.com/USAWebsolutions" target="_blank">USAWeb</a>: '
                +    post.text
                +'</li>'
            );
        });
	});

});

/* update slideshow when window is resized */
$(window).resize(function() {
	uws.set_window();
});

/* detect ipad, load 960 stylesheet */
var isiPad = navigator.userAgent.match(/iPad/i) != null ;
if ( isiPad ) {
	var link = $("<link>");
	link.attr({
		type: 'text/css',
		rel: 'stylesheet',
		href: 'include/styles-960.css'
	});
	$("head").append( link ); 
}

/* init slideshow */
var uws = new UW_Slideshow( 'uws' );
var uwp = new UW_Portfolio();
var uwu = new UW_Util();
var uwr = new UW_Request();
