var popUpWindow;

function openPopUpWindow(link)
{
    popUpWindow = window.open(link, 'popUpWindow', 'menubar=0,toolbar=0,status=0,height=500,width=500,resizable=1');
    popUpWindow.focus();
}

function closePopUpWindow(link)
{
    window.opener.focus();
    window.close();
}

// ----------------------
function initImageRollovers(){
    $(':img.btn').each(function(i){
        $(this).mouseover(function(){
            var oldImgSrc = $(this).attr('src');
            var newImgSrc = oldImgSrc.replace('.gif', '-over.gif');
            newImgSrc = newImgSrc;
            $(this).attr('src', newImgSrc);
            $.preloadImages.add(newImgSrc);
        });
        $(this).mouseout(function(){
            var oldImgSrc = $(this).attr('src');
            var newImgSrc = oldImgSrc.replace('-over.gif', '.gif');
            newImgSrc = newImgSrc.replace('-over.jpg', '.jpg');
            $(this).attr('src', newImgSrc);
        });
    });
    $.preloadImages.start();
}

function initLightBox(){
    if ($("a.lightbox").length < 1) 
        return;
    $("a.lightbox").lightBox({
        imageLoading: '/images/lightbox-ico-loading.gif',
        imageBtnPrev: '/images/lightbox-btn-prev.gif',
        imageBtnNext: '/images/lightbox-btn-next.gif',
        imageBtnClose: '/images/lightbox-btn-close.gif',
        imageBlank: '/images/lightbox-blank.gif'
    });
}

var c = 0;

function addTextBoxControl(panel, name)
{
    controlName = name + ++c;
    $(panel).append("<div><input class=\"ifc-input-text\" name=\"" + controlName + "\" type=\"text\"/></div>");
    setPHeight()
}

function addTwoTextBoxControl(panel, first, second, firstTitle, secondTitle)
{
    firstControlName = first + ++c;
    secondControlName = second + c;
    var html = "";
    html += "<div><input class=\"ifc-name-input-text\" name=\"" + firstControlName + "\" type=\"text\"";
    if (firstTitle != "")
    {
        html += " title=\"" + firstTitle + "\"";
    }
    html += "/> <input class=\"ifc-name-input-text\" name=\"" + secondControlName + "\" type=\"text\"";
    if (secondTitle != "")
    {
        html += " title=\"" + secondTitle + "\"";
    }
    html += "/></div>";
    $(panel).append(html);
    $(".ifc-name-input-text").labelify();
    setPHeight();
}
function addOneTextBoxControl(panel, first, firstTitle)
{
    firstControlName = first + ++c;
    //secondControlName = second + c;
    var html = "";
    html += "<div><input class=\"ifc-credits-input-text autocompl\" name=\"" + firstControlName + "\" type=\"text\" ";
    if (firstTitle != "")
    {
        html += " title=\"" + firstTitle + "\"";
    }
    html += "/></div>";
     
    $(panel).append(html);
    $(".ifc-credits-input-text").labelify();
    $('.autocompl').autocomplete({ serviceUrl:'/autocomplete/moviepeople.aspx', width:300 });
    setPHeight();
}
function addOneTextBoxControlWithRole(panel, first, firstTitle, roleField)
{
    firstControlName = first + ++c;
    //secondControlName = second + c;
    thirdControlName = roleField + c;
    var html = "";
    html += "<div><input class=\"ifc-credits-input-text autocompl\" name=\"" + firstControlName + "\" type=\"text\" ";
    if (firstTitle != "")
    {
        html += " title=\"" + firstTitle + "\"";
    }
    html += "/><input class=\"ifc-name-input-text\" name=\"" + thirdControlName + "\" type=\"text\" title=\"role\" /></div>";
     
    $(panel).append(html);
    $(".ifc-credits-input-text").labelify();
    $(".ifc-name-input-text").labelify();
    $('.autocompl').autocomplete({ serviceUrl:'/autocomplete/moviepeople.aspx', width:300 });
    setPHeight();
}

function is_other(obj)
{
	if(obj.value=="ENTER ADDITIONAL ROLE HERE")
	{
		var val = prompt("ADDITIONAL ROLE:");
		
		if(val==null)
		{
			alert("You chose not to enter a role. Default value (cinematographer) will be set.");
			obj.value = "cinematographer";	
		}
		else if(val.length>0)
		{
			val = val.toLowerCase();

			var arr = [];

			for( var i=0;i<obj.options.length;i++)
			{
				arr[i] = obj.options[i].value;
			}
			
			if($.inArray(val, arr) < 0)
			{
				obj.options[obj.options.length-1] = new Option(val,val);
				obj.options[obj.options.length] = new Option("ENTER ADDITIONAL ROLE HERE");
			}

			obj.value = val;
		}
		else
		{
			alert("You chose not to enter a role. Default value (cinematographer) will be set.");
			obj.value = "cinematographer";	
		}
		
	}
}

var current_dropdown;
	
function show_other(obj)
{
	if(obj.value=="ENTER ADDITIONAL ROLE HERE")
	{
		current_dropdown = obj;
		$( '#dialog-form' ).dialog( 'open' ).css('height','50px');
		var p = $(current_dropdown);
		var position = p.position();
		$('.ui-dialog-titlebar').css('display','none');
		$('.ui-dialog').css('top',position.top);
	}
}

