(function() {
    try {
        if (!ubk.exists) { throw "ogncObjUndefined" }

        //Commonly used functions/helpers
        ubk.get_real =
{
    curUCGID: 0,
    curUGCType: null,
    curUGCData: null,
    curUGCTitle: null,
    trackingType: null,
    captionObject: null,
    captionsIndex: 0,
    captionsLength: 0,
    curCaptionId: 0,

    //Find Poll
    findPoll: function() {
        var thisPoll = ognc.utils.getParameter('poll');

        if (thisPoll) {
            $('#' + thisPoll).removeClass('featured');
            $('#' + thisPoll).addClass('shared');
        }
    },

    //Update caption based on user click
    updateCaption: function() {

        //Turn them off for data toggles
        $('#item_copy').css('display', 'none');
        $('#caption_total').css('display', 'none');
        $('#user_id').css('display', 'none');

        $('#item_copy').html(this.captionObject.captions[this.captionsIndex].comment_text);
        $('#caption_total').html('L\351gende ' + (this.captionsIndex + 1) + ' of ' + this.captionsLength);
        $('#user_id').html(this.captionObject.captions[this.captionsIndex].comment_by + ' <em>' + this.captionObject.captions[this.captionsIndex].comment_dt + '</em>');

        this.curCaptionId = this.captionObject.captions[this.captionsIndex].comment_id;

        //Button Controls left
        if (this.captionsIndex > 0) {
            $('#left_arrow').fadeTo("normal", 1);
        } else {
            $('#left_arrow').fadeTo("normal", 0.23);
        }

        //Button Controls right
        if ((this.captionsIndex + 1) == this.captionsLength) {
            $('#right_arrow').fadeTo("normal", 0.23);
        } else {
            $('#right_arrow').fadeTo("normal", 1);
        }

        //Turn Them on
        $('#item_copy').fadeIn("slow");
        $('#caption_total').fadeIn("slow");
        $('#user_id').fadeIn("slow");

        //Set up Comments section
        document.forms.comments_form.id.value = this.captionObject.captions[this.captionsIndex].comment_id;
        this.getCommentsForCaption(this.captionObject.captions[this.captionsIndex].comment_id);

    },

    //Get comments list for current caption
    getCommentsForCaption: function(argId) {
        $.ajax({
            type: "GET",
            url: "/apps/comments/getlisthtml?id=" + argId + "&type=comment&xsl=caption_comments.xsl",
            success: function(data) {
                $('#comments_list_container').html(data);
                $('#comments_total').html('(' + ubk.commentCount + ')');
            },
            error: function(xrl, status, error) {
                console.log(xrl, status, error);
            }
        });
    },

    //Update caption -1
    preCaption: function() {
        if (this.captionsIndex > 0) {
            this.captionsIndex--;
            this.updateCaption();
        }
    },

    //Update caption +1
    nextCaption: function() {
        if ((this.captionsIndex + 1) < this.captionsLength) {
            this.captionsIndex++;
            this.updateCaption();
        }
    },

    //Example overlay
    example: function(id) {
        ubk.showFragment("preview_holder", "/apps/fragment/getreal/" + id + "_example");
    },

    //Preview overlay
    preview: function(id) {
        ubk.showFragment("preview_holder", "/apps/fragment/getreal/preview");
    },

    //Grab spoof ID and populate form 
    selectSpoof: function(id, type, videoID, videoTitle) {
        this.curUCGID = id;
        this.curUGCType = type;
        this.trackingType = 'spoof';
        if (type == "video") {
            ubk.showVideoFragment(videoID, videoTitle, 'preview_holder', "/apps/fragment/getreal/preview");
        } else {
            ubk.showFragment("preview_holder", "/apps/fragment/getreal/preview");
        }
    },

    //Stamp it overlay
    selectStamp: function(id, type) {
        this.curUCGID = id;
        this.curUGCType = type;
        this.trackingType = 'stamp';
        ubk.showFragment("preview_holder", "/apps/fragment/getreal/preview");
    },

    //UGC overlay
    selectUGC: function(id, type, title, data) {
        this.curUCGID = id;
        this.curUGCType = type;
        this.curUGCData = data;
        this.curUGCTitle = title;
        ubk.showUGCFragment(data, title, "preview_holder", "/apps/fragment/getreal/preview");
    },

    //Upload overlay
    showUploadForm: function() {
        ubk.showFragment("preview_holder", "/apps/fragment/getreal/upload_form");
        ognc.tracking.linkTrack('content', 't\351l\351chargez la v\364tre');
    },

    /*Application Form Submit Method*/
    submitForm: function(argFormId) {
        if (argFormId == 'spoof_it') {
            window.location.replace('/get_real/view/detail?id=' + this.curUCGID + '&ugc_type=stamp_it&media_type=' + this.curUGCType);
            return false;
        } else {
            return false;
        }
    },

    //Spoof image upload
    uploadSpoofImage: function(argForm, argValue) {
        argForm.form.fakeupload.value = argValue;
        $('#selected_image').empty();
        if (navigator.userAgent.indexOf('Firefox/3') >= 0) {
            argValue = argForm.form.upfile.files[0].getAsDataURL();
        }
        $("#selected_image").html('<img src="' + argValue + '" />');
        document.forms.spoof_it_form.id.value = this.curUCGID;
    },

    //Stamp it handler
    stampIt: function() {
        ubk.stampItem(this.curUCGID, true);
    },

    //Use it handler
    useIt: function() {
        $('#selected_image').empty();
        $('#selected_image').html('<img src="' + this.curUGCData + '" />');
        ubk.closeOverlay();
        document.forms.spoof_it_form.id.value = this.curUCGID;
        $('html, body').animate({
            scrollTop: $("#slot_2").offset().top
        }, 1000);
    },

    /*Application Form Submit Method for Story's*/
    submitStory: function(argForm) {
        ubk.checkProfanity(argForm, function(data) {
            if (ubk.checkFields(data, argForm)) {
                ognc.tracking.conversionTrack('content', 'write_your_story', 'UBK_CreateWrite', '2', '1');
                ubk.updateMissionFooter('own', 'story');
                argForm.submit();
            }
        });
    },

    /*Application Form Submit Method for Story's*/
    submitInspiration: function(argForm) {
        ubk.checkProfanity(argForm, function(data) {
            if (ubk.checkFields(data, argForm)) {
                ognc.tracking.conversionTrack('content', 'add', 'UBK_Inspiration', '2', '1');
                ubk.updateMissionFooter('submit', 'inspiration');
                argForm.submit();
            }
        });
    },

    submitDesignLoginForm: function(argForm) {
        ubk.checkProfanity(argForm, function(data) {
            if (ubk.checkFields(data, argForm)) {
                argForm.submit();
            }
        });
    },

    designVote: function(argId) {
        curVoteId = argId;

        if (isVoter == "") {
            message = "login";
            ubk.showFragment('design_vote', 'http://fr.ubykotex.com/apps/fragment/getreal/design_vote');
        } else {
            message = "return_voter";
            ubk.showFragment('design_vote', 'http://fr.ubykotex.com/apps/fragment/getreal/design_vote');
        }
		
    },

    submitDesignVoteForm: function(argForm) {
        var postAction = "http://fr.ubykotex.com/apps/designer/vote";
        //var postAction = "/apps/designer/login";

        //If return voter skip profanity check
        if (message == "return_voter") {

            $.ajax({
                type: "POST",
                url: postAction,
                data: $(argForm).serialize(),
                success: function(data) {
                    ubk.get_real.setVoteMessage(data.vote_result);
                    ubk.updateMissionFooter('submit', 'vote');
                },
                error: function(xrl, status, error) {
                    ubk.get_real.setVoteMessage("error");
                }
            });

        } else {

            $('#design_vote_container').css('display', 'none');
            ubk.get_real.setVoteMessage("return_voter");

            ubk.checkProfanity(argForm, function(data) {
                if (ubk.checkFields(data, argForm)) {

                    $.ajax({
                        type: "POST",
                        url: postAction,
                        data: $(argForm).serialize(),
                        success: function(data) {
                            ubk.get_real.setVoteMessage(data.vote_result);
                            ubk.updateMissionFooter('submit', 'vote');
                            message = "return_voter";
                            isVoter = ognc.getCookie('designerUser_ID');
                        },
                        error: function(xrl, status, error) {
                            ubk.get_real.setVoteMessage("error");
                        }
                    });
                }
            });
        }
    },

    setVoteMessage: function(str) {
        var activeDiv = "";

        switch (str) {
            case "return_voter":
                activeDiv = "#return_voter";
                break;
            case "success":
                activeDiv = "#thankyou";
	 			ognc.tracking.linkTrack('content', 'vote');
                break;
            case "vote_limit":
                activeDiv = "#vote_limit";
                break;
            case "error":
                activeDiv = "#error";
                break;
            default:
                return false;
        }

        $('#design_vote_message_container').css('display', 'block');
        $('#design_vote_message_container > div').css('display', 'none');
        $('#design_vote_message_container > div' + activeDiv).css('display', 'block');
    }
}

    } catch (e) { console.log("error: " + e); }
})();

