
var altGalCounter = 0;
var removedGalAlts = new Array();
/*function addGalleryAlts() {
    if(altGalCounter <= 10) {
        jQuery('#li_'+altGalCounter).show();
        jQuery('#li_'+altGalCounter+'error').show();
        /*var str = '<li id="li_'+(altCounter)+'"><input name="gallery_alt_'+altCounter+'" id="gallery_alt_'+altCounter+'" type="file" class="file" value="" onclick="javascript:galleryUploadListener(\"thumb\")"/><br /><span class="error" id="gallery_alt_error'+altCounter+'"></span></li>'
        jQuery(str).insertBefore('#more_alt');
        altGalCounter++;
    }
    else {
        jQuery('#more_alt').removeAttr('onclick');
        jQuery('#more_alt').html('Maximum of 10 alternate images allowed');
    }
}*/


function validateNewGalleryForm() {
       var validator = jQuery("#add_new_gallery").validate({
        rules: {
            gallery_title : {   
                                required: true,
                                 minlength: 3,
                                 special:true
                                 },
            gallery_comment : {required: true}/*,
            gallery_keywords : {required: true}*/
	   // agreement : { required: true }
            /*gallery_category : {required: true, noselect:true}*/
            
                        
        },
        
        messages: {
            gallery_title: { 
                             required: "Please enter a title for your Pic",
                             minLength: "Please enter at least 3 characters."
                          },
            gallery_comment: { 
                               required: "Please explain your pic"
                             }/*,        
            gallery_keywords: { 
                               required: "Please enter tag(s) for your gallery"
                              },
	    agreement:{ 
                        required: "you must accept the Terms and Conditions to proceed" 
			}*/
            
            
                    
            
		}, 
        // the errorPlacement has to take the table layout into account 
        errorPlacement: function(error, element) {
                 if(element.attr("name") == 'agreement'){
		    error.appendTo('#agreement_error'); 
		}else{
		     error.appendTo(element.next().next());
		}
        },

        invalidHandler : function (form, validator) {
            var errors = validator.numberOfInvalids();
            if(errors) {
                jQuery('#submit_error').html('<span class="error_submit">Some errors found. Please review detail.</span>');
            }
            else {
                jQuery('#submit_error').html('');
            }
        },


        // specifying a submitHandler prevents the default submit, good for the demo 
        submitHandler: function() {
            jQuery('#submit_error').html('');
               jQuery("#add_new_gallery").ajaxStart(function(){
                                           jQuery("#loading_add_gallery").show();
                                           jQuery("#loading_txt").show();
                                           
                                           jQuery("#submit_add_gallery").hide(); 
                                            });
                                            
            jQuery("#add_new_gallery").ajaxComplete(function(request, settings){
                                                  
                                             jQuery("#loading_add_gallery").hide();
                                             jQuery("#loading_txt").hide();
                                             
                                             jQuery("#submit_add_gallery").show();  
                                                 }); 
             if(jQuery("#uploaded_default").val() == "") {
				 jQuery('#submit_error').html('<span class="error_submit">Some errors found. Please review detail</span>');
                jQuery("#gallery_default_error").html('<div class="error">Please upload Main Pic</div>');
             }
             else if(jQuery('#agreement:checked').val() != "accepted") {
				 jQuery('#submit_error').html('<span class="error_submit">Some errors found. Please review detail</span>');
                jQuery('#agreement_error').html('<div class="error">you must accept the Terms and Conditions to proceed</div>');
            } else {
                jQuery("#gallery_default_error").html(''); 
                
                
                var queryString = jQuery('#add_new_gallery').formSerialize();
                      
                var options = {
                                url :  baseUrlForJs+'/gallery/gallery/add',
                                target : '#response',
                                type    : 'POST',
                                data :  queryString,
                                success: function (msg) {
                                var chunks = msg.split('|');
								setTimeout("sucessPopGallery(\""+chunks[2]+"\")", 200);
                                
                                    }
                                };
                jQuery.ajax(options);
             }
             
        }, 
        // set this class to error-labels to indicate valid fields 
        success: function(label) { 
            // set   as text for IE 
            label.html(" ").addClass("checked"); 
        }
    }); 

}