function addCrewmemberControl(panel, first, third, firstTitle)
{
    firstControlName = first + ++c;
    //secondControlName = second + c;
    thirdControlName = third + c;
    var html = "";
    html += "<div><input class=\"ifc-credits-input-text autocompl\" name=\"" + firstControlName + "\" type=\"text\"" ;
    if (firstTitle != "")
    {
        html += " title=\"" + firstTitle + "\"";
    }
    /*html += "/> <input class=\"ifc-name-input-text\" name=\"" + secondControlName + "\" type=\"text\"";
    if (secondTitle != "")
    {
        html += " title=\"" + secondTitle + "\"";
    }*/
    html += "/><select onchange=\"show_other(this)\" name=\"" + thirdControlName + "\" class=\"ifc-credits-select\"><option>cinematographer<option>editor<option>writer<option>original music<option>art director<option>ENTER ADDITIONAL ROLE HERE</select></div>";
    
    $(panel).append(html);
    $(".ifc-credits-input-text").labelify();
    $('.autocompl').autocomplete({ serviceUrl:'/autocomplete/moviepeople.aspx', width:300 });
    setPHeight();
}


function addFileUploadControl(panel, name)
{
    controlName = name + ++c;
    $(panel).append("<div><input class=\"ifc-input-text\" name=\"" + controlName + "\" type=\"file\"/></div>");
    setPHeight();
}

function submitComment(id, nameId, commentId)
{
    name = $("#" + nameId).val();
    comment = $("#" + commentId).val();
    $.get("/ajax/comment/?id=" + id + "&name=" + name + "&comment=" + comment, {}, function(data, status)
    {
        $("#ctl00_Content_UserCommentsItem_FormDiv").hide();
        $("#ctl00_Content_UserCommentsItem_MessageDiv").html("Your comment will appear once it has been approved.");
        $("#ctl00_Content_UserCommentsItem_MessageDiv").show();
    });
    return false;
}

$(document).ready(function()
{
	
	$("#FilmForm_CrewmembersRepeater_ctl00_crewmembersRoleField").attr("onclick","is_other(this)");
	//$("#ctl00_Content_SubmitFestivalFormItem_TypesInput_InputField").attr("onclick","is_other_festival(this)");

	//$("#FilmForm_CrewmembersRepeater_ctl00_crewmembersRoleField").css("display","none");

	//addCrewmemberControl('#FilmForm_CrewmembersPanel', 'crewmembersFullNameField', 'crewmembersRoleField', 'firstname lastname');
	
	$("#base_crewer").css("display","none");
		
    if ($("#overlay-iframe").size() == 0)
    {
        // iframe autoheight
        setHeight(parent.$("#overlay-iframe").get(0));
        
        //parent.$("#overlay-iframe").css('top', ($(window).scrollTop()+300) + "px");
        
        //alert(parent.$("#overlay-iframe").get(0));
    }
//
//    $("#overlayCloseButton").click(function() {
//        $("#ctl00_FilmForm_OverlayPanelVisible").val("");
//        $('.overlay').hide();
//        return false;
//    });
//    
//    $("#ctl00_FilmForm_Cancel").click(function() {
//        $("#ctl00_FilmForm_OverlayPanelVisible").val("");
//        $('.overlay').hide();
//        return false;
//    });

    $("#FilmForm_Submit").click(function(){
    })
    
    $("#ctl00_PromoColumn_AddNewFilmmakerItem_SubmitButton").click(function() {
        var name = $("#ctl00_PromoColumn_AddNewFilmmakerItem_FilmmakerTitle").val();
        var path = '/israeli-film-database/filmmaker-form/?name=' + name
        return showOverlay(path);
    });

    /*$("#ctl00_Content_MovieDetailItem_AddFilm").click(function() {
        var path = '/israeli-film-database/film-form/';
        return showOverlay(path);
    });*/

    $("#ctl00_Content_MovieDetailItem_EditFilm").click(function() {
        var path = location.pathname.replace('/israeli-film-database/films/', '/israeli-film-database/film-form/');
        return showOverlay(path);
    });

    $("#ctl00_Content_MovieDetailItem_AddFilm").click(function() {
        var path = '/israeli-film-database/film-form/';
        
        return showOverlay(path);
    });

    $("#ctl00_PromoColumn_AddNewFilmItem_AddNewFilmButton").click(function()
    {
        var path = '/israeli-film-database/film-form/';
        var title = $("#ctl00_PromoColumn_AddNewFilmItem_FilmTitle").val();
        //$("#overlay-iframe").contents().find("#FilmForm_TitleField").val();
        showOverlay(path + "?title=" + title);
        return false;
    });
/// -----------------------------------

    $("#ctl00_Content_MovieDetailItem_MovieTitleItem_EditFilm").click(function() {
        var path = location.pathname.replace('/israeli-film-database/films/', '/israeli-film-database/film-form/');
        return showOverlay(path);
    });

    $("#ctl00_Content_MovieDetailItem_MovieTitleItem_AddFilm").click(function() {
    
        var path = '/israeli-film-database/film-form/';
        return showOverlay(path);
    });


    try
    {
        initImageRollovers();
        initLightBox();
    } 
    catch (e)
    {
    }
});

function showOverlay(path)
{         
    if ($("#overlay-iframe").attr("src") != path)
    {
        $("#overlay-iframe").attr("src", path);
    }
    $('.overlay').show();
    $('#overlay-iframe').load(function(){ setPHeight(); })

    return false;
}

function setHeight(e)
{       

    if (e != undefined)
    {
	    if (e.contentDocument)
	    {
		    e.height = e.contentDocument.body.offsetHeight + 18;
	    }
	    else
	    {
		    e.height = e.contentWindow.document.body.scrollHeight;
	    }
    }
}
function setPHeight()
{        
   if(typeof (setHeight) != 'undefined') {
      setHeight($("#overlay-iframe").get(0));
      if(parent.$("#overlay-iframe")) setHeight(parent.$("#overlay-iframe").get(0));
   }

}
