var bannerTimer = '';
$(document).ready(function()
{
$("#uploadfanphoto").click(showFanForm);
$("#closeFanForm").live('click', hideFanForm);
$("#uploadMore").live('click', showFanForm);
$("#uploadContestMore").live('click', showContestContainer);
$("#closeContestForm").live('click', hideContestForm);
 $('.jqContestImageUploadButton').live('click', uploadContestImage);

    $('#txtContestName').focus(function(){
        if($(this).val() == contestFormDefaultText)
            {
                $(this).val('');
            }

    }).blur(function(){

    if($(this).val() == '')
            {
                $(this).val(contestFormDefaultText);
            }

    });

    $('#txtContestEmail').focus(function(){
        if($(this).val() == contestFormDefaultEmail)
            {
                $(this).val('');
            }

    }).blur(function(){

    if($(this).val() == '')
            {
                $(this).val(contestFormDefaultEmail);
            }

    });
    $('#txtContestField').focus(function(){
        $contestTitle = $("#txtContestField").attr("title");
        if($(this).val() == $contestTitle)
            {
                $(this).val('');
            }

    }).blur(function(){

    if($(this).val() == '')
            {
                $(this).val($contestTitle);
            }

    });


    $('#txtFanName').focus(function(){
        if($(this).val() == fanFormDefaultText)
            {
                $(this).val('');
            }

    }).blur(function(){

    if($(this).val() == '')
            {
                $(this).val(fanFormDefaultText);
            }

    });

    $('#txtFanEmail').focus(function(){
        if($(this).val() == fanFormDefaultEmail)
            {
                $(this).val('');
            }

    }).blur(function(){

    if($(this).val() == '')
            {
                $(this).val(fanFormDefaultEmail);
            }

    });

    $('.jqFanImageUploadButton').live('click', uploadFanImage);
    
    $('.jgFeaturedThumb').click(function()
    {
        
        $(this).blur();
        $('.activeNow').removeClass('activeNow');
        $(this).addClass('activeNow');

        curIndex =      $('.jgFeaturedPreview').index($('.jqBannerContainer.show'));

            myIndex = $('.jgFeaturedThumb').index($(this));

            //alert(curIndex+' '+ myIndex)

        var current = ($('.jqBannerContainer.show')?  $('.jqBannerContainer.show') : $('.jqBannerContainer:first'));
	var next = $('.jgFeaturedPreview').eq($('.jgFeaturedThumb').index($(this)));

        if(curIndex != myIndex)
        {
            //Set the fade in effect for the next image, the show class has higher z-index
            next.css({opacity: 0}).show().addClass('show').animate({opacity: 1}, 1000);
            //Hide the current image
            current.animate({opacity: 0}, 1000, function(){   $(this).hide(); }).removeClass('show');
            clearInterval(bannerTimer);
            bannerTimer = setInterval(rotate, 5000);
        }
});
    theRotator() ;


    $('.jqNextToNewsletter').click(function()
    {
        var name = $('#txtName').val();
        var email = $('#txtEmail').val();
        $('#frmNewsLetter').submit();
        
    });




   





});

function theRotator() {
	//Set the opacity of all images to 0
	$('.jqBannerContainer').css({opacity: 0.0}).not(":first").hide();

	//Get the first image and display it (gets set to full opacity)
	$('.jqBannerContainer:first').css({opacity: 1.0});

	//Call the rotator function to run the slideshow, 6000 = change to next image after 6 seconds
	bannerTimer = setInterval(rotate, 5000);

}