function sucessPopGallery(result)
{
jQuery.blockUI({ 
message: '<div class="pop_div">'+result+'<br/><div class="link"><img src="'+baseUrlForJs+'/images/pop_link_icon.gif" width="11" height="11" align="absmiddle"/><a href="'+baseUrlForJs+'" > Go To Home Page</a></div><div class="link"><img src="'+baseUrlForJs+'/images/pop_link_icon.gif" width="11" height="11" align="absmiddle"/><a href="'+baseUrlForJs+'/'+GALLERY_URL+'"> Go To Gallery </a></div><div class="link"><img src="'+baseUrlForJs+'/images/pop_link_icon.gif" width="11" height="11" align="absmiddle"/><a href="'+baseUrlForJs+'/'+GALLERY_URL+'/submission"> Add Another Gallery Designs</a></div></div>'});
jQuery("#submitB").hide(); 
jQuery("#subB").hide(); 
jQuery("#loading").hide();
jQuery("#loading_txt").hide();
}


function galleryUploadListener(type) {

new Ajax_upload('#gallery_'+type, {
                        //action: 'upload.php',
                        action: baseUrlForJs+'/gallery/gallery/handle/type/'+type, 
                        name: 'file',
                        onSubmit: function(file, extension) {
                                    jQuery("#gallery_"+type+"_error").html("");
                                    jQuery("#loading_"+type).show();
                                    jQuery('#gallery_'+type).hide(); 
                                    jQuery('#submit_add_gallery').hide(); 
                                },
                        onComplete : function(data,response){
                       
                            jQuery("#gallery_"+type+"_error").html("");
                            jQuery("#loading_"+type).hide();
                            jQuery('#gallery_'+type).show(); 
                            jQuery('#submit_add_gallery').show(); 
                            //split the string
                            var result = response.split('|',2);
                            
                             if(jQuery.trim(result[0]) == "FAILED") {
                                jQuery("#gallery_"+type+"_error").show();
                                switch(type) {
                                    case 'default':
                                       jQuery("#gallery_"+type+"_error").html(result[1]);  
                                    break;
                                                  
                                    
                                    default:
                                        jQuery("#gallery_"+type+"_error").html(result[1]);
                                    break;
                                }
                                //jQuery("#gallery_"+type+"_error").html('Error uploading '+type+' image due to the following errors: <br />'+result[1]);
                             }
                             else {
                              if(type == 'alt_1') {
                                    is_uploaded_model = true;
                                }
                                switch(type) {
                                    case 'default':
                                      // jQuery("#gallery_"+type+"_error").html(result[1]);  
                                    
                                                  
                                    
                                    
                                        jQuery('#uploaded_'+type).val(jQuery.trim(result[1]));
                                        var str = '<li class="uploaded_img_preview" id="li_'+type+'"><img id="'+type+'_preview" width="75" height="75" src="'+baseUrlForJs+'/temp/thumb_image/'+result[1]+'" /><br /><span class="change" id="'+type+'_remove_image" ><a href="javascript:void(0)" >(Remove)</a></span></li>';
                                        //jQuery(str).insertAfter('#gallery_'+type);
					jQuery(str).insertAfter('#preview_uploaded');
                                       
                                        jQuery("#"+type+"_remove_image").click( function () {
                                        jQuery('#'+type+'_preview').remove();
					 jQuery('#li_'+type).remove();
                                      //  jQuery("#gallery_"+type+"_error").html('');
					//jQuery("#gallery_"+type+"_error").hide('');
                                        jQuery("#gallery_"+type).show();
                                        jQuery('#uploaded_'+type).val('');
                                        });
                                        jQuery("#gallery_"+type).hide(); 
                                        break;
                                
                                    default:
                                          altGalCounter=altGalCounter+1;
                                        
                                         if(removedGalAlts.length > 0) { //user removed some pics
                                            
                                            removedGalAlts.sort();
                                            
                                            var emptyAltCounter = removedGalAlts.pop();
                                             jQuery('#uploaded_alt_'+emptyAltCounter).val(jQuery.trim(result[1]));

                                            var str = '<li id="li_'+emptyAltCounter+'"><img id="'+emptyAltCounter+'_preview" width="75" height="56" src="'+baseUrlForJs+'/temp/thumb_image/'+result[1]+'" /><br /><span class="change"><a href="javascript:void(0)" onclick="removeAltDesImage(\''+emptyAltCounter+'\')">(Remove)</a></span></li>';
                                         }
                                         else {
                                             jQuery('#uploaded_alt_'+altGalCounter).val(jQuery.trim(result[1]));
                                            var str = '<li id="li_'+altGalCounter+'"><img id="'+altGalCounter+'_preview" width="75" height="56" src="'+baseUrlForJs+'/temp/thumb_image/'+result[1]+'" /><br /><span class="change"><a href="javascript:void(0)" onclick="removeAltGalImage(\''+altGalCounter+'\')">(Remove)</a></span></li>';
                                           
                                         
                                         }
                                         
                                        jQuery('#upload_pic').before(str);
                                        if(altGalCounter == 10) {
                                           jQuery('#gallery_alt_1').hide();
					   jQuery('#gallery_alt_1').after('<div class="error" id="max_alt_error">Uploaded 10 images</div>');
                                        }
                                       
                                        break;
                                } 
                             }
                           
                        }    
}); 

}

