﻿// MAIN BODY
$(function(){
    
    //*** create menu
    menu();
    
    //*** do action on page selected
    switch(getFileName()){
    case "rate-meals.aspx": 
        rateFlavor();
        break;
    case "findFlavor.aspx":
        $("div.maincnt").css("background", "url(/images/flavor-plate0.jpg) right 145px no-repeat"); //set plate pictures
        chooseFlavor();
        break;
    case "write-review.aspx": 
        submitForm();
        break;
        
        
    }
    
    
    
    
    

});


function starRate(rateId){
    $("input#ratingValue").val(rateId);

}

function submitForm(){
    var validEntry,validEntry1,validEntry2,validEntry3,validEntry4 = 0
    $("input.submit").click(function(){
        if ($.trim($(".review").val()) == "") { $(".review").css("border","solid red 1px"); $("span.error:eq(0)").fadeIn(); validEntry4 =  1 } else { $(".review").css("border","solid red 0px"); $("span.error:eq(0)").hide(); validEntry4 = 0}
        if ($.trim($(".firstName").val()) == "") { $(".firstName").css("border","solid red 1px"); $("span.error:eq(1)").fadeIn(); validEntry1 =  1 } else { $(".firstName").css("border","solid red 0px"); $("span.error:eq(1)").hide(); validEntry1 = 0}
        if ($.trim($(".city").val()) == "") { $(".city").css("border","solid red 1px"); $("span.error:eq(2)").fadeIn(); validEntry2 =  1 } else { $(".city").css("border","solid red 0px"); $("span.error:eq(2)").hide(); validEntry2 = 0}
        if ($.trim($(".state").val()) == "") { $(".state").css("border","solid red 1px"); $("span.error:eq(3)").fadeIn(); validEntry3 =  1 } else { $(".state").css("border","solid red 0px"); $("span.error:eq(3)").hide(); validEntry3 = 0}
        validEntry = validEntry1 + validEntry2 + validEntry3 + validEntry4;
        if ( validEntry == 0) { return true } else { return false}
    });
    

}

function rateFlavor(){
    $("label.seeGoodie").click( function(){
            $("img.flavorImage").hide();
            $("img.flavorImage").attr("src","/images/goodie-1.jpg").fadeIn();
    })
	
	
    
    var flavorId = 1;
    $("ul.products li label").click( function(){
        flavorId = $(this).attr("for");
        $("ul.products li label").css("text-decoration","none");
        $(this).css("text-decoration","underline");
        $("img.flavorImage").hide();
        $("img.flavorImage").attr("src","/images/flavor-" + flavorId + ".jpg").animate({ width: 'show' });
        $("img.bt-rate").hide();
        $("img.bt-rate").attr("src","/images/bt-flavor" + flavorId + ".gif").show();
    })
    
    // mouse over
    
    $("ul.products li label").mouseover(function(){
         $(this).css("text-decoration","underline");
         $("ul.products li label").mouseleave(function(){
            if ($(this).attr("for") != flavorId) {
                $(this).css("text-decoration","none");
            }
         })
    })
    
    $("label.seeGoodie").click( function(){
        $("img.flavorImage").hide();
        $("img.flavorImage").attr("src","/images/goodie-" + flavorId + ".jpg").fadeIn();
    })
	
    $("img.bt-rate").click( function(){
        $("div#rateThis").hide();
        chooseRating(flavorId);
        $("img.flavorImage").hide();
        $("img.flavorImage").attr("src","/images/flavor-" + flavorId + ".jpg").animate({ width: 'show' });
    })
    


}

function chooseFlavor() {

    var flashvars = {};
    var params = {
        wmode: "transparent"
    };
    var attributes = {
      id: "myFlavor",
      name: "myFlavor"
    };
    
    if (swfobject.hasFlashPlayerVersion("9.0.0")) {

        swfobject.embedSWF("/swf/flavor_finder.swf", "whichFlavor", "300", "250", "9.0.0", "expressInstall.swf", flashvars, params, attributes);
        
    }
    
    
    

}


