/* App Wide Scripts */
var jq = jQuery.noConflict();
function generateUniqueId()
{
    var newDate = new Date;
    return newDate.getTime();
}
function jobTitleOtherSelected()
{
    var value = jq('#title').val();
    if (value == 'Other') {
        jq('#titleOther-element').show();
        jq('#titleOther').show();
    } else {
        jq('#titleOther-element').hide();
        jq('#titleOther').hide();
    }
}

function readMoreClick()
{
    jq('A[class=rm_button]').live('click', function(){
        var jobId = jq(this).attr('rel');
        var originalContent = jq('SPAN[id=jd_' + jobId + ']').html();
        var newDate = new Date;
        var param = {jobId:jobId, token:newDate.getTime()};
        jq.getJSON(baseUrl + '/ajax/get-Job-Description', param, function(json){
            if (json.errorCode != 0) {
                alert(json.errorMsg);
            } else {
                var content = json.content;
                content = content + ' <a href="javascript:void(0);" class="rl_button" id="' + jobId + '">Less...</a>';
                jq('SPAN[id=jd_' + jobId + ']').html(content);
                jq('A[class=rl_button]').live('click', function(){
                    jq('SPAN[id=jd_' + jobId + ']').html(originalContent);
                });
            }
        });
    });
}

function initTweet()
{
    jq(".tweet").tweet({
        username: "HeadlinesComms",
        join_text: "auto",
        /*avatar_size: 32,*/
        /*avatar_size: "null",*/
        /*template: function(i){return i["{text}"]},*/
        count: 3,
        auto_join_text_default: "we said,",
        auto_join_text_ed: "we",
        auto_join_text_ing: "we were",
        auto_join_text_reply: "we replied to",
        auto_join_text_url: "we were checking out",
        loading_text: "loading tweets..."
    });
}
function imageRollovers()
{
    jq('#welcomeStar img').hover(
        function() {
            jq(this).attr('src', baseUrl + '/images/star_over.png');
        },
        function() {
            jq(this).attr('src', baseUrl + '/images/star.png');
        }
    );
    jq('#signup').hover(
        function() {
            jq(this).attr('src', baseUrl + '/images/signup_over.jpg');
        },
        function() {
            jq(this).attr('src', baseUrl + '/images/signup_up.jpg');
        }
    );
    jq('#my-account').hover(
        function() {
            jq(this).attr('src', baseUrl + '/images/my_account_over.jpg');
        },
        function() {
            jq(this).attr('src', baseUrl + '/images/my_account.jpg');
        }
    );
    jq('#settings').hover(
        function() {
            jq(this).attr('src', baseUrl + '/images/settings_over.jpg');
        },
        function() {
            jq(this).attr('src', baseUrl + '/images/settings.jpg');
        }
    );
    jq('#twitter-logo').hover(
        function() {
            jq(this).attr('src', baseUrl + '/images/twitter_logo_over.jpg');
        },
        function() {
            jq(this).attr('src', baseUrl + '/images/twitter_logo_s1.jpg');
        }
    );
    jq('#facebook-logo').hover(
        function() {
            jq(this).attr('src', baseUrl + '/images/facebook_logo_over.jpg');
        },
        function() {
            jq(this).attr('src', baseUrl + '/images/facebook_logo_s1.jpg');
        }
    );
    jq('#linkedin-logo').hover(
        function() {
            jq(this).attr('src', baseUrl + '/images/linkedin_logo_over.jpg');
        },
        function() {
            jq(this).attr('src', baseUrl + '/images/linkedin_logo_s1.jpg');
        }
    );
    jq('#headlines_logo').hover(
        function() {
            jq(this).attr('src', baseUrl + '/images/headlines_logo_over.jpg');
        },
        function() {
            jq(this).attr('src', baseUrl + '/images/headlines_logo.jpg');
        }
    );
    jq('#magazine_logo').hover(
        function() {
            jq(this).attr('src', baseUrl + '/images/magazine_logo_over.jpg');
        },
        function() {
            jq(this).attr('src', baseUrl + '/images/magazine_logo.jpg');
        }
    );
    jq('#worldpay_logo').hover(
        function() {
            jq(this).attr('src', baseUrl + '/images/worldpay_logo_over.jpg');
        },
        function() {
            jq(this).attr('src', baseUrl + '/images/worldpay_logo.jpg');
        }
    );
    jq('#apply-button').hover(
        function() {
            jq(this).attr('src', baseUrl + '/images/apply_over.png');
        },
        function() {
            jq(this).attr('src', baseUrl + '/images/apply.png');
        }
    );
    jq('#pdf_download img').hover(
        function() {
            jq(this).attr('src', baseUrl + '/images/pdf_arrow.jpg');
        },
        function() {
            jq(this).attr('src', baseUrl + '/images/pdf_arrow_s2.jpg');
        }
    );
}
function initCarousel()
{
    jq(".scroller").jCarouselLite({
        btnNext: ".next",
        btnPrev: ".prev",
        vertical: true,
        /*mouseWheel: true,*/
        speed: 500,
        easing: "backout"
    });
    jq(".scroller img").click(function() {
        //Load the selected image into the large div with class="mid" on clicking the thumbnail
        jq(".mid img").attr("src", jq(this).attr("src"));
        //Set the link for the download pdf button depending on the clicked thumbnail image.
        <!--TODO. Prevent external direct access to this pdf directory-->
        jq("#downloadpdf").attr("href", baseUrl + '/file/view/pdf/' + jq(this).attr("id") + '/' + generateUniqueId());
        jq("a.flipper").attr("href", jq(this).attr("rel"));
        <!-- pdf/issue_13.pdf-->
    });
}
function initFlipper()
{
    var loadInIframeModal = function(hash){
        var $trigger = jq(hash.t);
        var $modal = jq(hash.w);
        var myUrl = $trigger.attr('href');
        var myTitle= $trigger.attr('title');
        var $modalContent = jq("iframe", $modal);

        $modalContent.html('').attr('src', myUrl);
        //let's use the anchor "title" attribute as modal window title
        jq('#jqmTitleText').text(myTitle);
        hash.w.show();
    }
    // initialise jqModal
    jq('#modalWindow').jqm({
        modal: true,
        trigger: 'a.flipper',
        target: '#jqmContent',
        onShow:  loadInIframeModal
    });
}
jq(document).ready(function() {
    if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i))) {
        jq("#interactive").hide();
    };
    initFlipper();
    readMoreClick();
    if (jq(".tweet").length > 0) {
        initTweet();
    }
    imageRollovers();
    initCarousel();
});