function removeAltGalImage(counter)
{
    removedGalAlts.push(counter);
    
    jQuery('#li_'+counter).remove();
    jQuery('#uploaded_alt_'+counter).val('');
    
    altGalCounter=altGalCounter-1;
    
    if(altGalCounter < 10) {
	jQuery('#max_alt_error').remove();
       jQuery('#gallery_alt_1').show();   
    }                                   
                              
                          
}
function validateGalleryPostForm(){

       var validator = jQuery("#new_post_form").validate({
    
    
       rules : {gallery_you_tub : {you_tube_link : true}},
       messages :{gallery_you_tub: {you_tube_link : 'Enter valide you tub link'}},
       errorPlacement: function(error, element) {
                jQuery("#gallery_you_tub_error").html('');
                error.appendTo (element.next()); 
        },
       submitHandler: function(data) {

            if(altGalCounter > 0 || jQuery('#gallery_post').val() != '' || jQuery('#gallery_you_tub').val() !=''){
                var queryString = jQuery('#new_post_form').formSerialize();
                  
                var options = {
                                url :  baseUrlForJs+'/gallery/gallery/handlegallerypost/id/'+jQuery('#gallery_id').val(),
                                target : '#response',
                                type    : 'POST',
                                data :  queryString,
                                beforeSend : function(XMLHttpRequest){
                                           jQuery('#gallery_alt_1_error').html('');
                                           jQuery("#loading_gallery_post").show();
                                           jQuery("#loading_txt").show();
                                           jQuery("#submit_gallery_post").hide(); 
                                        },
                                success: function (msg) {
                                     jQuery("#loading_gallery_post").hide();
                                     jQuery("#loading_txt").hide();
                                     jQuery("#submit_gallery_post").show(); 
                                     if(jQuery.trim(msg) != 'False'){
                                    jQuery('#gallery_post').val('');
                                    jQuery('#response').prepend(jQuery.trim(msg));
                                    
                                    //ajax call to update the post count
                                    jQuery.ajax({
                                        url :  baseUrlForJs+'/gallery/gallery/getblogcount',
                                        type : 'POST',
                                        data : {id : jQuery('#gallery_id').val()},
                                        success : function (msg) {
                                            var chunks = msg.split('|');
                                            if(jQuery('#has_gallery_comment').val() == 'no'){
						jQuery('#response').attr('style','margin-bottom:30px;');
						jQuery('#gallerys_comment_png').show();
                                            }
                                            jQuery('#gallery_comment_count').hide();
                                            jQuery('#gallery_comment_count').html('');
                                            jQuery('#gallery_comment_count').html(chunks[1]+' people');
                                            jQuery('#gallery_comment_count').show();
                                            jQuery('#gallery_you_tub').val('');
					    
					    jQuery('#gallerys_comment_count').hide();
                                            jQuery('#gallerys_comment_count').html('');
                                            jQuery('#gallerys_comment_count').html(chunks[1]+' ');
                                            jQuery('#gallerys_comment_count').show();
                                            jQuery('#gallery_you_tub').val('');
                                            jQuery("iframe").contents().find("body").empty();
                                            
                                            altGalCounter = 0;
                                                    for(var gallBlogCounter = 1; gallBlogCounter <= 10; gallBlogCounter++)
                                                    {
                                                        jQuery('#li_'+gallBlogCounter).remove();
                                                        jQuery('#uploaded_alt_'+gallBlogCounter).val('');
                                                        
                                                    }
                                                    
                                                    for(var i=0 ; i < removedGalAlts.length ; i++ ){
                                                        removedGalAlts.splice(i, 1);
                                                    }
                                                   jQuery('#upload_pic').show(); 
                                        }
                                    });
                                   }else{
                                        jQuery.blockUI({ 
                                        message: jQuery('#login_popup'), 
                                        css: { width: '375px' }
                                                    });
                                   }
                                }
                                };
                jQuery.ajax(options);  
            }else{
                jQuery('#gallery_you_tub_error').show();
                jQuery('#gallery_you_tub_error').html('<div class="error">You must either comment, youtube link or upload at least one image file</div>');
               
            }  
            
            }, 
        // set this class to error-labels to indicate valid fields 
        success: function(label) { 
            // set   as text for IE 
            label.html(" ").addClass("checked"); 
        }
    });

}
function OLD2152009validateGalleryPostForm() {
    /*var validator = jQuery("#new_post_form").validate({
    
    
       rules : {gallery_post : {required : true}},
       messages :{gallery_post: {required : 'Please fill the comment box'}},
       errorPlacement: function(error, element) {
                error.appendTo (element.next()); 
        },
       submitHandler: function(data) {*/
            
             //console.log(jQuery('#uploaded_default').val());
             //console.log(jQuery('#uploaded_default_location').val());
          /*   jQuery("#new_post_form").ajaxStart(function(){
                                           jQuery("#loading_gallery_post").show();
                                           jQuery("#loading_txt").show();
                                           jQuery("#submit_gallery_post").hide(); 
                                           
                                            });
                                            
            jQuery("#new_post_form").ajaxComplete(function(request, settings){                    
                                             jQuery("#loading_gallery_post").hide();
                                             jQuery("#loading_txt").hide();
                                             jQuery("#submit_gallery_post").show(); 
                                            
                                                 }); */
             
            //var queryString = jQuery('#new_post_form').formSerialize();
            galleryAjaxOptions();
                  
            /*var options = {
                            url :  baseUrlForJs+'/gallery/gallery/handlegallerypost/id/'+jQuery('#gallery_id').val(),
                            target : '#response',
                            type    : 'POST',
                            data :  queryString,
                            success: function (msg) {
                                jQuery('#gallery_post').val('');
                                jQuery('#response').prepend(jQuery.trim(msg));
                                
                                //ajax call to update the post count
                                jQuery.ajax({
                                    url :  baseUrlForJs+'/gallery/gallery/getblogcount',
                                    type : 'POST',
                                    data : {id : jQuery('#gallery_id').val()},
                                    success : function (msg) {
                                        var chunks = msg.split('|');
                                        jQuery('#gallery_comment_count').hide();
                                        jQuery('#gallery_comment_count').html('');
                                        jQuery('#gallery_comment_count').html(chunks[1]+' people');
                                        jQuery('#gallery_comment_count').show();
                                    }
                                });
                            }
                            };
            jQuery.ajax(options);*/
             
       /* }, 
        // set this class to error-labels to indicate valid fields 
        success: function(label) { 
            // set   as text for IE 
            label.html(" ").addClass("checked"); 
        }
    });*/
}