$(document).ready(function () {
	// highlight the currently selected item
	
	if(ognc.utils.getParameter("poll")) {
		$("#content_" + ognc.utils.getParameter("poll")).addClass("section_border_color");
	} else if(ognc.utils.getParameter("id")) {
		$("#content_" + ognc.utils.getParameter("id")).addClass("section_border_color");
	}
	//Set Title Colors for addThis
	addthis_config.ui_header_background = "#e81e75";
	addthis_config.ui_header_color = "#000000";

	//Draw in design challenge link in secondary nav
	if (document.location.toString().indexOf("/get_real/design") > 0 && document.location.toString().indexOf("/get_real/design/tool") == -1) {
	   $('#submenu').after('<a id="enter_design_link" onmousedown="ognc.tracking.linkTrack(\'SecondaryNav\',\'Enter Design Challenge\');" href="http://fr.ubykotex.com/get_real/design/tool"><img onmouseover="$(this).attr(\'src\',\'http://fr.ubykotex.com/img/get_real/design/design_challenge_link_on.png\');" onmouseout="$(this).attr(\'src\',\'http://fr.ubykotex.com/img/get_real/design/design_challenge_link.png\');" src="http://fr.ubykotex.com/img/get_real/design/design_challenge_link.png" /></a>');
	}
	
});
