$(function(){
    $('.code_cover').click(function(){
        $(this).slideUp().parent().next().addClass('revealstyle').slideDown();
    });
});


$(function(){
    $('.showmore_link').click(function() {
        $(this).hide();
        $(this).next().show();
    });
});


function externalLinks() {
 if (!document.getElementsByTagName) return;
 var anchors = document.getElementsByTagName("a");
 for (var i=0; i<anchors.length; i++) {
   var anchor = anchors[i];
   if (anchor.getAttribute("href") &&
       anchor.getAttribute("rel") == "external")
     anchor.target = "_blank";
 }
}
window.onload = externalLinks;


function add_comment(coupon_id){

    //remove every single dialog before opening a new one
    //this is to make the ReCaptcha js function to work properly
    $('.ui-dialog').empty();

    //create the dialog
    $dialog = $('<div></div>')
          .load('/comments/add/coupon_id/'+coupon_id)
		.dialog({
			autoOpen: false,
			title: 'Add comment',
               modal: true,
               resizable: false,
               width:'auto',
               height:'auto',
               closeText: 'cancel',
               "buttons": {
                  "SUBMIT": function() {

                      var data = $('#add_comment_form').serialize();
                      
                      $.ajax({
                        type: "POST",
                        url: "/comments/validate-submission",
                        data: data,
                        success: function(response){
                             if(response[0] == 1 ){
                                 //success
                                 $('#error_container').text('');
                                 $('.ui-dialog-buttonpane').hide();
                                 $('.ui-dialog-content').text(response[1]);
                                 setTimeout(function(){
                                     $dialog.dialog('close');
                                     //$dialog.dialog("destroy");
                                 }, 3000);
                             }else{
                                //error
                                $('#error_container').text(response[1]);
                                //reload a new captcha
                                Recaptcha.reload();
                             }

                             },
                        error: function(msg){
                             alert("SERVER ERROR!!! " + msg);
                             }
                        });
                       }
                  }
		});

          //setTimeout gives the js engine the time to calculate the
          //right position for the UI dialog
          setTimeout(function(){
              //open the dialog
              $dialog.dialog('open');
          },500);

}

function voteCode(coupon_id, vote_type){
    //waiting icon
    $("#votebuttons" + coupon_id ).html('please wait...');
    //ajax voting
    $.ajax({
           type: "POST",
           url: "/codes/vote/",
           data: "couponid="+coupon_id+"&votetype="+vote_type,
           /*dataType: JSON*/
           success: function(msg){
               //if vote is saved
               if (msg[0]==1){ 
                   // update vote percentage
                   if (msg[1]){
                       $("#percentage_" + coupon_id ).html(msg[1]+'%');
                   }
                   // show thanks button
                   $("#votebuttons" + coupon_id ).html('<div class="thanksvote" ></div>');
               } else {
                   // display error, probably duplicate (msg[1]=2)
                   $("#votebuttons" + coupon_id ).html(msg[2]);
               }

               /*if (GLOBAL_NO_PROD){
                   console.log(msg[2] + ' (this message will not appear on production env)');
               }*/

           }
    });
    
    return false;
}

function SelectAll(id) {
    document.getElementById(id).focus();
    document.getElementById(id).select();
}
