$(document).ready(function() {

touchClick=touchDeviceTest();

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// LOAD CSS
$("<link/>",{rel: "stylesheet",type: "text/css",href: "includes/css/site.css"}).appendTo("head");

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//DRAW:

// DRAW LOGO
$("body").prepend('<div id="mywdot_logo" ><a href="http://www.mywdot.com"></a></div>');

// DRAW LOADING CONTAINER
$("body").prepend('<div id="mywdot_loading"></div>');

// DRAW CHECKOUT BTN
$("body").prepend('<div id="mywdot_loadCheckoutBtn">\
<div style="float:left; padding-top:7px; padding-left:70px;"><span style="font-size:18px;">Checkout</span></div><div id="checkoutBtn_number_items" style="float:left; padding-left:70px;">Items in Cart <span id="numberOfItemsInCart"></span></div>\
</div>');

// DRAW CHECKOUT IF IT DOES NOT EXIST
if($('#mywdot_checkout').length==0){
$("body").prepend('<div id="mywdot_checkout"></div>');
}

// DRAW ECOMMERCE ITEMS IF IT DOES NOT EXIST
if($('#mywdot_ecommerceItems').length==0&&$('.mywdot_loadEcommerceItemsBtn').length>0){
$("body").prepend('<div id="mywdot_ecommerceItems"></div>');
}

// DRAW COMMENTS IF IT DOES NOT EXIST
if($('#mywdot_comments').length==0&&$('.mywdot_loadCommentsBtn').length>0){
$("body").prepend('<div id="mywdot_comments"></div>');
}
//response[\'href\']
// FACEBOOK - load sdk and if attr.loadUrl exists for mywdot_comments - preload the comments
//FB.Event.subscribe(\'comment.create\',function(response) { $.ajax({ type: "POST", data: "action=addFacebookComment&mediaId="+encodeURIComponent(1000)+"&commentId="+encodeURIComponent(response[\'commentID\']), url: "includes/ajax/profile.php"})});\

$("body").prepend('<div id="fb-root"></div>\
<script>window.fbAsyncInit = function() {\
FB.Event.subscribe(\'comment.create\',function(response) { $.ajax({ type: "POST", data: "action=addFacebookComment&href="+encodeURIComponent(response[\'href\'])+"&commentId="+encodeURIComponent(response[\'commentID\']), url: "includes/ajax/profile.php"})});\
FB.Event.subscribe(\'comment.remove\',function(response) { $.ajax({ type: "POST", data: "action=removeFacebookComment&commentId="+encodeURIComponent(response[\'commentID\']), url: "includes/ajax/profile.php"})});\
FB.init({appId:\'330188633677789\',channelUrl:\'www.mywdot.com\',status:true,cookie:true,xfbml:true});\
if(typeof $("#mywdot_comments").attr(\'loadUrl\') != \'undefined\'){loadComments_function($("#mywdot_comments").attr(\'loadUrl\'));}\
};\
(function(d){var js, id = \'facebook-jssdk\'; if (d.getElementById(id)) {return;} js = d.createElement(\'script\'); js.id = id; js.async = true;js.src = "//connect.facebook.net/en_US/all.js";d.getElementsByTagName(\'head\')[0].appendChild(js);}(document));\
</script>');



///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// BTN FUNCTIONALITY

// LOAD ECOMMERCE ITEMS
$('.mywdot_loadEcommerceItemsBtn').live(touchClick, function(event){
event.stopPropagation();
hide_function();
loadEcommerceItems_function($(this).attr('mediaId'));})

// ADD TO CART BTN
$('.addToCartBtn').live(touchClick, function(e){
e.stopPropagation();
addToCart_function($(this).attr('itemId'));
})

// LOAD ECOMMERCE ITEMS
$('.mywdot_loadCommentsBtn').live(touchClick, function(event){
event.stopPropagation();
hide_function();
loadComments_function($(this).attr('loadUrl'));
})

// CHECKOUT BTN
$('#mywdot_loadCheckoutBtn').live(touchClick, function(e){
e.stopPropagation();
hide_function();
loadCheckout_function();})

// REMOVE FROM CART BTN
$('.removeFromCartBtn').live(touchClick, function(e){
e.stopPropagation();
removeFromCart_function($(this).attr('itemId'));
})

// CLOSE BTN
$('.closeBtn').live(touchClick, function(e){
e.stopPropagation();
$('#'+$(this).attr('closeDiv')).mooSlideUp("slow");
showHide_checkoutBtn_fucntion();
})


// CLOSE CHECKOUT
$('#paypalBtn').live(touchClick, function(e){
e.stopPropagation();
if($('#nameAndAdress').val()==''){
alert('Please add your delivery details');
return false;}
if($('#termsAndConditions').attr('checked')!='checked'){
alert('You must agree to the terms and conditions');
return false;}
$('#cn').val($('#nameAndAdress').val());
$('#paypalForm').submit();
})







///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// DRAW CONTACT FORM
if($('#mywdot_contactForm').length>0){

if(typeof $('#mywdot_contactForm').attr("backgroundColor")!='undefined'){var backgroundColor=$('#mywdot_contactForm').attr("backgroundColor");}else{var backgroundColor="#dadada";}
if(typeof $('#mywdot_contactForm').attr("borderCss")!='undefined'){var borderCss=$('#mywdot_contactForm').attr("borderCss");}else{var borderCss="0px solid #dadada";}
if(typeof $('#mywdot_contactForm').attr("errorColor")!='undefined'){var errorColor=$('#mywdot_contactForm').attr("errorColor");}else{var errorColor="#f9c9c7";}
if(typeof $('#mywdot_contactForm').attr("textColor")!='undefined'){var textColor=$('#mywdot_contactForm').attr("textColor");}else{var textColor="#4a4a4a";}
if(typeof $('#mywdot_contactForm').attr("formLabelColor")!='undefined'){var formLabelColor=$('#mywdot_contactForm').attr("formLabelColor");}else{var formLabelColor="#fff";}
if(typeof $('#mywdot_contactForm').attr("fontSize")!='undefined'){var fontSize=$('#mywdot_contactForm').attr("fontSize");}else{var fontSize="18px";}

$('#mywdot_contactForm').html('<div class="formLabel">Name</div>\
<input class="contactFormData" type="text" id="contactName" name="contactName" defaultInput="Please add your name" />\
<div class="formLabel">Telephone</div>\
<input class="contactFormData" type="text" id="contactTelephone" name="contactTelephone" defaultInput="Please add your telephone number" >\
<div class="formLabel">Email</div>\
<input class="contactFormData" type="text" id="contactEmail" name="contactEmail" defaultInput="Please add your email" >\
<div class="formLabel">Message</div>\
<textarea class="contactFormData" id="contactMessage" name="contactMessage" defaultInput="Please add a message" rows="6"></textarea>\
<div class="greenBtn" id="sendBtn"><span>Send</span></div></div>');

$('.contactFormData').css({'background':backgroundColor,'border-color':backgroundColor,'color':textColor,'border': borderCss,'padding':'5px','fontSize':fontSize,'font':'100% Arial, Helvetica, sand-serif'});
$('.formLabel').css({'color':formLabelColor});
}

$('.contactFormData').each(function() {
$(this).focus(function() {
if($(this).val()==$(this).attr('defaultInput')){
$(this).val('');
$(this).css({'background':backgroundColor});}})

$(this).blur(function() {
if($(this).val()==''||$(this).val()==$(this).attr('defaultInput')){
$(this).css({'background':errorColor});
$(this).val($(this).attr('defaultInput'));
}});
})

$('#sendBtn').click(function(e){								 
var error=0;
$('.contactFormData').each(function() {
if($(this).val()==''||$(this).val()==$(this).attr('defaultInput'))
{$(this).css({'background':errorColor});
$(this).val($(this).attr('defaultInput'));
error=1;}
})
if(error==0){
sendMessage_function();
}
})


// SEND MESSAGE
function sendMessage_function(){
var inputData_string='';
var inputs_array = $('.contactFormData');
var i=0;
inputs_array.each(function() {
if(i!=0){inputData_string+='&';}
inputData_string+=this.name+'='+encodeURIComponent($(this).val());
i++;
});
$.ajax({
type: "POST",
url: "includes/ajax/site.php",
data: "action=sendMessage&"+inputData_string+'&userId='+encodeURIComponent($('#mywdot_contactForm').attr("userId")),
dataType: 'html',
success: function(html){
$('#mywdot_contactForm').html(html);
}})
}






///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// PRELOAD
// COUNT ITEMS IN CART
countItemsInCart_function();

// PRELOAD ECOMMERCE ITEMS IF REQUESTED
if(typeof $("#mywdot_ecommerceItems").attr('mediaId') != 'undefined'){
loadEcommerceItems_function($("#mywdot_ecommerceItems").attr('mediaId'));}

// CLOSE DOCUMENT READY
})





