var $output;
var $result;
var $html;
var name = "test";
var myTimer;

var header = Array();
var css = Array();

var text =  '<div class="poem">You know the day destroys the night<br />'+
            'Night divides the day<br />'+
            'Tried to run<br />'+
            'Tried to hide<br />'+
            'Break on through to the other side<br />'+
            'Break on through to the other side<br />'+
            'Break on through to the other side, yeah<br />'+
            'We chased our pleasures here<br />'+
            'Dug our treasures there<br />'+
            'But can you still recall<br />'+
            'The time we cried<br />'+
            'Break on through to the other side<br />'+
            'Break on through to the other side<br /><br />'+
            'Yeah!<br />'+
            'C\'mon, yeah<br />'+
            'Everybody loves my baby<br />'+
            'Everybody loves my baby<br />'+
            'She get<br />'+
            'She get<br />'+
            'She get<br />'+
            'She get high<br /><br />'+
            'I found an island in your arms<br />'+
            'Country in your eyes<br />'+
            'Arms that chain us<br />'+
            'Eyes that lie<br />'+
            'Break on through to the other side<br />'+
            'Break on through to the other side<br />'+
            'Break on through, oww!<br />'+
            'Oh, yeah!<br /><br />'+
            'Made the scene<br />'+
            'Week to week<br />'+
            'Day to day<br />'+
            'Hour to hour<br />'+
            'The gate is straight<br />'+
            'Deep and wide<br />'+
            'Break on through to the other side<br />'+
            'Break on through to the other side<br />'+
            'Break on through<br />'+
            'Break on through<br />'+
            'Break on through<br />'+
            'Break on through<br />'+
            'Yeah, yeah, yeah, yeah<br />'+
            'Yeah, yeah, yeah, yeah, yeah</div>';

header["default"] = '<html>\n'+
                    '<head>\n'+
                    '   <title>CSSFly - Design HTML & CSS on the fly!</title>\n'+
                    '</head>\n'+
                    '<body>\n'+
                    '   <div id="header">"Break on through" by The DOORS</div>\n'+
                    '   <div id="content"><p>Play with HTML and CSS with live Preview or <a href="/csslyrics/">transform your poem to CSSLyrics</a>...</p>'+text+'</div>\n'+
                    '   <div id="footer">&copy; CSSFLY</div>\n'+
                    '</body>\n'+
                    '</html>\n';
css["default"] =    'body,html{\n'+
                    '   background:0;\n'+
                    '   font-family:Verdana,Arial,sans;\n'+
                    '   font-size:21px;\n'+
                    '   margin:0; padding:0;\n'+
                    '}\n'+
                    'a{ color:#900; }\n'+
                    '.poem{ font-size:12px; text-align:center;}\n'+
                    '#header{ \n'+
                    '   background:#212121; \n'+
                    '   color:#ccc; \n'+
                    '   padding:10px 0px; \n'+
                    '   min-height:30px; \n'+
                    '   text-align:center; \n'+
                    '}\n'+
                    '#content{ background:#fff; padding:20px; min-height:500px; }\n'+
                    '#footer{ \n'+
                    '   background:#666; \n'+
                    '   color:#fff; \n'+
                    '   height:30px;\n'+
                    '   font-size:11px; \n'+
                    '   text-align:center; \n'+
                    '   padding-top:10px; \n'+
                    '}\n';

$(document).ready( function()
{    
    $html = $("#htmloutput");
    $output = $("#cssoutput");
    $result = $("#result");
   
    $html.val(header["default"]);
    $output.val(css["default"]);

    $("#btnconvert").bind("click" , renderHTML);    
    $("#btnconvert").trigger("click");
    
    $("#cssoutput,#htmloutput").keyup(d);
});
function cssToggle()
{
    $("#wrapper").toggle();
}
function htmlToggle()
{
    $(".htmloutput").show();
    $(".cssoutput").hide();
    $("#togglehtml").addClass("active");
    $("#togglecss").removeClass("active");
}
function cssToggle2()
{
    $("#togglehtml").removeClass("active");
    $("#togglecss").addClass("active");
    $(".cssoutput").show();
    $(".htmloutput").hide();
}
function d()
{
    clearInterval(myTimer);
    myTimer = setInterval(renderHTML , 1000);
}
function renderHTML()
{
    var stamp = Math.random()*100000 + "";
    var str = stamp.substr(0,10);
    var htmlStr = $html.val();
    var fcss = $output.val();
    
    fcss = fcss.replace(/body,html/g, "#result").
                replace(/body /g, "#result").
                replace(/body\{/g, "#result{").
                replace(/html /g, "#result").
                replace(/html\{/g, "#result{");
    
    $("head style").text(fcss);
    var cont = htmlStr.split("<body>")[1].split("</body>")[0].replace(/\$\.post/gi,'\\$\\.post').replace(/\$\.put/gi,'\\$\\.put').replace(/\$\.delete/gi,'\\$\\.delete').replace(/\$\.ajax/gi,'\\$\\.ajax');
    var tit = htmlStr.split("<title>")[1].split("</title>")[0];
    
    $result.html(cont);
    $("title").text(tit);
}


