$(document).ready(function(){

/*
	Contact Form Styling
*/

$('#change-form,#change-form-2,#change-form-3').jqTransform();
$('#change-form-3 .jqTransformInputWrapper,#change-form-3 .jqTransformTextarea').css("border","1px solid #959595");
$('#change-form-3 textarea#comments').css("width","600px");

/*
	Contact Panel Slider
*/

$.fn.clearForm = function() {
  $('form#change-form-2').reset();
};

$("#flip").click(function(e){
	e.preventDefault();
    $("#panel").slideToggle("3000");
});

$("#change-form input.cancel").click(function(e){
	e.preventDefault();
	$("form#change-form")[0].reset();
    $("#panel").slideToggle("3000");
});

$(".trigger").click(function(e){
	e.preventDefault();
	$('html, body').animate({ scrollTop: 0 }, '3000');
    $("#panel").slideToggle("3000");
});

$(document).bind({
    keydown:function(e) {
        if (e.keyCode == 27 ) {
            $("#panel").slideUp("3000");
			$("form#change-form-2")[0].reset();
			$('#fade , .popup_block').fadeOut(function() {
				$('#fade').remove();  //fade them both out
			});
        }
    }, click: function(e) {
        $("#panel").slideUp("3000");
    }
});
$('#flip, #panel').bind('click', function(e){e.stopPropagation();});


/*
	Refer a Friend Modal Window
*/

$('a.poplight[href^=#]').click(function() {
    var popID = $(this).attr('rel'); //Get Popup Name
    var popURL = $(this).attr('href'); //Get Popup href to define size

    //Pull Query & Variables from href URL
    var query= popURL.split('?');
    var dim= query[1].split('&');
    var popWidth = dim[0].split('=')[1]; //Gets the first query string value

    //Fade in the Popup and add close button
    $('#' + popID).fadeIn().css({ 'width': Number( popWidth ) });

    //Define margin for center alignment (vertical   horizontal) - we add 80px to the height/width to accomodate for the padding  and border width defined in the css
    var popMargTop = ($('#' + popID).height() + 80) / 2;
    var popMargLeft = ($('#' + popID).width() + 80) / 2;

    //Apply Margin to Popup
    $('#' + popID).css({
        'margin-top' : -popMargTop,
        'margin-left' : -popMargLeft
    });

    //Fade in Background
    $('body').append('<div id="fade"></div>'); //Add the fade layer to bottom of the body tag.
    $('#fade').css({'filter' : 'alpha(opacity=80)'}).fadeIn(); //Fade in the fade layer - .css({'filter' : 'alpha(opacity=80)'}) is used to fix the IE Bug on fading transparencies 

    return false;
});

//Close Popups and Fade Layer
$('input.close, #fade').live('click', function() { //When clicking on the close or fade layer...
	$("form#change-form-2")[0].reset();
	$('#fade , .popup_block').fadeOut(function() {
        $('#fade').remove();  //fade them both out
    });
    return false;
});

/*
	Remove bottom border from last sidebar nav item
*/

$("ul.sidenav li:last-child").addClass("last");

/*
	News page sidebar tabs
*/

$(".tab-content").hide(); //Hide all content
	$("ul.news-tabs li:first").addClass("active").show(); //Activate first tab
	$(".tab-content:first").show(); //Show first tab content

	//On Click Event
	$("ul.news-tabs li").click(function() {

		$("ul.news-tabs li").removeClass("active"); //Remove any "active" class
		$(this).addClass("active"); //Add "active" class to selected tab
		$(".tab-content").hide(); //Hide all tab content

		var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
		$(activeTab).fadeIn(); //Fade in the active ID content
		return false;
	});

});

