/// <reference path="jquery-1.3.2.min.js" />

$(function() {
    
    $(".iframe").colorbox({ width: "730", height: "410", iframe: true });
    
    // What's this for?
    //$("a[rel='example1']").colorbox();   


    // Check user has registered before launching colorbox
    /*
    $(".iframe").click(function(e) {

        var target = $(this)

        var go = function() {

            var clone = target.clone()
    clone.colorbox({ width: "730", height: "410", iframe: true });

            clone.click()
    }

        var success = function(data) {

            if ($(data).find("success").size() > 0) { go() }
    else {
    location.href = "/register.shtml?returnUrl=" + window.location.pathname
    }
    }
        
    $.ajax({url:"/ajax/protected.shtml",success:success})

        return false;
    })
    */


    // Check links are protected before launching
    var isAccessible = false;
    $("a.protected , a[href^='/resource.shtml']").each(function() {

        var element = $(this)

        element.click(function() {

            if (!isAccessible) {

                var success = function() {
                    isAccessible = true;

                    if (element.is(".iframe")) { element.click() }
                    else { location.href = element.attr("href") }

                    //element.get(0).click()//triggerClick()
                }

                var failure = function() {

                    var onComplete = function() {
                        var container = $("#cboxLoadedContent")
                        var form = container.find("form")
                        form.ajaxForm({
                            success: function(data) {
                                if ($(data).find("success").size() > 0) {
                                    $.fn.colorbox.close();
                                    setTimeout(success,500)
                                }
                                else {
                                    
                                    container.html(data);
                                    onComplete();
                                    $.fn.colorbox.resize();

                                }
                            }
                        })

                    }

                    $.fn.colorbox({ href: '/ajax/registration.shtml', onComplete: onComplete,scrolling:false })

                }

                var ajaxCallback = function(data) {
                    if ($(data).find("success").size() > 0) { success() }
                    else { failure(); }
                }

                $.ajax({ url: "/ajax/protected.shtml", success: ajaxCallback })

                return false;
            }

            if (element.is(".iframe")) {
                var clone = element.clone()
                clone.colorbox({ width: "730", height: "410", iframe: true });
                clone.click()
                return false;
            }

            return true;
        })

    })



    // Outward Link logging

    $("a[href^='http']").not(".no-log").not(".log-download").click(function(event) {

        event.preventDefault()

        var data = new Object()
        data.activity_title = getActivityTitle(this)
        data.activity_type = "external_link"


        data.request_path = this.href
        data.activity_info = location.pathname

        var aTag = this
        if (aTag.target == "_blank") {
            var newWin = window.open()
        }


        var callback = function(data, textStatus) {

            if (aTag.target == "_blank") {

                newWin.location.href = aTag.href
                newWin.focus()
            }

            else { location.href = aTag.href }

        }

        $.post("/ajax/log_activity.shtml", data, callback)


    })


    // Download Click Logging

    $("a[href^='/resources/'],a.log-download").click(function(event) {

        event.preventDefault()



        var data = new Object()
        data.activity_title = getActivityTitle(this)
        data.activity_type = "download"


        data.request_path = this.href
        data.activity_info = location.pathname

        var aTag = this
        if (aTag.target == "_blank") {
            var newWin = window.open()
        }


        var callback = function(data, textStatus) {

            if (aTag.target == "_blank") {

                newWin.location.href = aTag.href
                newWin.focus()
            }

            else { location.href = aTag.href }

        }

        $.post("/ajax/log_activity.shtml", data, callback)


    })
})

function getActivityTitle(el) {
	
	var text = el.title ? el.title : $(el).text()
	if(!text) {
		text = $(el).find("img").attr("alt")	
	}
	
	return text
	
}


// This isn't working so I've given up on it
/*
jQuery.fn.triggerClick = function() {
  return this.each(function(){

    var element = $(this).get(0)
    
    console.log(element)
    
    if (document.createEventObject){
        // dispatch for IE
        var evt = document.createEventObject();
        element.fireEvent('onclick',evt)
    }
    else{
        // dispatch for firefox + others
        var evt = document.createEvent("MouseEvents");
        evt.initMouseEvent('click', true, true, element.ownerDocument.defaultView, 1, 0, 0, 0, 0, false, false, false, false, 0, null);
        this.dispatchEvent(evt);
    }
  });
};
*/

function showVideo() {
    $(".video.msi").click();
}
 
