$(document).ready(function() {	
	setImageAsSelectBoxButton();
	
	$('.year').bind('click', function() {		
		$('[name=year]').val(this.id);
		document.showcaseFilter.submit();
	});

	$('#typeAll').bind('click', function() {
		clear_form_elements($('#typeFilter'));
		document.showcaseFilter.submit();
	});
	
    $('#btnShare').click(function() {
    	fireMyPopup('shareDiv');
        return false;
    });

    $('.closeLayer').click(function() {
    	fadeOutMyPopup();
    	if($('#vidOverlayContent')) {
        	$('#vidOverlayContent').empty();
    	}
        return false;
    });

    $('.closeSignup').click(function() {
    	fadeOutMyPopup();
        return false;
    });

    $('#fadeIn').click(function () {
    	if($('#vidOverlayContent')) {
        	$('#vidOverlayContent').empty();
    	}
    	fadeOutMyPopup();
   	});
    
    $('#signUpFadeIn').click(function () {
    	fadeOutMyPopup();
   	});
    
    $('.profileLink').click(function() {
    	showProfile(this.id);
    });

    $('.listItemDetail').mouseleave(function() {
    	$(this).hide('fast');
	});

    $('#mycarousel').jcarousel({
        wrap: 'circular',
        initCallback: mycarousel_initCallback
    });
    
    $('.listingPrint').click(function () {
    	window.print();
    	return false;
   	});
    
    $('#introAdDiv').css({top:'53%',left:'50%',margin:'-'+($('#introAdDiv').height() / 2)+'px 0 0 -'+($('#introAdDiv').width() / 2)+'px'});
    $('#shareDiv').css({top:'50%',left:'50%',margin:'-'+($('#shareDiv').height() / 2)+'px 0 0 -'+($('#shareDiv').width() / 2)+'px'});
    $('#signUpDiv').css({top:'53%',left:'50%',margin:'-'+($('#signUpDiv').height() / 2)+'px 0 0 -'+($('#signUpDiv').width() / 2)+'px'});
    $('#signUpThankYouDiv').css({top:'53%',left:'50%',margin:'-'+($('#signUpThankYouDiv').height() / 2)+'px 0 0 -'+($('#signUpThankYouDiv').width() / 2)+'px'});
    $('#signUpSorryDiv').css({top:'53%',left:'50%',margin:'-'+($('#signUpSorryDiv').height() / 2)+'px 0 0 -'+($('#signUpSorryDiv').width() / 2)+'px'});
    $('#vidOverlay').css({top:'53%',left:'50%',margin:'-'+($('#vidOverlay').height() / 2)+'px 0 0 -'+($('#vidOverlay').width() / 2)+'px'});

    $(".toggle_container_first").show();
    $(".toggle_container").hide();
    $(".listingItemLittleText").show();

	$("span.moreTextLinkContainer").click(function(){
		$(this).toggleClass("active").prev().prev().slideToggle("slow");
		$(this).html('<a class="moreTextLink" href="javascript:void(0)">Show Less</a>');
		if(!$(this).hasClass("active")){
			$(this).html('<a class="moreTextLink" href="javascript:void(0)">Show More</a>');
		}
	});
	
	$("span.moreTextLinkContainerSpecial").click(function(){
		$(this).toggleClass("active").prev().prev().slideToggle("slow");
		$(this).prev().prev().prev().slideToggle("slow");
		$(this).html('<a class="moreTextLink" href="javascript:void(0)">Show Less</a>');
		if(!$(this).hasClass("active")){
			$(this).html('<a class="moreTextLink" href="javascript:void(0)">Show More</a>');
		}
	});
});

function clear_form_elements(element) {
	$('label.error').hide();
    $(element).find(':input').each(function() {
        switch(this.type) {
            case 'password':
            case 'select-multiple':
            case 'select-one':
            case 'text':
            case 'textarea':
                $(this).val('');
                break;
            case 'checkbox':
            case 'radio':
                this.checked = false;
        }
    });

}

function mycarousel_initCallback(carousel) {

	carousel.options.scroll = 1;
	
    // Disable autoscrolling if the user clicks the prev or next button.
    carousel.buttonNext.bind('click', function() {
        carousel.startAuto(0);
    });

    carousel.buttonPrev.bind('click', function() {
        carousel.startAuto(0);
    });

    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
        carousel.stopAuto();
        carousel.startAuto();
    });
};

function showProfile(which) {
	//reset z-index of all list items
	$('.listItem').css('z-index','100');
	//hide all except clicked to prevent multiple profiles open, and allow toggling via the link
	$('.listItemDetail').each(function(index) {
		if(index != which) {
			$(this).hide();
		}
	});
	$('#profile'+which).toggle('fast');
	//move clicked item to top
	$('#listItem'+which).css('z-index','200')
}