function galleryRating()
{
   //alert(members_id);
    jQuery.ajax({
        type : 'POST',
        url : baseUrlForJs+'/gallery/gallery/handleaddrating',
        data : 'gallery_id='+jQuery('#item_id').val(),
        beforeSend:function (XMLHttpRequest){
                                           jQuery("#loading_favorite").show();
                                         //  jQuery("#image_favorite").hide();

                                        },
        success: function(msg){
            var chunks = msg.split('|');    
            //console.log(chunks);
            if(chunks[1] == 'SUCCESS') {
	       //alert(chunks[1]);
               if(chunks[3] == 1){
                  jQuery('#gallery_rating_count').html(chunks[3]+' member');
               }else{
                  jQuery('#gallery_rating_count').html(chunks[3]+' members');
               }
             // jQuery.blockUI({ message: '<div class="pop_div"><div class="close_btn"><img src="'+baseUrlForJs+'/images/close_btn.png" width="12" height="12" onclick="javascript:closeLoginPop()" /></div>'+chunks[2]+'</div>'});
               jQuery('#heart_it_'+chunks[4]).removeClass('heart_it');
               jQuery('#heart_it_'+chunks[4]).html('<div class="msg">'+chunks[2]+'</div>');
              
              
              
            }else{
                jQuery('#heart_it_'+chunks[4]).removeClass('heart_it');
               jQuery('#heart_it_'+chunks[4]).html('<div class="error">'+chunks[2]+'</div>');
            }
        }
    }); 
}

