// default start
var Void=function(){};if(!window.console){window.console={log:Void}};

var QueryString = { 
    Init: function() {
        QueryString.__PARSED = {};
        $.each( window.location.search.substring(1).split("&"), function() {
            QueryString.__PARSED[ this.split("=")[0] ] = this.split("=")[1];
        });
    },
    Get: function( index ){ 
        if( ! QueryString.__PARSED ) {
            QueryString.Init();
        }
        
        return QueryString.__PARSED[ index ];
    } 
}; 

var Message = [
	{
		Title: "Thank you for sharing your interests with People Capital!",
		Body: "After we've reviewed your submission, we will send you an email with more details on how People Capital can help you."  
	}
];    

var Site = {
    Init: function() {
		Site.__MONTH_YEAR_PICKERS = $( $('input.monthYear').monthYear() );
        Site.ContactForm.Init();
        Site.Overlay.CheckQueryForMessage();
    },
    Overlay: {
        CloseWindow: function() {
            $( Site.Overlay.__ELEMENT ).fadeOut( 250 );
        },
        CheckQueryForMessage: function() {
            switch( QueryString.Get("msg") ) {
                case "1": 
                    Site.Overlay.ShowMessage(Message[0].Title, Message[0].Body);
                    break;
                default:
					break;
            }
        },
        ShowMessage: function( title, message ) {
            if( ! Site.Overlay.__ELEMENT ) {
                Site.Overlay.Init();
            }
            
            Site.Overlay.__ELEMENT.show();
            Site.Overlay.__MAIN_CONTENT_ELE.empty();
            Site.Overlay.__MAIN_CONTENT_ELE.append(
                Site.Overlay.GetCloseButton(),
                $( '<h4></h4>' ).text(title),
                $( '<p></p>' ).html(message)
            );
        },
        GetCloseButton: function() {
            return $('<a></a>')
                .attr('class', 'closeButton')
                .click( Site.Overlay.CloseWindow )
				.text('Close')
				.voidLink();
        },
        Init: function() { 
            Site.Overlay.__ELEMENT = $('<div></div>)')
				.hide()
				.attr('class', 'messageBoxContainer')
				.append(
					$('<div></div>')
						.attr('class', 'messageBox')
						.append(
							$('<table></table>')
								.attr('cellpadding','0')
								.attr('cellspacing','0')
								.append(
									$('<tbody></tbody>').append(
										$('<tr></tr>').append(
											$('<td> </td>').attr('class','shadowTopLeft').html('&nbsp;'),
											$('<td> </td>').attr('class','shadowTopCenter').html('&nbsp;'),
											$('<td> </td>').attr('class','shadowTopRight').html('&nbsp;')
										),
										
										$('<tr></tr>').append(
											$('<td> </td>').attr('class','shadowMiddleLeft').html('&nbsp;'),
											$('<td> </td>').attr('class','mainOverlayContent').append(
												Site.Overlay.__MAIN_CONTENT_ELE = $('<div></div>').attr('class','clearfix')	
											),
											$('<td> </td>').attr('class','shadowMiddleRight')
										),
										
										$('<tr></tr>').append(
											$('<td> </td>').attr('class','shadowBottomLeft').html('&nbsp;'),
											$('<td> </td>').attr('class','shadowBottomCenter').html('&nbsp;'),
											$('<td> </td>').attr('class','shadowBottomRight').html('&nbsp;')
										)
									)
								)
						)
				);

            $(document.body).append( Site.Overlay.__ELEMENT );
        }
    },
	Tabs: {
		Init: function( ele ) {
			Site.Tabs.__ACTIVE = -1;
			var pages = ele.find('.page');
			pages.each( function() {
				if( $(this).hasClass('active') ) { Site.Tabs.__ACTIVE = this; }
				$(this).find('li a').each( function( linkIndex ) {
					$(this).voidLink().click(function() {
						if( pages[ linkIndex ] != Site.Tabs.__ACTIVE ) {
							
							$( Site.Tabs.__ACTIVE ).fadeOut( 400 );
							$( pages[ linkIndex ] ).fadeIn( 400 ).addClass('active');
							
							Site.Tabs.__ACTIVE = pages[ linkIndex ];
						}
						return false;
					});
				});
			});
		},
		Set: function( id ) {
			$(function() { 
				Site.Tabs.Init( $( "#" + id ) );
			});
		}
	},
	
	Popup: {
		Set: function( id ) {
			$(function() {				
				$("#" + id).click(function() { 
					window.open(this.href, "_blank", "height=500,width=450,toolbar=no,scrollbars=yes,resizable=no,status=no");
					return false;
				});
			});
		}
	},
	
	ContactForm: {
	    Init: function() {
	        Site.ContactForm.InterestedInSwitcher();
	    },
	    StateCollegeDropDown: function( options ) {
            var hiddenField = $("#" + options.CollegeHiddenField);
            var collegeSelectEle = $("#" + options.CollegeDropDown);
            var stateEle = $("#" + options.StateDropDown);
            
            if( hiddenField.size() > 0 && collegeSelectEle.size() > 0 && stateEle.size() > 0 ) {
                stateEle.change(function() {
                    collegeSelectEle
						.addClass('noData')
						.empty()
						.append( 
							$('<option></option>' )
								.html('Please Wait...')
								.val('-1')
						);
						
                    $.postJSON(
						'/WebServices/SchoolService.asmx/GetCollegesByStateId',
						{ StateId: stateEle.val() },
						function( jsonObj ) {
                            jsonObj = jsonObj.d;
                            
                            if( jsonObj.length > 0 ) { collegeSelectEle.removeClass('noData'); }
                            
                            collegeSelectEle.empty();
                            collegeSelectEle.append( 
                               $('<option value="-1">&nbsp;</option>' )
                            );

                            $.each( jsonObj, function( i, collegeEle ) {
                                selected = false;
                                
                                if( hiddenField.val() == collegeEle.CollegeId ) {
                                    selected = true;
                                }
                                                               
                                collegeSelectEle.append( 									
									$('<option></option>') 
										.val(collegeEle.CollegeId)
										.attr('selected', selected)
										.html(collegeEle.CollegeName)
								);
										
                            });
                        }                       
                    );
                });
                
                collegeSelectEle.change(function() {
					console.log(hiddenField);
                    hiddenField.val( collegeSelectEle.val() );
                });
                
                if( hiddenField.val() != "" ) {
                    stateEle.change();
                }
            }
	    },
	    InterestedInSwitcher: function() {
	        var ele = $('#interestedInField');
	        
	        if( ele.size() > 0 ) {
	            var eleIds = [ '#borrowerForm', '#lenderForm' ];
	            
	            var radioEles = ele.find('input');
	            radioEles.each( function( radioIndex ) {
	                $(this).click( function() {
						$( eleIds[0] ).hide();	
						$( eleIds[1] ).hide();
						
						if( radioIndex < eleIds.length ) {
							$( eleIds[radioIndex] ).show();
						}
						
						if( Site.__MONTH_YEAR_PICKERS.length > 0 ) {
							Site.__MONTH_YEAR_PICKERS.each( function() { this.repositionTrigger() } );
						}
	                });
	                										
	                if( this.checked ) { 
	                    $(this).click();
	                }
	            });
	        }
	    }
	}
};


$( Site.Init );