var ugc_request=false;



function fetchWritten(e,data){
var url ='/site/util/written_stories/?u='+data.user;
var AjaxObject = {

    handleSuccess:function(o){
        // This member handles the success response
        // and passes the response object o to AjaxObject's
        // processResult member.
        this.processResult(o);
    },

    handleFailure:function(o){
        // Failure handler
    },

    processResult:function(o){
        xml = o.responseXML.documentElement ;
        user_entries =xml.getElementsByTagName("entry");
        len= (user_entries.length >=5)? 5 : user_entries.length;
        
            var arr = new Array();
            for(var loop =0; loop < len; loop++){
                switch(user_entries[loop].attributes.getNamedItem("type").value){
                    case 'story':
                        title=user_entries[loop].firstChild.nodeValue;
                        cat= user_entries[loop].childNodes[1].firstChild.nodeValue.trim()
                        cat=(cat.trim() == "") ? data.template: cat.trim();
                        id=user_entries[loop].attributes.getNamedItem("id").value;
                        var item = document.createElement('li');
                        item.setAttribute('class','ugc_list');
                        var link = document.createElement('a');
                        link.setAttribute('href', 'http://'+cat+'.mync.com/site/'+cat+'/'+data.weblog+'/story/'+id);
                        link.setAttribute('class','ugc_item');
                        link.innerHTML = title.substring(0, 25);
                        link.innerHTML += (title.length < 25)? "" : "...";
                        item.appendChild(link);
                        arr.push(item);
                    break;
                    
                    case 'photo':
                        title=user_entries[loop].firstChild.nodeValue;
                        cat= user_entries[loop].childNodes[1].firstChild.nodeValue.trim()
                        cat=(cat.trim() == "") ? data.template: cat.trim();
                        id=user_entries[loop].attributes.getNamedItem("id").value;
                        var item = document.createElement('li');
                        item.setAttribute('class','ugc_list');
                        var link = document.createElement('a');
                        link.setAttribute('href', 'http://'+cat+'.mync.com/site/'+cat+'/submit_photos/'+id+'/1');
                        link.setAttribute('class','ugc_item_photo');
                        link.innerHTML = title.substring(0, 25);
                        link.innerHTML += (title.length < 25)? "" : "...";
                        item.appendChild(link);
                        arr.push(item);
                    break;
                    
                    case 'none-stories':
                        var item = document.createElement('li');
                        item.setAttribute('class','ugc_list');
                        item.innerHTML="No articles submitted";
                        arr.push(item); 
                    break;
                    
                    case 'none-photos':
                        var item = document.createElement('li');
                        item.setAttribute('class','ugc_list');
                        item.innerHTML="No photos submitted";
                        arr.push(item);                     
                    break;
                }
            }
            
            base=document.getElementById("ugc_menu");
            load=document.getElementById("ugc_loading");
            img=document.getElementById("ugc_loading_img");    
            load.removeChild(img);   
            base.removeChild(load);
            for(var loco=0; loco< arr.length; loco++){base.appendChild(arr[loco]);}
            
        var moreItem = document.createElement('li');
        moreItem.setAttribute('class','ugc_list');  
        var link = document.createElement('a');
        link.setAttribute('href', 'http://'+data.template+'.mync.com/site/'+data.template+'/submitted_content/');
        link.setAttribute('class','ugc_label');
        link.innerHTML = "View all your content...";
        moreItem.appendChild(link);
        base.appendChild(moreItem); 
 
    },

    startRequest:function() {
        ugc_request = true;
        YAHOO.util.Connect.asyncRequest('get',url, callback,null);
    }
};

var callback =
{
    success:AjaxObject.handleSuccess,
    failure:AjaxObject.handleFailure,
    scope: AjaxObject
};

// Start the transaction.
if(ugc_request == false){AjaxObject.startRequest();}
}


					YAHOO.util.Event.addListener("story_registration_btn", "click", show_registration);
					YAHOO.util.Event.addListener("story_login_btn", "click", show_login);
					