function showGalleryByCategory() {
    var catId = jQuery('#choose_category').val();
    var url = baseUrlForJs+'/'+GALLERY_URL+'/'+catId;
    jQuery('#show_cat').attr('action',url).attr('method','POST').submit();
}



function galleryUploadBlogFile(type) {


new Ajax_upload('#gallery_'+type, {
                        //action: 'upload.php',
                        action: baseUrlForJs+'/gallery/gallery/handleblogfile/type/'+type, 
                        name: 'file',
                        onSubmit: function(file, extension) {
                                     jQuery("#gallery_"+type+"_error").html("");
                                    jQuery("#loading_"+type).show();
                                    jQuery('#gallery_'+type).hide(); 
                                    jQuery('#submit_gallery_post').hide(); 
                                    //jQuery('#submit_add_gallery').hide(); 
                                    jQuery('#gallery_you_tub_error').html('')
                                    
                                    
                                },

                        onComplete : function(data,response){
                       
                            jQuery("#gallery_"+type+"_error").html("");
                            jQuery("#loading_"+type).hide();
                            jQuery('#gallery_'+type).show();
                            jQuery('#submit_gallery_post').show();  
                            //split the string
                              var result = response.split('|');
                           
                            
                            var otherInc=parseInt(result[3])+1;
                            
                             if(jQuery.trim(result[0]) == "FAILED") {
                                jQuery("#gallery_"+type+"_error").show();
                                
                                jQuery("#gallery_"+type+"_error").html(result[1]);
                                  
                                
                             }
                             else {
                             
                             altGalCounter=altGalCounter+1;
                             
                             var ext = result[4];
                             
                             
                             if(removedGalAlts.length > 0) { //user removed some pics
                                
                                removedGalAlts.sort();
                                
                                var emptyAltCounter = removedGalAlts.pop();
                                
                                 jQuery('#uploaded_alt_'+emptyAltCounter).val(jQuery.trim(result[1]));
                                var str = formatGalleryImageStr(emptyAltCounter,result[1], result[4]);
                             }
                             else {
                                 jQuery('#uploaded_alt_'+altGalCounter).val(jQuery.trim(result[1]));
                                var str = formatGalleryImageStr(altGalCounter,result[1], result[4]);
                               
                             
                             }
                             
                            jQuery('#upload_pic').before(str);
                            if(altGalCounter == 10) {
                               jQuery('#upload_pic').hide(); 
                            }
                            
                             }
                              
                        }    
}); 

}

