locationDetails = whereAmI();

function showOverlay() {
    if($("div.overlay").size() == 0) {
        $("<div class='overlay'></div>").css({position:"absolute",zIndex:10000,top:0,left:0,width:$(document).width(),height:$(document).height(),"background-color":"black",opacity:.5}).appendTo("body");
    } else {
        $("div.overlay").show();
    }
}

function hideOverlay() {
    $("div.overlay").hide();
}

function generateID() {
    var curdate = new Date()
    var seconds = curdate.getSeconds();
    var milliseconds = curdate.getTime();
    id = "id" + Math.floor(Math.random()*Math.floor(milliseconds/seconds));
    return id;
}

function getUID() {
    var d = new Date();
    var uID = Math.floor(Math.random()*d.getMilliseconds()) + "-" + Math.floor(Math.random()*d.getMilliseconds());
    return uID;
}

function checkVideo() {
    if(typeof embeddedPlayerManager.getPlayer().currentClip.title != "undefined") {
        showVideoDetails();
    } else {
        setTimeout(function () {
            checkVideo();
        }, "250");
    }
}

function showVideoDetails() {
    currClipInfo = embeddedPlayerManager.getPlayer().currentClip;
    $("div.videos span#video_title").html(currClipInfo.title);
}

function playSelectedVideo(url) {
    if($("div#videoplayer").size() != 0) {
        param = url.substring(url.lastIndexOf("=")+1, url.length);
        if(embeddedPlayerManager.getPlayer().isPlayingAd() == false) {
            embeddedPlayerManager.getPlayer().playVideo(param);
            embeddedPlayerManager.getPlayer().play();
        } else {
            alert("Please wait till the pre-roll ad is finished before selecting another video to play.");
        }
    } else {
        window.location = url;
    }
    return false;
}

function getPageScrollTop(){
    var yScrolltop;
    var xScrollleft;
    if (self.pageYOffset || self.pageXOffset) {
        yScrolltop = self.pageYOffset;
        xScrollleft = self.pageXOffset;
    } else if (document.documentElement && document.documentElement.scrollTop || document.documentElement.scrollLeft ){	 // Explorer 6 Strict
        yScrolltop = document.documentElement.scrollTop;
        xScrollleft = document.documentElement.scrollLeft;
    } else if (document.body) {// all other Explorers
        yScrolltop = document.body.scrollTop;
        xScrollleft = document.body.scrollLeft;
    }
    arrayPageScroll = new Array(xScrollleft,yScrolltop) 
    return arrayPageScroll;
}

function getPageSize(){
    var de = document.documentElement;
    var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
    var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight
    arrayPageSize = new Array(w,h) 
    return arrayPageSize;
}

function setPos(elm) {
    $elm = $(elm);
    /*var pagesize = [];
    pagesize.push($(window).width());
    pagesize.push($(window).height());*/
    var arrayPageSize = getPageSize();
    var arrayPageScroll = getPageScrollTop();
    var newLeft = arrayPageScroll[0] + (arrayPageSize[0] - $elm.width())/2;
    var newTop = arrayPageScroll[1] + (arrayPageSize[1]-$elm.height())/2;
    if(newLeft < 0) {
        newLeft = 0;
    }
    if(newTop < 0) {
        newTop = 0;
    }
    var style = {position:"absolute", left: newLeft, top: newTop, display:"none"};
    $elm.css(style).fadeIn();
}