///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// COMMENTS
function loadComments_function(loadUrl){	
lastChar=loadUrl.charAt(loadUrl.length-1);
if(lastChar!='/'){loadUrl=loadUrl+'/';}

$('html, body').animate({scrollTop:$("#mywdot_comments").offset().top}, 'slow');

if(typeof $("#mywdot_comments").attr('width')!='undefined'){
commentsWidth=$("#mywdot_comments").attr('width');}else{commentsWidth='500px';}

$('#mywdot_comments').html('<div class="mywdot"><div class="title_container"><div class="closeBtn" closeDiv="mywdot_comments"></div><div class="title">Comments</div></div>\
<fb:comments href="'+loadUrl+'" num_posts="10" width="'+commentsWidth+'" publish_feed="true"></fb:comments></div class="mywdot">');

FB.XFBML.parse(document.getElementById('mywdot_comments'),callBack_Function);
}

function callBack_Function(){
$('#mywdot_comments').mooSlideDown("slow");
}


///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// ECOMMERCE
var numberOfItemsInCart=0;

// LOAD ECOMMERCE
function loadEcommerceItems_function(mediaId){
attachLoading();
//hide_function();
$.ajax({
type: "POST",
url: "includes/ajax/site.php",
data: "action=loadEcommerceItems&mediaId="+mediaId,
success: function(html){	
removeLoading();
$('#mywdot_ecommerceItems').html(html);
$('#mywdot_ecommerceItems').mooSlideDown("slow");
}});
}