function findFlavor(flavorId) {
	//alert (flavorId);
	$("div.flavorfinder").hide();
	$("div#"+ flavorId).show();
    $("div.maincnt").css("background", "url(/images/flavor-plate" + flavorId + ".jpg) right 145px no-repeat");

    $("div.share img").click(function () {

        var preview_flavors = [
        "Shrimp Lo Mein",
        "Sweet & Sour Chicken",
        "Kung Pao Chicken",
        "Beef & Broccoli",
        "Szechuan Style Chicken",
        "Orange Chicken",
        "Sweet & Spicy Shrimp",
        "General Tso's Spicy Garlic Chicken"
        ];

        $("div#"+ flavorId).hide();
        $("div.flavorShare").show();
        $("#preview_flavor").html(preview_flavors[flavorId - 1]);
    })
    
    $("#sendNow").click ( function(){
        var yourName = $("input#yourName").val();
        var yourEmail = $("input#yourEmail").val();
        var friendName = $("input#friendName").val();
        var friendEmail = $("input#friendEmail").val();
        
        if (validateEmail()){
            sendEmail(flavorId,yourName,yourEmail,friendName,friendEmail);
            $("div.flavorShare").hide();
            $("div.flavorThanks").show();
        } else { 
            // do nothing  
        }

    })



    
    $("#share-more").click ( function(){
        $("input#yourName").val("");
        $("input#yourEmail").val("");
        $("input#friendName").val("");
        $("input#friendEmail").val("");
        $("div.flavorThanks").hide();
        $("div.flavorShare").show();
    })
    
} 

function sendEmail(flavorId,yourName,yourEmail,friendName,friendEmail){

var code =new Array();      
code[1]="sl";
code[2]="sc";
code[3]="kc";
code[4]="bb";
code[5]="zc";
code[6]="oc";
code[7]="ss";
code[8]="gc";

var flavorCode = code[flavorId];
 $.ajax({
      type: "GET",
      url: "sendFlavorEmail.aspx",
      data: "productCode=" + flavorCode + "&txtSendersName=" + yourName + "&txtSendersEmail=" + yourEmail + "&txtRecipientsName=" + friendName + "&txtRecipientsEMail=" + friendEmail
 });

}

function validateEmail() {
    var validEntry,validEntry1,validEntry2,validEntry3,validEntry4 = 0

    if ($.trim($("#yourName").val()) == "") { $("span.error:eq(0)").fadeIn(); validEntry1 =  1 } else { $("span.error:eq(0)").hide(); validEntry1 = 0}
    if ($.trim($("#yourEmail").val()) == "") { $("span.error:eq(1)").fadeIn(); validEntry2 =  1 } else { $("span.error:eq(1)").hide(); validEntry2 = 0}
    if ($.trim($("#friendName").val()) == "") { $("span.error:eq(2)").fadeIn(); validEntry3 =  1 } else { $("span.error:eq(2)").hide(); validEntry3 = 0}
    if ($.trim($("#friendEmail").val()) == "") { $("span.error:eq(3)").fadeIn(); validEntry4 =  1 } else { $("span.error:eq(3)").hide(); validEntry4 = 0}
    validEntry = validEntry1 + validEntry2 + validEntry3 + validEntry4;
    if ( validEntry == 0) { return true } else { return false}

}

function chooseRating(productId) {

    $("ul.products").css("visibility", "hidden");
    $("div#rate").fadeIn();
    
    var flashvars = {};
    var params = {
        wmode: "transparent"
    };
    var attributes = {
      id: "rateFlash",
      name: "rateFlash"
    };
    
    if (swfobject.hasFlashPlayerVersion("9.0.0")) { 

        swfobject.embedSWF("/swf/star_rater_290x50.swf", "rateBar", "290", "50", "9.0.0","expressInstall.swf", flashvars, params, attributes);
    }

	function fbshare(url,title){
	if (productId == 1) {
	url = "http://www.wanchaiferry.com/orange-chicken.aspx";
	title = "I just rated Wanchai Ferry Orange Chicken and got a free Chinese paper lantern.";
	}
	else if (productId == 2) {
	url = "http://www.wanchaiferry.com/kung-pao-chicken.aspx";
	title = "I just rated Wanchai Ferry Kung Pao Chicken and got free napkin rings.";
	}
	else if (productId == 3) {
	url = "http://www.wanchaiferry.com/szechuan-style-chicken.aspx";
	title = "I just rated Wanchai Ferry Szechuan Style Chicken and got a digital fortune cookie.";
	}
	else if (productId == 4) {
	url = "http://www.wanchaiferry.com/beef-broccoli.aspx";
	title = "I just rated Wanchai Ferry Beef & Broccoli and got free instructions on how to use chopsticks.";
	}
	else if (productId == 5) {
	url = "http://www.wanchaiferry.com/sweet-spicy-shrimp.aspx";
	title = "I just rated Wanchai Ferry Sweet & Spicy Shrimp and got five beautiful Chinese art prints.";
	}
	else if (productId == 6) {
	url = "http://www.wanchaiferry.com/spicy-garlic-chicken.aspx";
	title = "I just rated Wanchai Ferry General Tso's Spicy Garlic Chicken and got an authentic Chinese zodiac placemat.";
	}
	else if (productId == 7) {
	url = "http://www.wanchaiferry.com/shrimp-lo-mein.aspx";
	title = "I just rated Wanchai Ferry Shrimp Lo Mein and learned five great Chinese phrases.";
	}
	else if (productId == 8) {
	url = "http://www.wanchaiferry.com/sweet-sour-chicken.aspx";
	title = "I just rated Wanchai Ferry General Sweet & Sour Chicken and got a free lesson in folding dinner napkins.";
	}
	else {
	url = "http://www.wanchaiferry.com";
	title = "I just rated Wanchai Ferry authentic Chinese Food!";
	}
	
	window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(url)+'&t='+encodeURIComponent(title),'FacebookShare','toolbar=0,status=0,width=626,height=350');
}  

	function fbCheckbox() {
	        var checkValue = 0
			if($('#fbSubmit').is(':checked')) {
			    checkValue = 1
			    fbshare(); 
			}
			return checkValue;
	  }

    $("img#submitFinal").click(function(){
        var ratingValue = $("input#ratingValue").val();
        
        if (ratingValue == 0 ) { $("div.errRate").fadeIn();}
        else {
		    
		    var share = fbCheckbox();
		    
		    recordRating(productId,ratingValue,share)
		
            $("div#ratings").hide();
            $("img.findOut").hide();
            $("div#ratingsThanks").fadeIn();
            $("div.thanks" + productId).fadeIn();
            
            // create flash
            if (productId == 7) {
                var flashvars = {};
                var params = {
                    wmode: "transparent"
                };
                var attributes = {
                  id: "songFlash",
                  name: "songFlash"
                };
                
                if (swfobject.hasFlashPlayerVersion("9.0.0")) { 

                    swfobject.embedSWF("/swf/wanchai_mp3_player_280x80.swf", "songPlay", "280", "80", "9.0.0","expressInstall.swf", flashvars, params, attributes);
                }
            }
        };
    })

}