function rotate() {
	//Get the first image
	var current = ($('.jqBannerContainer.show')?  $('.jqBannerContainer.show') : $('.jqBannerContainer:first'));

	//Get next image, when it reaches the end, rotate it back to the first image
	var next = ((current.next().length) ? ((current.next().hasClass('show')) ? $('.jqBannerContainer:first') :current.next()) : $('.jqBannerContainer:first'));

         curIndex =      $('.jgFeaturedPreview').index(next);

            myLink = $('.jgFeaturedThumb').eq(curIndex);

	$('.activeNow').removeClass('activeNow');
       myLink.addClass('activeNow');


        //Set the fade in effect for the next image, the show class has higher z-index
	next.css({opacity: 0})
        .show()
	.addClass('show')
	.animate({opacity: 1}, 1000);

	//Hide the current image
	current.animate({opacity: 0}, 1000, function(){   $(this).hide(); })
	.removeClass('show');



}

function uploadFanImage()
{

    var name = $('#txtFanName').val();
    var email = $('#txtFanEmail').val();
    var fileName = $('#fileImage').val();
    var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;

    if(name == fanFormDefaultText || name == '')
    {
        alert('Please give name');
    }
    else if(email == fanFormDefaultEmail || email == '')
    {
        alert('Please give email');
    }
    else if(reg.test(email) == false)
    {
        alert('Invalid email address');
    }
    else if(fileName == '')
    {
        alert('Please select a image');
    }
    else if(!checkFileType(fileName, 'img'))
    {
        alert('Please select a valid image file');
    }
    else
    {
        $("#frmFanImageForm").ajaxSubmit({
            dataType:'json',
            success:function(reply)
        {
            if(reply.error == 'validate')
            {
                showFailureMessage(reply.info);
            }
            else if(reply.error)
            {
                alert(reply.info);
            }
            else if(reply.success)
            {
                

                $("#dvFormContainer").hide();
                $("#dvSuccessContainer").show();
                    $('#txtFanName').val(fanFormDefaultText);
                    $('#txtFanEmail').val(fanFormDefaultEmail);
                $('#fileImage').val('');

            }
        }});
    }

return false;

    
}

//Image file type checking.
function checkFileType(fileName, fileType)
{
	check = false;
	if(fileName != "" && fileType != "")
        {
        check = true;
	fileType = fileType.toLowerCase();
	iPos1		= fileName.lastIndexOf(".");
	if(iPos1 > 0)
	{
		strExtension = fileName.substring(iPos1+1);
		strExtension = strExtension.toLowerCase();
		switch(fileType)
		{
			case "png":
				var imageTypes = new Array("png");
				if($.inArray(strExtension, imageTypes) < 0)
				{
					check = false;
				}
			break;

			case "img":
				var imageTypes = new Array("jpg", "gif", "jpeg", "png");
				if($.inArray(strExtension, imageTypes) < 0)
				{
					check = false;
				}
			break;

            case "pdf":
				var imageTypes = new Array("pdf", "PDF");
				if($.inArray(strExtension, imageTypes) < 0)
				{
					check = false;
				}
			break;
		}
	}
        }
	return check;
}

function showFanForm()
{
$("#dvSuccessContainer").hide();
$("#dvFormContainer").show();

    var connectionRequestBox = $("#fanImageContainer");
   var connectionRequestBoxHeight     = connectionRequestBox.outerHeight();
       var connectionRequestBoxWidth     = connectionRequestBox.outerWidth();
       var windowWidth             = $(window).width();
       var windowHeight             = $(window).height();
       var pageScrollTop           = document.documentElement.scrollTop ? document.documentElement.scrollTop : (document.body.scrollTop ? document.body.scrollTop : 0);


       var connectionRequestBoxTop        = pageScrollTop + (windowHeight - connectionRequestBoxHeight) / 2;
       var connectionRequestBoxLeft       = (windowWidth - connectionRequestBoxWidth) / 2;
      connectionRequestBox.css({
                   position : "absolute",
                   left : connectionRequestBoxLeft,
                   top: connectionRequestBoxTop
               }).fadeIn("slow");

}

function hideFanForm()
{
    $("#fanImageContainer").fadeOut();
    return false;
    

}

function hideContestForm()
{
    $("#contestImageContainer").fadeOut();
    return false;
    

}