/*function formatGalleryImageStr(counter, result, ext) {
    if(ext != "null") {
        return '<li class="uploaded_img_preview" id="li_'+counter+'"><img id="'+counter+'_preview" width="50" src="'+baseUrlForJs+'/images/icons/'+ext+'.gif" /><br />'+result+'<br /><span class="change"><a href="javascript:void(0)" onclick="removeAltGalImage(\''+counter+'\')">(Remove)</a></span></li>';
    }
    else {
        return '<li class="uploaded_img_preview" id="li_'+counter+'"><img id="'+counter+'_preview" width="50" src="'+baseUrlForJs+'/temp/thumb_image/'+result+'" /><br /><span class="change"><a href="javascript:void(0)" onclick="removeAltGalImage(\''+counter+'\')">(Remove)</a></span></li>';
    }
    
}*/

function checkSearchGallery() {
//    jQuery('#loading_search').show();
//    jQuery('#search_btn').hide();
//    //jQuery('#search_submit_img').hide();
//       jQuery('#search_error_span').html('');  
    
//                if(jQuery('#keyword').val() == ''){
//
//                jQuery('#search_error_span').html('<div class="error">Please enter proper keywords to search </div>');
//
//                jQuery('#loading_search').hide();
//                jQuery('#search_btn').show();
//                //jQuery('#search_submit_img').show();
//                    }else{
//                if(SERVER_MODE=='SERVER'){
//           location.replace(baseUrlForJs+'/gallery/search/'+encodeURIComponent(jQuery('#keyword').val()));
//	  }
//	  else{
//		location.replace(baseUrlForJs+'/gallery/gallery/index/keywords/'+encodeURIComponent(jQuery('#keyword').val())+'/search_in/'+jQuery('#search_in').val());
//	  }
//
//          // location.replace(baseUrlForJs+'/gallery/search/'+encodeURIComponent(jQuery('#keywords').val()));
//
//         }
//



var validator = jQuery("#gallery_search_form").validate({
        rules : {keywords :
                    {required : true}},
        messages :{keywords:
                    {required : 'Please enter proper keywords to search'}},
        errorPlacement: function(error, element) {
                 jQuery('#search_error_span').html('');
                error.appendTo('#search_error_span');
               // alert(jQuery('#keywords').val());


        },

         submitHandler: function(form) {

             jQuery('#loading_search').show();
            jQuery('#search_btn').hide();
           
            //alert('in submit');
             if(jQuery('#keywords').val() == 'keywords' ){

                jQuery('#search_error_span').html('<div class="error">Please enter proper keywords to search </div>');

                jQuery('#loading_search').hide();
                jQuery('#search_btn').show();
               
                    }else{
                        //form.submit();
						location.replace(baseUrlForJs+'/gallery/?keywords='+encodeURIComponent(jQuery('#keywords').val())+'&search_in='+jQuery('#search_in').val()+'&searched=true');
                    }
			
         },
        // set this class to error-labels to indicate valid fields
        success: function(label) {
            // set   as text for IE
            label.html(" ").addClass("checked");
        }
    });
    
                
    
}

function selectKeywordsText()
{
    jQuery('#keywords').select();
}

