﻿var qNo = 1;
var url = parent.document.URL;
var q = url.substring(url.indexOf("?")+3,url.indexOf("?")+4)

// if there is a question number in the url set qNo
if(q!=null && isInteger(q))
{
    qNo=parseInt(q);
}

$(function(){
    
    if(qNo==1)
    {
        $("a.arrowLeft").addClass("inactive");
        $("a.arrowRight").addClass("inactive");
    }
    if(qNo==7)
    {
        $("a.arrowRight").addClass("inactive");
    }
    
    $(".evidence").hide();
    
    var e = url.substring(url.indexOf("&e")+3,url.indexOf("&e")+7)
    // if e=true show the evidence slider
    if(e=="true")
    {
        getEvidence(qNo);
    }
    
    var noOfImages = 0;
    $(".caseStudyImages img").each(function(){
        noOfImages++;
    });
        
    $(".caseStudyImages img.hidden").css("display","block");
    
    if(noOfImages>1)
    {
        $(".caseStudyImages").jCarouselLite({
		    btnNext: "a.caseStudyRight",
		    btnPrev: "a.caseStudyLeft",
		    visible: 1,
		    speed: 1000,
		    easing: "easeOutQuad",
		    circular: true,
		    scroll: 1,
		    mouseWheel: true,
		    auto: 5000
	    }); 
    }
    else
    {
        $(".caseStudyImages").jCarouselLite({
		    visible: 1,
		    circular: false,
		    scroll: 1		    
	    }); 
    }
    
	$("a.caseStudyRight, a.caseStudyLeft").click(function(){
	    return false;
	});    
    
    // Insert the flash file
    var flashvars = {};
    var params = {
            menu: "false",
            quality: "high",
            scale: "noscale",
            wmode: "transparent" };
    var attributes = {};
    swfobject.embedSWF("../SWF/M2590QuestionsV2.swf", "questions", "300", "450", "9.0.0", "expressInstall.swf", flashvars, params, attributes);       
    
    var flashID = "questions";
    
    
    // Tell flash to move to next question
    $("a.arrowRight").click(function(){
        if(qNo<7)
        {
            qNo++;
            loadQuestion();
            $("a.arrowLeft").removeClass("inactive");
        }
        
        if(qNo==7)
        {
            $("a.arrowRight").addClass("inactive");
        }
        
        
        return false;
    });
    
    // Tell flash to move to previous question    
    $("a.arrowLeft").click(function(){
        if(qNo>1)
        {
            qNo--;
            loadQuestion();
            $("a.arrowRight").removeClass("inactive");
        }
        if(qNo==1)
        {
            $("a.arrowLeft").addClass("inactive");
        }
        return false;
    });
    
    xOffset = 40;
    yOffset = -40;
    $("a.arrow").hover(function(e){
    
        this.t = this.title;
        this.title = "";	
        var c = (this.t != "") ? "<br/>" + this.t : "";
        $("body").append("<p id='arrow'>"+ c +"</p>");								 
        $("#arrow")
            .css("position", "absolute")
            .css("z-index","10")
	        .css("top",(e.pageY - xOffset) + "px")
	        .css("left",(e.pageX + yOffset) + "px")
	        .fadeIn("fast");						
    },
    function(){
        this.title = this.t;	
        $("#arrow").remove();
    });	
    $("a.arrow").mousemove(function(e){
        $("#arrow")
	        .css("top",(e.pageY - xOffset) + "px")
	        .css("left",(e.pageX + yOffset) + "px");
    });
    
    //$(".caseStudyImages").css("visibility",""); 
    
    
});

function getEvidence(questionNo)
{
    // do ajax call to retrieve projects and put them in the evidence div.
    $(".evidence").load("GetProjects.aspx?q=" + questionNo, function(){
    
        $(".evidenceProjects").jCarouselLite({
            btnNext: "a.evidenceDown",
            btnPrev: "a.evidenceUp",
            visible: 2,
            speed: 1000,
            easing: "easeOutQuad",
            circular: true,
            vertical: true,
            scroll: 1,
            mouseWheel: true
        });
        
        $(".evidenceUp, .evidenceDown").click(function(){
            return false;
        });
        
        // Hide the evidence panel
        if(qNo!=7)
        {
            $("a.close").click(function(){
                $.expose.close();
                $(".evidence").fadeOut(500, function(){
                    $(".evidence").css("left", "");
                    $(".evidence").css("right", "-850px");
                });        
                return false;
            }); 
        }
        
    });
    
    $(".evidence")
            .show()
            .animate({easing: "swing", right: "0px"})
            .expose({closeOnClick: false});
    
}

function Tracking(question,response)
{
    $.ajax({
          type: "POST",
          url: "Default.aspx/Tracking",
          data: "{'question':'" + question + "','response':'" + response + "'}",
          contentType: "application/json; charset=utf-8",
          dataType: "json"
    });
    if(response=="No")
    {
        qNo++;
        $("a.arrowLeft").removeClass("inactive");
    }
}



function loadQuestion()
{
    var flashID = "questions";
    var questionToLoad;
    if(qNo!=null && isInteger(qNo))
    {
        questionToLoad = qNo;
    }
    else
    {
        questionToLoad = 1;
    }
    
    switch(questionToLoad)
    {
        case 1:
            window.document[flashID].GotoFrame(11);
            window.document[flashID].Play();
            $("a.arrowRight").removeClass("inactive");
            break;
        case 2:
            window.document[flashID].GotoFrame(211);
            window.document[flashID].Play();
            break;
        case 3:
            window.document[flashID].GotoFrame(311);
            window.document[flashID].Play();
            break;
        case 4:
            window.document[flashID].GotoFrame(411);
            window.document[flashID].Play();
            break;
        case 5:
            window.document[flashID].GotoFrame(511);
            window.document[flashID].Play();
            break;
        case 6:
            window.document[flashID].GotoFrame(611);
            window.document[flashID].Play();
            break;
        case 7:
            window.document[flashID].GotoFrame(711);
            window.document[flashID].Play();
            break;
    }
    Tracking(questionToLoad,"View");
}

function isInteger(s) {
    return (s.toString().search(/^-?[0-9]+$/) == 0);
}