function mail(user,email)
{
    link = document.getElementById("field_id_50").value;
    if(link != "")
    {
        YAHOO.util.Dom.addClass('field_id_50', 'validate-url');
    } else
    {
        YAHOO.util.Dom.removeClass('field_id_50', 'validate-url');
    }
    if(FIC_checkForm(document.forms['entryform']))
    {
        select = document.getElementsByTagName("select");
        for (var in_count=0;in_count < select.length;in_count++)
        {
            if(select[in_count].name.indexOf("-") != -1)
            {
                select[in_count].name ="category[]";
            }
        }


        bodytext= document.getElementById("field_id_6").value;
        newbodytext="<p>";
        newbodytext+=bodytext.replace(/\r|\n|\r\n/g, "</p><p>");
        newbodytext=newbodytext.replace(/<p><\/p>/g, "");
        newbodytext+="</p>";
        document.getElementById("field_id_6").value = newbodytext;

        // Submit the image upload form. On return it will tell the main page to submit the primary form
        document.getElementById('file_upload_form').target = 'upload_target'; //'upload_target' is the name of the iframe        
        
        
        //create basic display overlay elements 
        var overlay = document.createElement('div');
        overlay.setAttribute('class','ugc_photo_info');     
        overlay.setAttribute('id','overlay');

        var shade1 = document.createElement('div');   
        shade1.setAttribute('id','shadebox');
        
        var shade2 = document.createElement('div');
        shade2.setAttribute('id','shadebox2');
        
        var head = document.createElement('h1');    
        heading_text = document.createTextNode("Processing Submission.");
        head.setAttribute('id','ugc_msg');  
        head.appendChild(heading_text);
        
        overlay.appendChild(head);
        landing = document.getElementById("popupspace");
        landing.appendChild(shade1);               
        landing.appendChild(shade2);  
        landing.appendChild(overlay);
        
        document.getElementById('file_upload_form').submit();
    }
}


/* Image Upload JS */
num_image_forms = 1;

// When you click add additional image, this animates the expansion to show the extra upload box
function show_additional_image_form()
{
	if (num_image_forms < 8)
	{
		num_image_forms = num_image_forms + 1;
		
		// Animate the container
		attributes = {
			height: {
				to: 80
			}
		}
		anim = new YAHOO.util.Anim('image_ul_'+num_image_forms, attributes, .75, YAHOO.util.Easing.easeIn);
	    anim.animate();
	        
        // The spacer div that makes a hole (since the real form is abs positioned) needs to grow as well
        var h = parseInt(YAHOO.util.Dom.getStyle('image_uploader_spacer', 'height').replace(/[^0-9]/g, '')) + 80;
        
        attributes = {
			height: {
				to: h
			}
		}
		anim = new YAHOO.util.Anim('image_uploader_spacer', attributes, .75, YAHOO.util.Easing.easeIn);
	    anim.animate();
	}

	if (num_image_forms > 7)
		YAHOO.util.Dom.setStyle('add_more_images', 'display', "none"); 		

}

YAHOO.util.Event.addListener("add_more_images", "click", show_additional_image_form);





