﻿var dbg = {};
function LoadImg(_src) {

}

function SetStage(_artwork) {

    switch (_artwork.toLowerCase()) {
        case "artwork01":
            SetStageElement({
                logo: "logo_1.jpg",
                bw: "paperads_b01.gif",
                colour: "paperads_c01.gif",
                print: "logo1.pdf",
                lbl:"Candidate 1"
            });
            break;
        case "artwork02":
             SetStageElement({
                logo: "logo_2.jpg",
                bw: "paperads_b02.gif",
                colour: "paperads_c02.gif",
                print: "logo2.pdf",
                lbl: "Candidate 2"
            });
            break;
        case "artwork03":
            SetStageElement({
                logo: "logo_3.jpg",
                bw: "paperads_b03.gif",
                colour: "paperads_c03.gif",
                print: "logo3.pdf",
                lbl:"Candidate 3"
            });
            break;
        case "artwork04":
            SetStageElement({
                logo: "logo_4.gif",
                bw: "paperads_b04.gif",
                colour: "paperads_c04.gif",
                print: "logo4.pdf",
                lbl: "Candidate 4"
            });
            break;

    }

    function SetStageElement(_param) {

        $('a.Logoartwork').attr('href', _param.logo);
        $('a.npBW').attr('href', _param.bw);
        $('a.npColour').attr('href', _param.colour);
        $('a.Letter').attr('href', 'print/' + _param.print);
        $('a.Letter').attr('target', '_blank');

        $('.stage').show();
        $('a.Logoartwork').trigger('click');
        $('h2.currentArtwork').text(_param.lbl);
        
    }
}

$(document).ready(function(_obj) {

/*
    $('a.LoadImg').bind('click', function() {
        $('.presoImg').attr('src', 'images/' + $(this).attr('href'));
        return false;
    });
    $('a.LoadPrint').bind('click', function() {

    });
    $('a.presoManu').bind('click', function() {
        SetStage($(this).text());
        return false;
    });
    $('.selectArtwork').bind('change', function() {
        SetStage($(this).val());
    });

    SetStage('Artwork01');



    $('.whoTxtcnt p.more a').bind('click', function() { return false; });
*/
    /** */


    o.initialise();

	/*
    $(function() {
        $('#gallery a').lightBox({ fixedNavigation: true });
    });
	*/





});