function show_contest_form(contestDefaultValue)
{
    $("#dvContestSuccessContainer").hide();
    $("#dvContestFormContainer").show();
    
    var connectionRequestBox = $("#contestImageContainer");
    var connectionRequestBoxHeight     = connectionRequestBox.outerHeight();
    var connectionRequestBoxWidth     = connectionRequestBox.outerWidth();
    var windowWidth             = $(window).width();
    var windowHeight             = $(window).height();
    var pageScrollTop           = document.documentElement.scrollTop ? document.documentElement.scrollTop : (document.body.scrollTop ? document.body.scrollTop : 0);


    var connectionRequestBoxTop        = pageScrollTop + (windowHeight - connectionRequestBoxHeight) / 2;
    var connectionRequestBoxLeft       = (windowWidth - connectionRequestBoxWidth) / 2;
    if (contestDefaultValue != ''){
        $("#txtContestField").val(contestDefaultValue);
        $("#txtContestField").attr("title", contestDefaultValue);
    }
    connectionRequestBox.css({
                   position : "absolute",
                   left : connectionRequestBoxLeft,
                   top: connectionRequestBoxTop
               }).fadeIn("slow");
    return false;

}

function uploadContestImage()
{

    var name = $('#txtContestName').val();
    var email = $('#txtContestEmail').val();
    var contestField = $('#txtContestField').val();
    var fileName = $('#fileImage').val();
    var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    var contestTitle = $("#txtContestField").attr("title");

    if(name == contestFormDefaultText || name == '')
    {
        alert('Please give name');
    }
    else if(email == contestFormDefaultEmail || email == '')
    {
        alert('Please give email');
    }
    else if(reg.test(email) == false)
    {
        alert('Invalid email address');
    }
    else if(contestField == contestTitle || contestField == '')
    {
        alert('Please give contest field');
    }
    else if(fileName == '')
    {
        alert('Please select a image');
    }
    else if(!checkFileType(fileName, 'img'))
    {
        alert('Please select a valid image file');
    }
    
    else
    {
        $("#frmContestImageForm").ajaxSubmit({
            dataType:'json',
            success:function(reply)
        {
            if(reply.error == 'validate')
            {
                showFailureMessage(reply.info);
            }
            else if(reply.error)
            {
                alert(reply.info);
            }
            else if(reply.success)
            {


                $("#dvContestFormContainer").hide();
                $("#dvContestSuccessContainer").show();
                    $('#txtContestName').val(contestFormDefaultText);
                    $('#txtContestEmail').val(contestFormDefaultEmail);
                    $('#txtContestField').val(contestTitle);
                $('#fileImage').val('');

            }
        }});
    }

return false;


}

function showContestContainer()
{
    $("#dvContestSuccessContainer").hide();
    $("#dvContestFormContainer").show();

    var connectionRequestBox = $("#contestImageContainer");
    var connectionRequestBoxHeight     = connectionRequestBox.outerHeight();
    var connectionRequestBoxWidth     = connectionRequestBox.outerWidth();
    var windowWidth             = $(window).width();
    var windowHeight             = $(window).height();
    var pageScrollTop           = document.documentElement.scrollTop ? document.documentElement.scrollTop : (document.body.scrollTop ? document.body.scrollTop : 0);


    var connectionRequestBoxTop        = pageScrollTop + (windowHeight - connectionRequestBoxHeight) / 2;
    var connectionRequestBoxLeft       = (windowWidth - connectionRequestBoxWidth) / 2;
    $("#txtContestField").val($("#txtContestField").attr("title"));

    connectionRequestBox.css({
                   position : "absolute",
                   left : connectionRequestBoxLeft,
                   top: connectionRequestBoxTop
               }).fadeIn("slow");
    return false;

}

/*function show_upload_image_link()
{
    var text = '';
    if(contestFieldDefaultValue)
    {
        text = contestFieldDefaultValue;
    }
    document.write('<a href="#">' + text + '</a>');

    //overlay

}*/