// The iframe uses this function to pass it's results back up in json format
function upload_complete(x)
{
    var url_root = "/images/uploads/";
	var prod = YAHOO.lang.JSON.parse(x); 
	if (prod[0])
		document.getElementById("field_id_15").value = url_root + prod[0];
	if (prod[1])
		document.getElementById("field_id_29").value = url_root + prod[1];
	if (prod[2])
		document.getElementById("field_id_32").value = url_root + prod[2];	
	if (prod[3])
		document.getElementById("field_id_35").value = url_root + prod[3];	
	if (prod[4])
		document.getElementById("field_id_38").value = url_root + prod[4];	
	if (prod[5])
		document.getElementById("field_id_40").value = url_root + prod[5];	
	if (prod[6])
		document.getElementById("field_id_43").value = url_root + prod[6];	
	if (prod[7])
		document.getElementById("field_id_46").value = url_root + prod[7];	
    
    // Copy the captions from the bottom form into custom fields in the main form
    document.getElementById("field_id_23").value = document.getElementById("entry_field_id_23").value;
    document.getElementById("field_id_31").value = document.getElementById("entry_field_id_31").value;
    document.getElementById("field_id_34").value = document.getElementById("entry_field_id_34").value;
    document.getElementById("field_id_37").value = document.getElementById("entry_field_id_37").value;
    document.getElementById("field_id_61").value = document.getElementById("entry_field_id_61").value;
    document.getElementById("field_id_42").value = document.getElementById("entry_field_id_42").value;
    document.getElementById("field_id_45").value = document.getElementById("entry_field_id_45").value;
    document.getElementById("field_id_48").value = document.getElementById("entry_field_id_48").value;
    
    document.forms['entryform'].submit();
}

function upload_complete2(name, images, errors)
{
    
        //all of out EE image and image captioon fields
        image_fileds = new Array("field_id_15","field_id_29","field_id_32","field_id_35","field_id_15","field_id_38","field_id_40","field_id_43","field_id_46");
        caption_fields = new Array("field_id_23","field_id_31","field_id_34","field_id_37","field_id_61","field_id_42","field_id_45","field_id_48");
        image_fileds= image_fileds.reverse();
        caption_fields = caption_fields.reverse();
        var url_root = "/images/uploads/"+name+"/";
        var images = YAHOO.lang.JSON.parse(images);
        var errors = YAHOO.lang.JSON.parse(errors); 
        
        if(images.length != 0){  
        
        //create basic display elements
        var overlay = document.getElementById("overlay"); 
        var msg = document.getElementById("ugc_msg");
        overlay.removeChild(msg);
        var head = document.createElement('h1');    
        heading_text = document.createTextNode("Please caption your photos");
        head.appendChild(heading_text);
        
        overlay.appendChild(head); 
        
        var list = document.createElement('ul');
        list.setAttribute('class','ugc_photo_list');       
        
        //display all the thimbnails that need to be captioned
        for(var loopcount=0;loopcount < images.length; loopcount++){
            nextImage = image_fileds.pop();   
            document.getElementById(nextImage).value = url_root + images[loopcount];
            nextCaption =caption_fields.pop(); 
            
            
            var item = document.createElement('li');  
            item.setAttribute('id','ugc_photo_list_item');
            
            var pic = document.createElement('img');
            pic.src = "http://www.mync.com"+url_root+"thumb/"+images[loopcount];
            pic.setAttribute("class", "ugc_img");   
            
            var label = document.createElement('label');
            label.innerHTML="Please enter a caption:";
            
            var input = document.createElement('input');  
            input.setAttribute("type", "text");
            input.setAttribute("class", "ugc_input");
            input.setAttribute("name", "entry_"+nextCaption);
            input.setAttribute("id", "entry_"+nextCaption);
            
            item.appendChild(pic);
            item.appendChild(input);
            
            list.appendChild(item);              
        }
        
        //display all errors if any
        if (errors.length != 0){
            var error_block = document.createElement('div');  
            error_block.setAttribute('id','ugc_photo_error');
            
            var ehead = document.createElement('h3');    
            eheading_text = document.createTextNode("The following errors occurred:");
            ehead.appendChild(eheading_text);
            
            error_block.appendChild(ehead);
            
            for(var loopcount=0;loopcount < errors.length; loopcount++){
                var p = document.createElement('p');
                p_text = document.createTextNode(errors[loopcount]); 
                p.appendChild(p_text);
                error_block.appendChild(p);
            }
           
        }
        
        //finish it off with a done button  
        
        var donebut = document.createElement('input');  
        donebut.setAttribute("type", "button");
        donebut.setAttribute("class", "ugc_field");
        donebut.setAttribute("name", "submitCaption");
        donebut.setAttribute("id", "submitCaption");  
        donebut.setAttribute("value", "Done");    
        //attach it all to the dom
        
        overlay.appendChild(list);
        if (errors.length != 0){overlay.appendChild(error_block);} 
        overlay.appendChild(donebut); 

        YAHOO.util.Event.addListener("submitCaption", "click", finish_submit);    
    } else{finish_submit();}
}