// ADD TO CART
function addToCart_function(itemId){
attachLoading();
hide_function();
//itemId_array=itemId.split('itemId_');
$.ajax({
type: "POST",
url: "includes/ajax/site.php",
data: "action=addToCart&itemId="+itemId,
success: function(html){	
removeLoading();
numberOfItemsInCart=html;
showHide_checkoutBtn_fucntion();
}});
}

function countItemsInCart_function(){
$.ajax({
type: "POST",
url: "includes/ajax/site.php",
data: "action=countItemsInCart",
success: function(html){	
numberOfItemsInCart=html;
showHide_checkoutBtn_fucntion();
}
});
}

function showHide_checkoutBtn_fucntion(){

if(numberOfItemsInCart>0){
//if(!($('#mywdot_loadCheckoutBtn').is(":visible"))){
$('#numberOfItemsInCart').html(numberOfItemsInCart);
$('#mywdot_loadCheckoutBtn').slideRight();//}
}else{
if($('#mywdot_loadCheckoutBtn').is(":visible")){	
$('#mywdot_loadCheckoutBtn').slideLeft();}}
}

//function hide_checkoutBtn_fucntion(){
//$('#mywdot_loadCheckoutBtn').slideLeft();	
//}

/////////////////////////////////////////                             CHECKOUT
function loadCheckout_function(){	
attachLoading();
//hide_function();
$('#mywdot_loadCheckoutBtn').slideLeft();
$.ajax({
type: "POST",
url: "includes/ajax/site.php",
data: "action=loadCheckout",
success: function(html){
removeLoading();
$('html, body').animate({scrollTop:0}, 'slow');
$('#mywdot_checkout').html(html);
$('#mywdot_checkout').mooSlideDown("slow");
}
});
}

