
/* for the radio player to load the mp3 */
function getFlashMovieObject(movieName) {
    if (window.document[movieName]) {
        return window.document[movieName];
    }

    if (navigator.appName.indexOf("Microsoft Internet")==-1) {
        if (document.embeds && document.embeds[movieName]) {
            return document.embeds[movieName];
        }else{
            return document.getElementById(movieName);
        }
    }
    return null;
}

function bindShowComments() {
    $('.showcomments').click(function() {
        $('#form-'+$(this).attr('id')).slideToggle('fast');
        $('#display-'+$(this).attr('id')).slideToggle('fast');
    });
}

function SendDataToFlashMovie(id){
    getFlashMovieObject("player_app").sendTextToFlash(id);
}


/* cant seem to get this to call at the minute */
function refreshComments(contentType, objectID) {
    $.ajax({
        type: 'POST',
        url: '/comments/list/'+contentType.slice(0, contentType.indexOf('.'))+'/'+objectID+'/',
        data: null,
        dataType: 'html',
        success: function(html) {

        },
        error: function(html) {
            alert('howd you do that?');
        }
    });
}

function doSifr(elem, bgcolor, size) {
    /* whole thing dropped and back to plain text, on the off chance...
    if (!size) {
        size = "14";
    }
    if (!elem) {
        elem = "h3.main-title";
    }
    if (!bgcolor) {
        bgcolor = "004A68";
    }
    $(document).ready(function() {
        if(typeof sIFR == "function"){
            sIFR.replaceElement(elem, named({sFlashSrc: "/media/swf/gothamultra.swf", sColor: "#FFFFFF",sBgColor: "#"+bgcolor, sWmode : "transparent", sCase: "upper", sSize: size }));
        };
    });

    */
}

function handleComment() {
    container = null;
    $(this).parent('div').each(function() {
        container = $(this).attr('id');
    });
    container = 'div#' + container;

    var cdata = {
            'email' : $(container+' > form > p > input[name=email]').val(),
            'name' : $(container+' > form > p > input[name=name]').val(),
            'url' : $(container+' > form > p > input[name=url]').val(),
            'comment' : $(container+' > form > p > textarea[name=comment]').val(),
            'content_type' : $(container+' > form > input[name=content_type]').val(),
            'object_pk' : $(container+' > form > input[name=object_pk]').val(),
            'timestamp' : $(container+' > form > input[name=timestamp]').val(),
            'security_hash' : $(container+' > form > input[name=security_hash]').val(),
            'honeypot' : $(container+' > form > input[name=honeypot]').val()
    }
    if (cdata['honeypot'] == undefined) {
        cdata['honeypot'] = '';
    }
    var postData = '';

    if (cdata['url'] == 'URL') {
        cdata['url'] = '';
    }

    for(key in cdata) {
        postData += key + '=' + cdata[key] + '&';
    }

    $.ajax({
        type: 'POST',
        url: '/comments/post/',
        data: postData,
        dataType: 'html',
        success: function(html) {
            $(container).html(html);
            addFormLabels();
        },
        error: function(html) {
            alert('em, that shouldnt of happened! - ajax fail');
        }
    });
    return false;
}

function makeRounded() {
    $(document).ready(function() {
        $('.post').corner('5px');
        $('.content').corner('5px');
    });
}
function hideComments() {
    $('.comment-form').hide();
    $('.comments').hide();
}
function addFormLabels() {
    $('label').hide();
    $('label').each(function(){
        target = $(this).next();
        if (target.attr('name') != 'honeypot' && target.attr('value') == '' && target.get(0).tagName != 'SELECT') {
            if ($(this).text()[$(this).text().length - 1] == ':') {
                target.attr('value', $(this).text().substr(0, $(this).text().length - 1));
            } else {
                target.attr('value', $(this).text());
            }

            target.bind('click', function() {
		if($(this).attr('id')!='id_comment') {
	              $(this).attr('value', '');
		}
            });

            target.attr('rows', '3');
            target.attr('cols', '5');
            }
    });
    $('.cform').bind('submit', handleComment);
}

function bindGallery() {
    $('.gallery, .exclusive a').fancybox({
        'overlayColor':'#1f5a7c',
        'overlayOpacity':0.9,
        'frameWidth':390,
        'frameHeight':390
    });
    $('.iframe-gallery').fancybox({
        'overlayColor':'#1f5a7c',
        'overlayOpacity':0.9,
        'frameWidth':720,
        'frameHeight':530
    });
    $('#fancy_inner').corner('5px');
}

function bindCommentClose() {
    $('.closecomments').bind('click', function() {
        var target_id = null;
        $(this).parent().parent().children('input').each(function() {
            if ($(this).attr('name')=='object_pk') {
            target_id = $(this).val();
            }
        });
        if(target_id) {
            $('#display-comments-'+target_id).slideUp();
            $('#form-comments-'+target_id).slideUp();
        }
    });
}

$(document).ready(function() {

    makeRounded();
    addFormLabels();
    bindGallery();
    bindCommentClose();
    hideComments();

    $('.popup').hide();

    $('.popup-container').hover(function() {
        $(this).find('.popup').slideDown('fast');
    }, function() {
        $(this).find('.popup').slideUp('fast');
    });


    $('#latestcomp').click(function() {
        $('#center').load('/competition/ajax/', function() {
            makeRounded();
            addFormLabels();
            bindGallery();
        });
    });

    $('#latestreview').click(function() {
        $('#center').load('/reviews/latest/ajax/', function() {
            setActive('review');
            makeRounded();
            hideComments();
            addFormLabels();
            bindGallery();
            bindShowComments();
            bindCommentClose();
        });
    });

    $('.showcomments').click(function() {
        $('#form-'+$(this).attr('id')).slideToggle('fast');
        $('#display-'+$(this).attr('id')).slideToggle('fast');
    });

    $('#frm-newslettersignup').submit(function(e) {
        e.preventDefault();
        container = $('#newsletter-input');
        var cdata = {
            'email' : $('form#frm-newslettersignup > input[name=email]').val(),
            }
        var postData = '';
        for(key in cdata) {
            postData += key + '=' + cdata[key] + '&';
        }
        $.ajax({
            type: 'POST',
            url: '/newsletter/signup/',
            data: postData,
            dataType: 'html',
            success: function(html) {
                $(container).replaceWith(html);
                addFormLabels();
                },
            error: function(html) {
                alert('em, that shouldnt of happened! - ajax fail');
            }
        });
        return false;
    });

    $('#newsletter-field').focus(function() {
	if($(this).val('Sign Up...')) 
            $(this).val('');
    });    

    $('#search-field').focus(function() {
	if ($(this).val()=='Search...')
       	    $(this).val('');
    });
    $('#search-field').keypress(function(e) {
        if(e.which == 13) {
            $(this).blur();
            $('#search-btn').focus().click();
        }
    });
    $('#search-btn').click(function() {
        var query = $('#search-field').val();
        $('#center').load('/search/'+escape(query)+'/', function() {
            makeRounded();
            addFormLabels();
            bindGallery();
            bindCommentClose();
            hideComments();
            doSifr();
        });
    });

});