function finish_submit(){
     if(document.getElementById("submitCaption")){
        var button = document.getElementById("submitCaption");
        button.value = "Finishing...";
        button.disabled =true;
     }    
    //Copy the captions from the bottom form into custom fields in the main form
    if(document.getElementById("entry_field_id_23"))
    document.getElementById("field_id_23").value = document.getElementById("entry_field_id_23").value;
    
    if(document.getElementById("entry_field_id_31"))    
    document.getElementById("field_id_31").value = document.getElementById("entry_field_id_31").value;
    
    if(document.getElementById("entry_field_id_34"))    
    document.getElementById("field_id_34").value = document.getElementById("entry_field_id_34").value;
    
    if(document.getElementById("entry_field_id_37"))    
    document.getElementById("field_id_37").value = document.getElementById("entry_field_id_37").value;
    
    if(document.getElementById("entry_field_id_61"))  
    document.getElementById("field_id_61").value = document.getElementById("entry_field_id_61").value;

    if(document.getElementById("entry_field_id_42"))    
    document.getElementById("field_id_42").value = document.getElementById("entry_field_id_42").value;
    
    if(document.getElementById("entry_field_id_45"))    
    document.getElementById("field_id_45").value = document.getElementById("entry_field_id_45").value;
    
    if(document.getElementById("entry_field_id_48"))    
    document.getElementById("field_id_48").value = document.getElementById("entry_field_id_48").value;
                              
    //finish submission
   document.forms['entryform'].submit();

    /*remove caption overlay (in theory this is a useless step.)
    landing = document.getElementById("image_uploader_spacer");  
    overlay = document.getElementById("overlay"); 
    shade1 = document.getElementById("shadebox"); 
    shade2 = document.getElementById("shadebox2");
    
    landing.removeChild(shade1); 
    landing.removeChild(shade2); 
    landing.removeChild(overlay);*/ 
    
}


YAHOO.namespace("cat.container");
(function() {
		
	// Define each tab as a YUI overlay. 

	function swapCities(e){
	if (!e) var e = window.event;		
	var obj= (e.srcElement) ? document.getElementById(e.srcElement.id): document.getElementById(e.target.id); //grab object 
var temp = 'k'+obj.value;

if(YAHOO.util.Dom.getStyle('checkhint', 'display') == 'block'){
YAHOO.util.Dom.setStyle('checkhint', 'display', "none");
YAHOO.util.Dom.setStyle('checkhint', 'visibility', "hidden");
}

radios = document.getElementsByTagName("input");
for (var in_count=0;in_count < radios.length;in_count++){
if(radios[in_count].type =='radio' && radios[in_count].id != obj.id)
radios[in_count].checked = false;
}


listed= document.getElementsByTagName("ul");
for (var ul_count=0;ul_count < listed.length;ul_count++){
if((YAHOO.util.Dom.getStyle(listed[ul_count], 'display') == 'block') && (listed[ul_count].id.indexOf('k') == 0)){
YAHOO.util.Dom.setStyle(listed[ul_count], 'display', "none");
YAHOO.util.Dom.setStyle(listed[ul_count], 'visibility', "hidden");
}
}

if(YAHOO.util.Dom.getStyle(temp, 'display') == 'block'){
YAHOO.util.Dom.setStyle(temp, 'display', "none");
YAHOO.util.Dom.setStyle(temp, 'visibility', "hidden");
}else{
YAHOO.util.Dom.setStyle(temp, 'display', "block");
YAHOO.util.Dom.setStyle(temp, 'visibility', "visible");
}
}
	YAHOO.util.Event.addListener("checkbox6", "click", swapCities);							
	YAHOO.util.Event.addListener("checkbox11", "click", swapCities);		
	YAHOO.util.Event.addListener("checkbox24", "click", swapCities);		
	YAHOO.util.Event.addListener("checkbox84", "click", swapCities);		
	YAHOO.util.Event.addListener("checkbox87", "click", swapCities);		
})();

