var works = [];

String.prototype.trim = function() {
    return this.replace(/^\s*(\S*(\s+\S+)*)\s*$/, "$1");
};

$(function() {
    $("#sidebar h2, #content h1").imagegenify({
        fontColor: "#ffffff", backgroundColor: "#000000", fontSize: "24"
    });

    $("h2.red a").imagegenify({
        fontColor: "#cc001d", backgroundColor: "#000000", fontSize: "24", hoverFontColor: "#ffffff"
    });

    $("h2.show a").imagegenify({
        fontColor: "#ffffff", backgroundColor: "#000000", fontSize: "24", hoverFontColor: "#EC1F23", width: "280"
    });

    $(".features h3").imagegenify({
        fontColor: "#777777", backgroundColor: "#000000", fontSize: "19"
    });

    $(".phone").imagegenify({
        fontColor: "#cc001d", backgroundColor: "#272727", fontSize: "30"
    });

    $("#sub h3, #sidebar h3").imagegenify({
        fontColor: "#FFFFFF", backgroundColor: "#272727", fontSize: "16", fontStyle: "Bold"
    });

   $(".section-title").imagegenify({
        fontColor: "#FFFFFF", backgroundColor: "#272727", fontSize: "20", fontStyle: "Bold"
    });

    $("#main-menu a.unselected").imagegenify({
        fontColor: "#272727", backgroundColor: "#90001a", fontSize: "16", hoverFontColor: "#FFFFFF", fontStyle: "Bold"
    });

    $("#main-menu a.selected").imagegenify({
        fontColor: "#FFFFFF", backgroundColor: "#90001a", fontSize: "16", fontStyle: "Bold"
    });


    if ($("#player").length > 0) {
        flowplayer("player", "/media/flowplayer/flowplayer-3.1.2.swf");
    }

    $('#predstave-flash-arhiva').flash({
         src: '	/media/7533/exit_predstave.swf',
         width: 570,
         height: 400,
         wmode: 'transparent',
	 flashvars:{ loka:'/predstave/arhiva-predstava.aspx?alttemplate=predstavexml' }
         },{update:false});

    $('#predstave-flash').flash({
         src: '	/media/7533/exit_predstave.swf',
         width: 570,
         height: 400,
         wmode: 'transparent',
	 flashvars:{ loka:'/predstave.aspx?alttemplate=predstavexml' }
         },{update:false});


    $("#search").keydown(function(e) {
        if (e.keyCode == 13) {
            doSearch();
            return false;
        }
    });
    $("#searchSubmit").click(doSearch);

	$('#search, #tb_name, #tb_email, #ta_comment, .input-field').focus(function() {
			value=$(this).val();
			$(this).attr("value","");
	});
	$('#search, #tb_name, #tb_email, #ta_comment, .input-field').blur(function() {
			if($(this).val()=="") {
				$(this).val(value);
			}
	});

    $(".ticket-price").click(function(e) {
        //var h0 = $("#content .flexi ul").height();
	$(this).parent().parent().prev(".show-price").toggle();
        //var h = $("#content .flexi ul").height() - h0;
        //$("#content .flexi div:eq(0)").height($("#content .flexi li:eq(0)").height() + h);
        e.preventDefault();
    });

    var img = 0;
    var len = works.length;
    var startHeight = 0;

    $("#photo-gallery img")
	.load(function() {
	    if (startHeight == 0) startHeight = this.offsetHeight;
	    $("#content .flexi div:eq(0)").height($("#content .flexi div:eq(0)").height() + this.offsetHeight - startHeight);
	    startHeight = this.offsetHeight;
	})
;

    $("#metadata .next-image a").click(function(e) {
        e.preventDefault();
        if ($(this).hasClass("disabled")) return;
        if (img < works.length - 1) {
            img++;
            showImg(img);
        }
    });

    $("#metadata .prev-image a").click(function(e) {
        e.preventDefault();
        if ($(this).hasClass("disabled")) return;
        if (img > 0) {
            img--;
            showImg(img);
        }
    });

    showImg = function(no) {
        $("#photo-gallery img").attr("src", works[img].src);
        $("#imgCounter").html((img + 1) + " od " + works.length);
        $("#imgDescription").html(works[img].description);
        if (img == 0) {
            $(".prev-image a").addClass("disabled");
        } else {
            $(".prev-image a").removeClass("disabled");
        }
        if (img == works.length - 1) {
            $(".next-image a").addClass("disabled");
        } else {
            $(".next-image a").removeClass("disabled");
        }
    }

    if ($("#photo-gallery").length) showImg(0);

});


/*
addEvent(window, 'load', initCorners);

function initCorners() {
    var settings = {
        tl: { radius: 12 },
        tr: { radius: 12 },
        bl: { radius: 12 },
        br: { radius: 12 },
        antiAlias: true
    }
    curvyCorners(settings, ".flexi");
};

*/

function addWork(source, desc) {
    var t = { src: source.trim(), description: desc.trim() };
    works.push(t);
}

function doSearch() {
    var s = "/pretrazivanje.aspx?search=" + escape($("#search").val());
    window.location.href = s;
}