$(document).ready(function () {
    $("div#logo").each(function () {
        var so = new SWFObject("http://accesshollywood.nbcunifiles.com/ah/flash/access-logo.swf", "logo", "250", "130", "9", "#000000");
        so.addParam("wmode", "transparent");
        so.write(this.id);
    });
    
    $(".flashcontent").each(function () {
        var newID = generateID();
        $(this).attr("id",newID);
        classNames = $(this)[0].className.split(" ");
        fontSize = 22;
        for(i=0;i<classNames.length;i++) {
            check = classNames[i].split("-");
            if(check.length > 1) {
                fontSize = check[1];
            }
        }
        $(this).removeClass("flashcontent").height(fontSize*2);
        txtString = $("#" + newID).text();
        uid = getUID();
        var so = new SWFObject("http://accesshollywood.nbcunifiles.com/ah/flash/reflectionHeadlines.swf?uid=" + uid, newID, "990", "88", "9", "#000000");
        so.addParam("wmode", "transparent");
        so.addVariable("text",txtString);
        so.addVariable("size",fontSize);
        so.addVariable("uid",uid);
        so.write(newID);
    });
    
    $("div.videos a[@href*=video]").bind("click", function () {
        playSelectedVideo(this.href);
        $("div.videos li.default").show();
        $("div.videos #video_title").empty();
        return false;
    });
    
    $("div.item div.wide a").bind("click", function () {
        if($("div#videoplayer").size() != 0) {
            embeddedPlayerManager.getPlayer().hidePlayer();
        }
        showOverlay();
        $("body").append($("<div class='contest-form'></div>"));
        frag = this.href.substring(this.href.lastIndexOf("/")+1,this.href.length) + "?" + Math.floor(Math.random()*11);
        $("div.contest-form").hide().load(frag, function () {
            setPos("div.contest-form");
            $("div.contest-form div.controls a").bind("click", function () {
                hideOverlay();
                if($("div#videoplayer").size() != 0) {
                    embeddedPlayerManager.getPlayer().unHidePlayer();
                }
                $("div.contest-form").remove();
                return false;
            });
            $("div.contest-form div.btnlcround a").bind("click", function () {
                pass = true;
                $("#giveaway_form input[@type=text], #giveaway_form select").each(function () {
                    if(this.value.length == 0) {
                        pass = false;
                    }
                });
                if(pass) {
                    baseURL = "http://ahcms2.nbcuni.com/services/ah2_contest_entry.php";
                    queryString = "?";
                    baseLength = $("div.contest-form form input[@type=text], div.contest-formform input[@type=hidden], div.contest-form form select").size();
                    $("div.contest-form form input[@type=text], div.contest-form form input[@type=hidden], div.contest-form form select").each(function (i) {
                        queryString += $(this).attr("name") + "=" + escape($(this).attr("value")) + "&";
                    });
                    $("div.contest-form form input[@type=radio], div.contest-form form input[@type=checkbox]").each(function (i) {
                        if($(this).attr("checked") == true) {
                            queryString += $(this).attr("name") + "=" + escape($(this).attr("value")) + "&";
                        }
                    });
                    queryString = queryString.substring(0, queryString.length - 1);
                    queryString += "&birth_date=00-00-0000";
                    finalURLString = baseURL + queryString;
                    $.ajax({
                        dataType: 'jsonp',
                        jsonp: 'jsonp_callback',
                        url: finalURLString,
                        success: function (data) {
                            console.log(data.message);
                            if(data.is_entered) {
                                $("div.contest-form div.thin *").hide();
                                $("div.contest-form div.thin").append($("<h3>thanks</h3><p><strong>" + data.message + "</storng></p>"));
                                setTimeout(function () {
                                    $("div.contest-form").fadeOut(function () {
                                        hideOverlay();
                                        if($("div#videoplayer").size() != 0) {
                                            embeddedPlayerManager.getPlayer().unHidePlayer();
                                        }
                                    });
                                }, "1000");
                            } else {
                                //$("div.contest-form div.thin *").not($("div.contest-form div.thin input[@type=hidden]")).hide();
                                //$("div.contest-form div.thin").prepend($("<p><strong>" + data.message + "</storng></p>"));
                                $("div.contest-form form").before($("<p class='error'>There was an error:<br/><strong>" + data.message + "</storng></p>"));
                                setTimeout(function () {
                                    $("div.contest-form div.thin p.error").remove();
                                }, "2500");
                            }
                        }
                    });
                } else {
                    $("div.contest-form form").before($("<p class='error'>There was an error, please make sure all fields are filled out before proceeding.</p>"));
                }
                return false; 
            });
        });
        return false;
    });
});