function editUGCdisplay(data){
    var arr = new Array();
    base = document.getElementById("mast");
    curHeight=base.offsetHeight;            
    
    var item = document.createElement('div');
    item.setAttribute('id','ugc_story_display');
    var span = document.createElement('span');  
    span.innerHTML= "This is your Story:";
    item.appendChild(span);
    
    var ul = document.createElement('ul');
    ul.setAttribute('class','ugc_story_display_list');
    
    var li = document.createElement('li');
    li.setAttribute('class','ugc_story_display_list_item');  
    
    var span = document.createElement('span');  
    span.innerHTML= "Orginal Submitted:";
    span.setAttribute('class','ugc_story_display_list_span');
    
    li.appendChild(span);
    li.innerHTML+= data.orgSub;
    ul.appendChild(li);     
    
    var li = document.createElement('li');
    li.setAttribute('class','ugc_story_display_list_item');     
    var span = document.createElement('span');  
    span.innerHTML= "Last Modified:";
    span.setAttribute('class','ugc_story_display_list_span');    

    li.appendChild(span); 
    li.innerHTML+=data.lastMod;
    ul.appendChild(li);
    arr.push(ul);
    
    var ul = document.createElement('ul');
    ul.setAttribute('class','ugc_story_display_list');
       
    var li = document.createElement('li');
    li.setAttribute('class','ugc_story_display_list_item'); 
    var span = document.createElement('span');  
    span.innerHTML= "Shared in categories:";
    span.setAttribute('class','ugc_story_display_list_span_single');
    
    li.appendChild(span);     
    li.innerHTML+="<br />"+data.cat;
    ul.appendChild(li);
    arr.push(ul);
    
    var ul = document.createElement('ul');
    ul.setAttribute('class','ugc_story_display_list');   
    
    var li = document.createElement('li');
    li.setAttribute('class','ugc_story_display_list_item'); 
    var span = document.createElement('span');  
    span.innerHTML= "Published in Regions:";
    span.setAttribute('class','ugc_story_display_list_span_single');  
    
    li.appendChild(span); 
    li.innerHTML+="<br />"+data.region;
    ul.appendChild(li);
    arr.push(ul);
    
    var ul = document.createElement('ul');
    ul.setAttribute('class','ugc_story_display_list');  
    var li = document.createElement('li');
    li.setAttribute('class','ugc_story_display_list_item'); 
    var span = document.createElement('span');
    span.setAttribute('class','ugc_story_display_list_span');  
    span.innerHTML= "Total Comments:";
       
    li.appendChild(span); 
    li.innerHTML+=data.comm;  
    ul.appendChild(li);
       
    var li = document.createElement('li');
    li.setAttribute('class','ugc_story_display_list_item'); 
    var span = document.createElement('span');  
    span.innerHTML= "Total Views:";
    span.setAttribute('class','ugc_story_display_list_span');  
    
    li.appendChild(span); 
    li.innerHTML+=data.views;   
    ul.appendChild(li);
    arr.push(ul);
    
    for(var loco=0; loco< arr.length; loco++){item.appendChild(arr[loco]);}  
    base.appendChild(item);
    newHeight=base.scrollHeight;
    attributes = {
        height:            
    };
    anim = new YAHOO.util.Anim(base, attributes, 1, YAHOO.util.Easing.easeIn);      
    anim.animate();     
}