//Browser safe opacity handling function
function setOpacity( value ) {
	if(document.getElementById("vidOverlay")){
		document.getElementById("vidOverlay").style.opacity = value / 10;
		document.getElementById("vidOverlay").style.filter = 'alpha(opacity=' + value * 10 + ')';
	}
	if(document.getElementById("signUpThankYouDiv")){
		document.getElementById("signUpThankYouDiv").style.opacity = value / 10;
		document.getElementById("signUpThankYouDiv").style.filter = 'alpha(opacity=' + value * 10 + ')';
	}
	if(document.getElementById("signUpSorryDiv")){
		document.getElementById("signUpSorryDiv").style.opacity = value / 10;
		document.getElementById("signUpSorryDiv").style.filter = 'alpha(opacity=' + value * 10 + ')';
	}
	if(document.getElementById("signUpDiv")){
		document.getElementById("signUpDiv").style.opacity = value / 10;
		document.getElementById("signUpDiv").style.filter = 'alpha(opacity=' + value * 10 + ')';
	}
	if(document.getElementById("shareDiv")){
		document.getElementById("shareDiv").style.opacity = value / 10;
		document.getElementById("shareDiv").style.filter = 'alpha(opacity=' + value * 10 + ')';
	}
	if(document.getElementById("fadeIn")){
		document.getElementById("fadeIn").style.opacity = value/15;
		document.getElementById("fadeIn").style.filter = 'alpha(opacity=' + value * 7 + ')';
	}
	if(document.getElementById("introAdDiv")){
		document.getElementById("introAdDiv").style.opacity = value / 10;
		document.getElementById("introAdDiv").style.filter = 'alpha(opacity=' + value * 10 + ')';
	}
}

function resize() {
	window_width = ''; blanket_height = '';
	var frame = document.getElementById("fadeIn"); 
	
	/* Getting width height */ 
	if (typeof window.innerWidth != 'undefined') {
		viewportheight = window.innerHeight;
	} else {
		viewportheight = document.documentElement.clientHeight;
	}
	if ((viewportheight > document.body.parentNode.scrollHeight) && (viewportheight > document.body.parentNode.clientHeight)) {
		blanket_height = viewportheight;
	} else {
		if (document.body.parentNode.clientHeight > document.body.parentNode.scrollHeight) {
			blanket_height = document.body.parentNode.clientHeight;
		} else {
			blanket_height = document.body.parentNode.scrollHeight;
		}
	}
	/* end of getting height */
	
	/* Getting width width */
	if (typeof window.innerWidth != 'undefined') {
		viewportwidth = window.innerHeight;
	} else {
		viewportwidth = document.documentElement.clientWidth;
	}
	if ((viewportwidth > document.body.parentNode.scrollWidth) && (viewportwidth > document.body.parentNode.clientWidth)) {
		window_width = viewportwidth;
	} else {
		if (document.body.parentNode.clientWidth > document.body.parentNode.scrollWidth) {
			window_width = document.body.parentNode.clientWidth;
		} else {
			window_width = document.body.parentNode.scrollWidth;
		}
	}

	/* end of getting width */
	frame.style.width = "4000px"; 
	frame.style.height = "2000px"; 
	frame.style.top = '-48px'; // Disney
	frame.style.left = '-135px';
} 

function fadeInMyPopup(which) {
	for( var i = 0 ; i <= 100 ; i++ )
		setTimeout( 'setOpacity(' + (i / 10) + ')' , 8 * i );
	document.getElementById("fadeIn").style.zIndex = 9000;
	document.getElementById("content").style.zIndex = 9001;
	document.getElementById(which).style.zIndex = 9002;
}

function fadeOutMyPopup() {
	for( var i = 0 ; i <= 100 ; i++ ) {
		setTimeout( 'setOpacity(' + (10 - i / 10) + ')' , 8 * i );
	}
	setTimeout('closeMyPopup()', 800 );
}

function closeMyPopup() {
	$('.overlay').css('display','none');
	if(document.getElementById("introAdDiv")){
		document.getElementById("introAdDiv").style.display = "none";
	}
	document.getElementById("fadeIn").style.display = "none";
	document.getElementById("content").style.zIndex = 10;
}

function fireMyPopup(which) {
	setOpacity( 0 );
	document.getElementById(which).style.display = "block";
	document.getElementById("fadeIn").style.display = "block";
	resize();
	fadeInMyPopup(which);
}

//Modify Select box button to an image
function setImageAsSelectBoxButton(){
	$("select.findState").each(function(){
			var selectId = $(this).attr("id");
			var selectBoxTxt = $("#" + selectId + " :selected").text();
			var insertHtmlContent = "<span id='select" + selectId + "' class='select'>" + selectBoxTxt + "</span>";
			
			$(insertHtmlContent).insertBefore(this);

			$(this).change(function(){
				var selectBoxTxt = $(this).find("option:selected").text();
				$("#select" + selectId).html(selectBoxTxt);
			});
	});

	$( "select.findState" ).css( {
		'opacity': '0',
		'position': 'absolute',
		'top': '0',
		'left': '0',
		'z-index': '5'
	});
}