var o = {
    ads: '',
    showreel: '',
    blocks: {
        whoson: 'templates/layouts/html/ks_whoson.htm',
        artistdetails: 'templates/layouts/html/kf_artistdetails.htm',
        showreel: 'templates/layouts/html/kf_showreel.htm',
        donationGuideline: 'templates/layouts/html/ks_donationGuideline.htm',
        registration: 'templates/forms/html/ks_registration.htm',
        contactus: 'templates/forms/html/ks_contactUs.htm',
        submission: 'templates/forms/html/ks_submission.htm',
        submissionConfirm: 'templates/forms/html/ks_registrationConfirm.htm',
        contactusConfirm: 'templates/forms/html/ks_contactUsConfirm.htm'
    },
    datasource: {
        ads: { ref: 'datasource/ads.xml', intervalId: '' },
        artistdetails: { ref: 'datasource/artistdetails.xml', intervalId: '' },
        showreel: { ref: 'datasource/showreel.xml', intervalId: '' }
    },
    initialise: function() {

        //InitialiseShowReelArtists();
        InitialiseAds();



        function InitialiseShowReelArtists() {
            $.get(o.datasource['showreel'].ref, function(data) {
                o.showreel = new o.services.SrcIndex($(data).find('artist'), function() { });
                o.datasource.showreel.intervalId = setInterval(o.services.Showreel, 5000);
            });

        }


        function InitialiseAds() {
            $.get(o.datasource['ads'].ref, function(data) {
                o.ads = new o.services.SrcIndex($(data).find('ad'), function() { });
                setInterval(o.services.sponsorAds, 3000);
            });
        }
    },
    services: {
        SrcIndex: function(_data) {
            var src = _data;
            var current = 0;
            var max = $(_data).length - 1;

            this.GetCurrent = function() {
			
                return $(src)[current];
            };
            this.MoveNext = function() {
                current++;
                if (current > max) {
                    current = 0;
                }
                return this.GetCurrent();

            };
            this.MovePrev = function() {
                current--;
                if (current < 0) {
                    current = max;
                }
                return this.GetCurrent();
            };


        },
        JSParser: function(_node) {
            if ($(_node).attr('jsClass') == undefined) { return; };


            var JsFilePath = '';
            var scriptInstances;
            var jsTarget = '';



            // Remove script previously loaded into DOM before loading the same for some reason such as DOM initialisations
            scriptInstances = $('body script[src="' + 'js/' + $(_node).attr('jsClass') + '.js' + '"]');

            jsTarget = 'js/' + $(_node).attr('jsClass') + '.js';
            scriptInstances = document.getElementsByTagName('script');
            dbg.scriptInstances = scriptInstances;
            dbg.jsTarget = jsTarget;

            for (var i = 0; i < scriptInstances.length; i++) {
                if (scriptInstances[i].getAttribute('src') == jsTarget) {
                    document.body.removeChild(scriptInstances[i]);
                }
            }
            /*
            if (scriptInstances.length > 0) {
                alert(scriptInstances.length);
                $(scriptInstances).each(function() {
                    $(this).remove();
                });
            }
            */

            //When removing comleted, we will instantiate the script within DOM
            try {
                var scriptElement = document.createElement('script');
                scriptElement.setAttribute('src', 'js/' + $(_node).attr('jsClass') + '.js');
                scriptElement.setAttribute('type', 'text/javascript');
                document.body.appendChild(scriptElement);
                dbg.scriptElement = scriptElement;
                //$(scriptElement).appendTo("body");
                
            }
            catch (e) {
                alert(e);
            }


        },


        Showreel: function() {

            var $artist = o.showreel.GetCurrent();
            $('.srName').text($($artist).find('name').eq(0).text());
            $('.srPlace').text($($artist).find('place').eq(0).text());
            $('.srWork img').attr('src', $($artist).find('img').eq(0).text());
            o.showreel.MoveNext();

        },
        sponsorAds: function() {
            var $ad = o.ads.GetCurrent();
            $('.adContent img').each(function() {
                $(this).fadeOut('slow', function() {
                    $(this).css('visibility', 'hidden')
                    $(this).attr('src', $($ad).find('img').text());
                    $(this).attr('alt', $($ad).find('name').text());
                    $(this).load(function() { $(this).css('visibility', 'visible').fadeIn('slow'); })
                })
            });
			/*
	            $("#adHolder").each(function() {
	                $(this).attr("href", $($ad).find('url').text());
	            })
			*/

            o.ads.MoveNext();
        },
        artistDetails: function(_artistID) {
            try {
                clearInterval(o.datasource.showreel.intervalId);
                var _name, _src, _artsrc;
                var _comment = '';




                $.get(o.datasource['artistdetails'].ref, function(data) {

                    $(data).find('artist:has(name:contains(' + _artistID + '))').each(function() {
                        _name = $(this).find('name').text();
                        $(this).find('comments p').each(function() {
                            _comment += '<p>' + $(this).text() + '</p>'
                        });
                        _artsrc = $(this).find('works work:eq(0) img').text();

                        _src = $(this).find('personalImg').text();


                        $.get(o.blocks['artistdetails'], function(_html) {

                            /**Make the stage fade out to white*/
                            var _ghtml = _html;
                            $('.KeyFeature').fadeOut('slow', function() {
                                $('.showreel').replaceWith($(_ghtml));
                                $(this).find('li.dArtistImg img').attr('src', _src);
                                $(this).find('.dItemImg img').attr('src', _artsrc);

                                $(this).find('li.dArtistTxt').html(_comment);
                                $(this).find('div.dName').text(_name);
                                $(this).fadeIn('slow');
                            });
                            return true;
                        });
                    })
                });
            }
            catch (e) {
            }


        },
        addKeyStone: function(_request) {
            RestoreKeystonelayout();

            if (_request == undefined) return;

            $.get(o.blocks[_request],
            function(data) {

                $('.Centre .permanentStone').before(data);
                $('.Centre .keystone .mBody').eq(0).css({ visibility: "hidden" }).css({ opacity: 0 }).css({ visibility: "visible" }).animate({ opacity: 1 }, 800);
                o.services.JSParser(data);
            });

            function RestoreKeystonelayout() {
                $(".Centre .keystone").each(function() {
                    if ($(this).attr('class').indexOf('permanentStone') < 0) {
                        $(this).remove();
                    };
                })
            }
        }
    }
}


