/*
 * Peugeot Nagradna
 */
 
var Peugeot = {
	// Noting for now
}

/**
 *  Design parts
 */
Peugeot.Design = {

	/**
     *  Selected tab
     */
	selected: 0,

    /**
     *  Slice timout
     */
	timeout: 2000,

    /**
     *  Show next slide
     */
    next: function(section)
	{
        if (Peugeot.Design.timeout) {
            if (Peugeot.Design.selected < $('#tabs-selector img').size() - 1) {
                Peugeot.Design.selected++;
            } else {
                Peugeot.Design.selected = 0;
            }


            Peugeot.Design.select(Peugeot.Design.selected, true);
            Peugeot.Design.out();
            Peugeot.Design.over($('#tabs-selector img')[Peugeot.Design.selected]);
            
            setTimeout("Peugeot.Design.next()", Peugeot.Design.timeout);
        }
    },

    /**
     *  Tourn on/off slide
     */
    select: function(section, auto)
	{
        // Stop slide loop
        if (typeof auto == "undefined") {
            Peugeot.Design.timeout = 0;
        }

        // Change section
		$('#tabs div').each(function(index, item) {
			if (index == section) {
				$(item).show();
				Peugeot.selected = section;
			} else {
				$(item).hide();
			}
		});
    },
	
	/**
     *  Roll over
     */
    over: function(img)
	{
		img.src = img.src.replace(/_off.gif$/ig, "_on.gif");
    },
	
	/**
     *  Roll out
     */
    out: function()
	{
		// Tourn off all images except the current tab
		$('#tabs-selector img').each(function(index) {
			if (index != Peugeot.selected) {
				this.src = this.src.replace(/_on.gif$/ig, "_off.gif");
			}
		});
    }
}

/**
 *  Poll parts
 */
Peugeot.Poll = {
	
	/**
     *  Questions 
     */
	questions: new Array(
					"Koliko je potrebno plačati za PEUGEOTOV ZIMSKI PREGLED pri pooblaščenem serviserju?",
					"Med svetlobna telesa štejemo:",
					"Koliko % popusta lahko prejmete v času promocije za nakup strešnih nosilcev?"
				),

	/**
     *  Answers 
     */				
	answers: new Array(
					new Array("100 €", "20 €", "12 €"), 
					new Array("žarnice", "zavorne diske", "zimske pnevmatike"), 
					new Array("10%", "20%", "35%")
				),

	/**
     *  Right answers
     */					
	solutions: new Array(2, 0, 1),
	
	/**
     *  Current
     */
	current: 0,
	
    /**
     *  Get render header
     */
    header: function()
	{
		return "<h3 style=\"font-size: 16px\">NAGRADNA IGRA</h3>"
                + Peugeot.Poll.questions[Peugeot.Poll.current] + '<br /><br />';
	},
	
    /**
     *  Tourn on/off slide
     */
    render: function()
	{		
		// Start html
		html = Peugeot.Poll.header();
		
		// Add answers
		for (j = 0; j < Peugeot.Poll.answers[Peugeot.Poll.current].length; j++) {
			html = html + (j+1) + ')<input type="radio" value="' + j + '" name="q" id="q' + j + '" />'
						+ ' <label for="q' + j + '">' + Peugeot.Poll.answers[Peugeot.Poll.current][j] + '</label>'
                        + '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
		}
		
		// Switch image on first poll
		if (Peugeot.Poll.current) {
			src = 'kviz_nadaljuj.gif';
		} else {
			src = 'kviz_sodeluj_in_zadeni.gif';
		}
		
		html = html + '<br /><br /><img src="images/' + src  + '" alt="Nadaljij" onclick="Peugeot.Poll.check()" />';
		
		$("#poll").html(html);
    },
	
	/**
     *  Check next step
     */
    check: function()
	{
		if (Peugeot.Poll.isRight()) {
			if (Peugeot.Poll.current + 1 < Peugeot.Poll.solutions.length) {
				Peugeot.Poll.current = Peugeot.Poll.current + 1;
				Peugeot.Poll.render();
			} else {
				$.get('/obrazec', function(data) { $.facebox(data) });
			}
		}  else {
			Peugeot.Poll.current = 0;
			html = Peugeot.Poll.header()
					+ '<br /><img src="images/kviz_napacen_odgovor.gif" alt="Poskusite znova" onclick="Peugeot.Poll.render()" />';
			$("#poll").html(html);
		}
    },
	
	/**
     *  Check selected
     */
    isRight: function()
	{
		return Peugeot.Poll.solutions[Peugeot.Poll.current] == $("input[@id='q']:checked").val();
    },
	
	/**
     *  Post form
     */
    post: function()
	{		
		submit = true;

        // Check non empty fields
		var values = {};
		$('#pform :input').each(function() {
			if ($(this).val()) {
                if (this.name == "Carrebuy") {
                    if (this.checked) {
                        values[this.name] = $(this).val();
                    }
                } else {
                    values[this.name] = $(this).val();
                }
			} else {
				submit = false;
				$(this).focus();
				return;
			}
		});

        // Check TOC
        if (typeof $('#pogoji:checked').val() == 'undefined') {
            alert("Za nadaljevanje se morate strinnjati s pogoji nagradne igre.");
            $('#pogoji').focus();
            submit = false;
        }
		
		if (submit) {
			$.post('/saveData', values, Peugeot.Poll.postCallback);
		}
    },
	
	/**
     *  Post form
     */
    postCallback: function()
	{
		jQuery(document).trigger('close.facebox');

        alert("Hvala! Podatki so uspešno oddani.");
    }
}

/*
 * Start script
 */
jQuery(document).ready(function($) {

	// load Facebox
	$('a[rel*=facebox]').facebox();
	
	// Start poll
	// Peugeot.Poll.render();
	
	// ADMIN 
    //$.get('/obrazec', function(data) { $.facebox(data) });

    // Slide animation
    setTimeout("Peugeot.Design.next()", Peugeot.Design.timeout);
}) 