/* (C) Innoware Ltd 2008 */

/*jslint  eqeqeq: true, browser: true */
/*global $, JSON, Babel */

$(document).ready(function () {
 McGurk.TurnOnLightBox();
 McGurk.filterList('');
});

var McGurk = function() {

    var that = {}; 

	that.deleteCar = function(){
	    if ($('#carID').val() !== '-1')
	    {
	        Babel.Delete($('#carID').attr('babel'),function(){
	            location.href="../search";
	        });
	    }
	};
	
	
	
	that.repIm = function(el)
	{
	    $('#pic' + el.id).attr('src',$(el).val());
	}
	
	that.imgUp = function(num)
	{

	    var height = 80;
	    var width = 146;
	    var idField = 'p' + num;
	    if (num === 0)
	    {
	        height = 242;
	        width =427;
	    }
	
        b2.uploadImg(width,height,idField);	    
	};
	

	that.filterList = function(sel){
	    if (sel !== '')
	    {
	        $('.carSynopsis').hide();
	        $('#noMatches').hide();
            if (typeof sel !== 'undefined')
            {
                if (sel==='rem'){
                    $('.carFilter').val('');
                }
                else{
                    $('#' + sel).val('');
                }
            }
            
	        $list = $('#carMatches>.carSynopsis');
    	    
	        if ($('#yearFilt').val() !== ''){
	            $list.tsort('.age',{order:$('#yearFilt').val()});
	        }
	        else if ($('#priceFilt').val() !== ''){
	            $list.tsort('.price',{order:$('#priceFilt').val()});
	        }
    	    
	        if($('#manFilt').val() === ''){
	            $('.carSynopsis').show();
	        }
	        else{
	            $('.' + $('#manFilt').val()).show();
	        }
	    }
	    if ($('#carMatches>.carSynopsis:visible').length === 0)
	    {
    	    $('#noMatches').show();
	    }
	};
	
	that.TurnOnLightBox = function(){
	    $('.lightbox').each( function(){
	        var href = $(this).attr('href').replace('/web/','/canvas/');
                $(this).attr('href',href)
	    });
	    $('.lightbox').lightBox();
	};
	
	that.PlaySound = function(sURL){
	    $('#soundSpan').html('<embed src="' + sURL + '" hidden="true" autostart="true" loop="false"/>');
	};
	
	that.ShowFAQVid = function(sURL){
		$('#FAQVideos').html('<embed src="mediaplayer.swf" allowscriptaccess="always" allowfullscreen="true" width="340" height="280" flashvars="autostart=true&repeat=false&file=' + sURL +'" />');

	};
	
	that.playMainVid = function(){
		jQuery('#content').css({background: 'url("")', backgroundColor: 'black'});
		$('#content').hide();
		$('#vidContent').show();
		$('#vidContent').html('<embed src="mediaplayer.swf" allowscriptaccess="always" allowfullscreen="true" width="720" height="586" flashvars="autostart=true&repeat=false&file=vids/MainVid.swf" />');

	};
	
	that.showFinanceVid = function(sURL){
		$('#financeVid').html('<embed src="mediaplayer.swf" allowscriptaccess="always" allowfullscreen="true" width="340" height="280" flashvars="autostart=true&repeat=false&file=' + sURL +'" />');

	};

	that.showWarrantyVid = function(sURL){
		$('#warrantyVid').html('<embed src="mediaplayer.swf" allowscriptaccess="always" allowfullscreen="true" width="340" height="280" flashvars="autostart=true&repeat=false&file=' + sURL +'" />');

	};
	
	that.sendEmail = function(emailAdd){
		if (emailAdd !== '')
		{
			var e =Babel.SendEmail('sales@mcgurk.com','News Letter Sign up','Someone has signed up to the news letter with the email address of: ' + emailAdd).value;
			if (e===true)
			{
				$('#emailIn').val('Thanks');
			}
		}

	};
	
	that.notifyInnoware = function(id) {
		Babel.SendEmailTo('toby.harison-denby@innoware.co.uk','support@innoware.co.uk','McGurk: New Car Added','McGurk have just added / updated a car on their website.\n\nhttp://www.mcgurk.com/cardetails/' + id + '\n\nGo to the McGurkNEW database on the Planir server, open the Pic table and remove the repeat slashes in the file paths to the car photos in the Pic table.\n\nMcGurk will phone you to complain about photo download speed issues if you do not do this. You have been warned!');
	};
	
	return that;
}();