function recordRating(productId,ratingValue,share){

//var data = "flavorId=" + productId + "&starRating=" + ratingValue + "&share=" + share;
//alert(data);

 $.get("/rateStars2.aspx",{'flavorId':productId,'starRating':ratingValue,'share':share});


//data: "flavorId=" + productId + "&starRating=" + ratingValue + "&share=" + share

}

function menu() {

    $("li.menuItem").mouseenter(function () {
        $('ul.subMenu li', this).css({ float: 'none' });
        $(this).stop().animate({ height: 34+$('ul.subMenu', this).height() }, 300, 'linear');
    })

    $("li.menuItem").mouseleave(function() 
    {
        $(this).stop().animate({ height: 34 }, 300, 'linear');
    })
}



function getFileName(){

    var filename = location.href.substring(location.href.lastIndexOf('/')+1) 
    var temName = filename.split("?");
    return temName[0];
    
}



function shareVideoHome(shareId, VideoId) {



    var url = "http://www.wanchaiferry.com/#" + VideoId;
    var facebookDesc = "FacebookShare - We surprised bloggers at restaurants in three cities by serving them Wanchai Ferry's Frozen Entrees for Two. See what they had to say about the switch! Visit www.wanchaiferry.com "
    var twittDesc = "Watch what happens when unsuspecting food bloggers are served Wanchai Ferry frozen entrees in Chinese restaurants in 3 cities: wanchaiferry.com"
    if (shareId == 1) {
        window.open('http://www.facebook.com/sharer.php?u=' + encodeURIComponent(url) + '&t=' + encodeURIComponent(facebookDesc), 'FacebookShare', 'toolbar=0,status=0,width=626,height=350');
    }
    else {
        window.open('http://twitter.com/share?url=' + encodeURIComponent(url) + '&amp;text=' + encodeURIComponent(twittDesc), 'Tweeter', 'toolbar=0,status=0,width=626,height=350');
    }



}



function getDefaultVid() {

    var vidId = location.href.substring(location.href.lastIndexOf('#') + 1)

    var returnValue = 0

    
    switch (vidId) {
        case "0":
            returnValue = 0
            break;
        case "1":
            returnValue = 1
            break;
        case "2":
            returnValue = 2
            break;
        case "3":
            returnValue = 3
            break;
        case "4":
            returnValue = 4
            break;
        default:
            returnValue = "0"
            

    }

    $("input#VideoId").val(returnValue);

    return returnValue
}





function playorNot() {

    var vidId = location.href.substring(location.href.lastIndexOf('#') + 1)

    var returnValue = 0


    switch (vidId) {
        case "0":
            returnValue = 0
            break;
        case "1":
            returnValue = 1
            break;
        case "2":
            returnValue = 2
            break;
        case "3":
            returnValue = 3
            break;
        case "4":
            returnValue = 4
            break;
        default:
            returnValue = -1


    }

    return returnValue
}