// REMOVE FROM CART
function removeFromCart_function(itemId){
attachLoading();
//itemId_array=itemId.split('itemId_');
$.ajax({
type: "POST",
url: "includes/ajax/site.php",
data: "action=removeFromCart&itemId="+itemId,
success: function(html){	
removeLoading();
numberOfItemsInCart=html;
loadCheckout_function();
}});
}


/*
function closeCheckout_function(){
$('#mywdot_checkout').mooSlideUp();
showHide_checkoutBtn_fucntion();	
}
*/










///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////// LOADING FUNCTIONS
var loading=0;

function attachLoading(){ 
if(loading=='0'){
loading=1;
$('#mywdot_loading').css({"height": "5000px" });
}
}

function removeLoading(){ 
loading=0;
$('#mywdot_loading').css({"height":"0px"});
}





function hide_function(){ 
if($("#mywdot_comments").css("display") != "none" ){

if($('.mywdot_loadCommentsBtn').length>0){
$("#mywdot_comments").hide();}

}

if($("#mywdot_ecommerceItems").css("display") != "none" ){
if($('.mywdot_loadEcommerceItemsBtn').length>0){
$("#mywdot_ecommerceItems").hide();}
}

if($("#mywdot_checkout").css("display") != "none" ){
$("#mywdot_checkout").hide();}

if($("#mywdot_loadCheckoutBtn").css("display") != "none" ){
$("#mywdot_loadCheckoutBtn").hide();}

}





function clearWithSlideUp_function(){ 


if($("#mywdot_comments").css("display") != "none" ){
$("#mywdot_comments").mooSlideUp("slow");}

if($("#mywdot_ecommerceItems").css("display") != "none" ){
$("#mywdot_ecommerceItems").mooSlideUp("slow");}

if($("#mywdot_checkout").css("display") != "none" ){
$("#mywdot_checkout").mooSlideUp("slow");}

showHide_checkoutBtn_fucntion();

}






function touchDeviceTest() {
	var el = document.createElement('div');
	el.setAttribute('ongesturestart', 'return;');
	if(typeof el.ongesturestart == "function"){
		return 'touchstart';
	}else {
		return 'click';
	}
}


///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////// MOO SLIDE FUNCTIONS


(function($){


$.fn.slideRight=function(){
return this.each(function() {      
jQuery(this).animate({width: 'show'});
});  
}

$.fn.slideLeft=function(){   
return this.each(function() {      
jQuery(this).animate({width: 'hide'});
});
}



$.fn.mooSlideDown=function(speed,callback){ 
return this.each(function() { 
var $this = $(this);

if($this.data('animating')){
return false;}
else{	
$this.data('animating','true');
divHeight=$this.height();
$this.css({ "display":"block", "marginTop": "-"+divHeight+"px" });
$this.wrap('<div class="jwrapper"/>');
$this.parent().animate({			   
"height" : divHeight					   
}, speed, function() {
});

$this.animate({			   
"marginTop" : "0px"
}, speed, function() {	
$this.unwrap('.jwrapper');
$this.removeData('animating');
if (callback && $.isFunction(callback)) 
{callback.call($this);}
});
}

//Close this each
})
//Close mooSlideDown function
}

$.fn.mooSlideUp=function(speed,callback){ 

return this.each(function() { 
var $this = $(this);

if($this.data('animating')){
return false;}
else{	
$this.data('animating','true');
divHeight=$this.height();
$this.wrap('<div class="jwrapper"/>');
$this.parent().css({ "height": divHeight+"px" });
$this.parent().animate({			   
"height" : "0px"					   
}, speed, function() {
});

$this.animate({			   
"marginTop" : "-"+divHeight
}, speed, function() {
$this.css({ "display" : "none" });	
$this.unwrap('.jwrapper');
$this.removeData('animating')
if (callback && $.isFunction(callback)) 
{callback.call($this);}
});
}

//Close this each
})
//Close mooSlideUp function
}

$.fn.mooSlideToggle = function(speed, callback) {	
return this.each(function() {					  
var $this = $(this);
if($this.is(":visible")) {	
$this.mooSlideUp(speed, callback);
} else {
$this.mooSlideDown(speed, callback);
}
});
};
   
// CLOSE FUNCTION
})(jQuery);



