/**
 * Namespaced object. Encapsulating private methods, exposing these 
 * as public via an object proxy. 
 *
 * @author 		Zone
 * @email		info@zonecontent.com
 * @url 		http://www.zonecontent.com/
 * @copyright 	Copyright (c) 2009, zonecontent.com. All rights reserved.
 */

var greatmagazines_gift = window.gift = function($) {


    // Private variables 
    var mags =null;


    // Constructor
    (function() {

    //Create the Carousel
    
    var swf = new SWFObject("/store_templates/swf/GreatMagazinesCarousel.swf", "GreatMagazinesCarousel", "100%", "100%", "10", "#869ca7");
    swf.addParam("quality", "high");
    swf.addParam("wmode", "window");
    swf.addParam("align", "middle");
    swf.addParam("allowScriptAccess", "sameDomain");
    swf.addParam("type", "application/x-shockwave-flash");
    swf.addParam("pluginspage", "http://www.adobe.com/go/getflashplayer");

    swf.addVariable("backgroundImage", "/store_templates/img/common/background_gift_flash.jpg")
   
    swf.write("flashcontent");

    // Set Find Now Button
    $("A[href='#FilterDisplay']").click(function() {
        setData();
        return false;
    });

    getXML();
    setGenderDefaults();
    $('input:radio').click(changeGender);
       
    })();

 
    // -------------------------------
   
    function getXML() {

        $.get('/store_templates/swf/data.xml', function(xml) {
            mags = $.xml2json(xml);
        });

    };

    //------------------------------------------------------------------------
    function setData() {

        var xml = '<items>';
        var index = 0;

        $("input:checkbox:checked").each(function(i) {

            var cat = this.value.toLowerCase();

            for (var i = 0; i < mags.item.length; i++) {
                if (mags.item[i].category.toLowerCase() == cat) {
                    index += 1;
                    xml += '<item>';
                    xml += '<title>' + mags.item[i].title + '</title>';
                    xml += '<strapline>' + mags.item[i].strapline + '</strapline>';
                    xml += '<image>' + mags.item[i].image + '</image>';
                    xml += '<logo>' + mags.item[i].logo + '</logo>';
                    xml += '<copy>' + mags.item[i].strapline + '\n\n' + mags.item[i].copy + '</copy>';
                    xml += '<link url="' + mags.item[i].link + '" window="_blank"/>';
                    xml += '</item>;'
                }
            }

        });

        xml += '</items>';

        //var sampledata = '<items><item><title>EMPIRE</title> <cover>empire.jpg</cover> <logo>empireLogo.jpg</logo> <copy>Counting celebrity fans like Steven Speilberg, George Lucas and Quentin Tarrentino, EMPIRE os currently the biggest selling, most respoected film magazine on the planet</copy><link url="http://www.tink.ws" window="_blank"/></item><item><title>EMPIRE</title> <cover>empire.jpg</cover> <logo>empireLogo.jpg</logo> <copy>Counting celebrity fans like Steven Speilberg, George Lucas and Quentin Tarrentino, EMPIRE os currently the biggest selling, most respoected film magazine on the planet</copy><link url="http://www.tink.ws" window="_self"/></item><item><title>EMPIRE</title> <cover>empire.jpg</cover> <logo>empireLogo.jpg</logo> <copy>Counting celebrity fans like Steven Speilberg, George Lucas and Quentin Tarrentino, EMPIRE os currently the biggest selling, most respoected film magazine on the planet</copy><link url="http://www.tink.ws" window="_parent"/></item><item><title>EMPIRE</title> <cover>empire.jpg</cover> <logo>empireLogo.jpg</logo> <copy>Counting celebrity fans like Steven Speilberg, George Lucas and Quentin Tarrentino, EMPIRE os currently the biggest selling, most respoected film magazine on the planet</copy><link url="http://www.tink.ws" window="_top"/></item></items>'
        flashMovie = document.GreatMagazinesCarousel;
        flashMovie.setData(xml, 0); //index /  for middle2



    };

    //------------------------------------------------------------------------
    function changeGender() {

        //alert('man:' + $('input[id=optMan]').attr('checked'));
        //alert('woman:' + $('input[id=optWomen]').attr('checked'));

        var gender = $('input:radio:checked').attr('value');
        $('input:checkbox').attr('checked', false);

        switch (gender) {
            case 'man':
                $('input[id=optMan]').attr('checked', true);
                $('input[id=chkCat2]').attr('checked', true);
                $('input[id=chkCat5]').attr('checked', true);
                $('input[id=chkCat7]').attr('checked', true);
                break;
            case 'woman':
                $('input[id=optWomen]').attr('checked', true);
                $('input[id=chkCat1]').attr('checked', true);
                break;
        };

        try {
            document.GreatMagazinesCarousel.prompt(gender);
        }
        catch (err) {
            setTimeout(setGenderDefaults, 10);
        }

    };

    //------------------------------------------------------------------------
    function setGenderDefaults() {

        var gender = getQueryParamValue("gender").toLowerCase();

        switch (gender) {
            case 'man':
                $('input[id=optMan]').attr('checked', true);
                $('input[id=chkCat2]').attr('checked', true);
                $('input[id=chkCat5]').attr('checked', true);
                $('input[id=chkCat7]').attr('checked', true);
                break;
            case 'woman':
                $('input[id=optWomen]').attr('checked', true);
                $('input[id=chkCat1]').attr('checked', true);
                break;
        };

        try {
            document.GreatMagazinesCarousel.prompt(gender);
        }
        catch (err) {
            setTimeout(setGenderDefaults, 10);
        }

    };
   
    // -------------------------------


} (jQuery);
