/**
 * Setup function for contact company
 *
 * @global array $contact_fields List of contact fields
 * @param string $field_name Field Name to be shown
 * @param <type> $rolo_tab_index
 * @since 0.1
 */
function rolo_setup_contact_company($field_name, &$rolo_tab_index, $contact_id = '')
{
    global $contact_fields;
    $company_field = $contact_fields[$field_name];
    $name = 'rolo_contact_' . $company_field['name'];
    if ($contact_id > 0) {
        $current_value = rolo_get_term_list($contact_id, 'company');
    } else {
        $current_value = $company_field['default_value'];
    }
    ?>
    <div class="ctrlHolder">
        <label for="<?php 
    echo $name;
    ?>
">
<?php 
    if ($company_field['mandatory'] == true) {
        echo '<em>*</em>';
    }
    echo $company_field['title'];
    ?>
        </label>
        <input type="text" name="<?php 
    echo $name;
    ?>
" value="<?php 
    echo $current_value;
    ?>
" size="55" tabindex="<?php 
    echo $rolo_tab_index++;
    ?>
" class="textInput company" />
    </div>
<?php 
}
/**
 * Setup field for editing address
 *
 * @global <type> $company_fields
 * @param <type> $field_name
 * @since 0.1
 */
function rolo_setup_company_address($field_name, &$rolo_tab_index, $company_id = '')
{
    global $company_fields;
    $address_field = $company_fields[$field_name];
    $company = get_post_meta($company_id, 'rolo_company', true);
    if (isset($company['rolo_company_address'])) {
        $current_value = $company['rolo_company_address'];
    } else {
        $current_value = '';
    }
    ?>
        <div class="ctrlHolder">
            <label for="rolo_company_address">
<?php 
    if ($address_field['mandatory'] == true) {
        echo '<em>*</em>';
    }
    echo $address_field['title'];
    ?>
            </label>
            <textarea rows="3" cols="20" name ="rolo_company_address" tabindex="<?php 
    echo $rolo_tab_index++;
    ?>
" class="textArea address" ><?php 
    echo $current_value;
    ?>
</textarea>
        </div>

<?php 
    $city = rolo_get_term_list($company_id, 'city');
    $state = rolo_get_term_list($company_id, 'state');
    $zip = rolo_get_term_list($company_id, 'zip');
    $country = rolo_get_term_list($company_id, 'country');
    $city = $city == '' ? 'City' : $city;
    $state = $state == '' ? 'State' : $state;
    $zip = $zip == '' ? 'Zip' : $zip;
    $country = $country == '' ? 'Country' : $country;
    ?>
        <div class="ctrlHolder">
            <input type="text" name="rolo_company_city" value="<?php 
    echo $city;
    ?>
" size="30" tabindex="<?php 
    echo $rolo_tab_index++;
    ?>
" class="textInput city" />
            <input type="text" name="rolo_company_state" value="<?php 
    echo $state;
    ?>
" size="15" tabindex="<?php 
    echo $rolo_tab_index++;
    ?>
" class="textInput state" />
            <input type="text" name="rolo_company_zip" value="<?php 
    echo $zip;
    ?>
" size="10" tabindex="<?php 
    echo $rolo_tab_index++;
    ?>
" class="textInput zip" />
        </div>

        <div class="ctrlHolder">
            <label for="rolo_company_country"></label>
            <input type="text" name="rolo_company_country" value="<?php 
    echo $country;
    ?>
" size="55" tabindex="<?php 
    echo $rolo_tab_index++;
    ?>
" class="textInput country" />
        </div>
<?php 
}
/**
 * Displays company detail information
 *
 * @param int $company_id
 * @return <type>
 *
 * @since 0.1
 */
function rolo_company_details($company_id)
{
    if (!$company_id) {
        return false;
    }
    $company = get_post_meta($company_id, 'rolo_company', true);
    $slug = $post_id->post_name;
    // define slug as $slug
    //    print_r($company);
    ?>
    <form id="company-form">
        <input type="hidden" name="rolo_post_id" id="rolo_post_id" value ="<?php 
    echo $company_id;
    ?>
" />
		<ul id="vcard-<?php 
    basename(get_permalink());
    ?>
" class="vcard">

		<li class="vcard-export"><a class="url-field" href="http://h2vx.com/vcf/<?php 
    the_permalink();
    ?>
"><span><?php 
    _e('Export vCard', 'rolopress');
    ?>
</span></a></li>
		
			<li>
                 <a class="fn org"
                    <?php 
    if (is_single()) {
        // show proper links on single or archive company pages
        ?>
                        href="<?php 
        echo get_term_link($company['rolo_company_name'], 'company');
        ?>
"><?php 
        echo $company['rolo_company_name'];
        ?>
                    <?php 
    } else {
        ?>
                        href="<?php 
        the_permalink();
        ?>
"><?php 
        echo $company['rolo_company_name'];
        ?>
                    <?php 
    }
    ?>
                </a>
			</li>
	               
			<?php 
    $rolo_company_full_address = $company['rolo_company_address'] . get_the_term_list($company_id, 'city', '', '', '') . get_the_term_list($company_id, 'state', '', '', '') . get_the_term_list($company_id, 'zip', '', '', '') . get_the_term_list($company_id, 'country', '', '', '');
    if ($rolo_company_full_address != "") {
        ?>
					<li class="map"><a class="url" href="http://maps.google.com/maps?f=q&source=s_q&geocode=&q=<?php 
        echo $company['rolo_company_address'] . " " . rolo_get_term_list($company_id, 'city') . " " . rolo_get_term_list($company_id, 'state') . " " . rolo_get_term_list($company_id, 'country') . " " . rolo_get_term_list($company_id, 'zip');
        ?>
 "><span><?php 
        _e('Map', 'rolopress');
        ?>
</span></a></li><?php 
    }
    ?>
		
			<li>
				<ul class="adr group">
				<span class="type hide">Work</span><!-- certain hcard parsers need this -->
				<?php 
    if ($company['rolo_company_address'] != "") {
        ?>
<li class="street-address" id="rolo_company_address"><?php 
        echo $company['rolo_company_address'];
        ?>
</li><?php 
    }
    if (get_the_term_list($company_id, 'city', '', '', '') != "") {
        ?>
<li class="url-field"><span class="type"><?php 
        _e('City', 'rolopress');
        ?>
</span><?php 
        echo get_the_term_list($company_id, 'city', '', '', '');
        ?>
<span id="city" class="locality edit-icon" style=""><?php 
        echo rolo_get_term_list($company_id, 'city');
        ?>
</span></li><?php 
    }
    if (get_the_term_list($company_id, 'city', '', '', '') != "") {
        ?>
<li class="url-field"><span class="type"><?php 
        _e('State', 'rolopress');
        ?>
</span><?php 
        echo get_the_term_list($company_id, 'state', '', '', '');
        ?>
<span id="state" class="region edit-icon" style=""><?php 
        echo rolo_get_term_list($company_id, 'state');
        ?>
</span></li><?php 
    }
    if (get_the_term_list($company_id, 'city', '', '', '') != "") {
        ?>
<li class="url-field"><span class="type"><?php 
        _e('Zip', 'rolopress');
        ?>
</span><?php 
        echo get_the_term_list($company_id, 'zip', '', '', '');
        ?>
</a><span id="zip" class="postal-code edit-icon" style=""><?php 
        echo rolo_get_term_list($company_id, 'zip');
        ?>
</span></li><?php 
    }
    if (get_the_term_list($company_id, 'city', '', '', '') != "") {
        ?>
<li class="url-field"><span class="type"><?php 
        _e('Country', 'rolopress');
        ?>
</span><?php 
        echo get_the_term_list($company_id, 'country', '', '', '');
        ?>
<span id="country" class="country-name edit-icon" style=""><?php 
        echo rolo_get_term_list($company_id, 'country');
        ?>
</span></li><?php 
    }
    ?>
				
				</ul>
			</li>


            <?php 
    if ($company['rolo_company_email'] != "") {
        ?>
<li class="email-address url-field"><a class="email" href="mailto:<?php 
        echo $company['rolo_company_email'];
        ?>
"><?php 
        echo $company['rolo_company_email'];
        ?>
</a><span id="rolo_company_email" class="edit-icon" style=""><?php 
        echo $company['rolo_company_email'];
        ?>
</span></li><?php 
    }
    ?>

            <li>
                <ul class="tel group">
				<?php 
    if ($company['rolo_company_phone_Mobile'] != "") {
        ?>
<li class="tel tel-mobile"><span class="type"><?php 
        _e('Mobile', 'rolopress');
        ?>
</span>: <span class="value" id="rolo_company_phone_Mobile"><?php 
        echo $company['rolo_company_phone_Mobile'];
        ?>
</span></li><?php 
    }
    if ($company['rolo_company_phone_Work'] != "") {
        ?>
<li class="tel tel-work"><span class="type"><?php 
        _e('Work', 'rolopress');
        ?>
</span>: <span class="value" id="rolo_company_phone_Work"><?php 
        echo $company['rolo_company_phone_Work'];
        ?>
</span></li><?php 
    }
    if ($company['rolo_company_phone_Fax'] != "") {
        ?>
<li class="tel tel-fax"><span class="type"><?php 
        _e('Fax', 'rolopress');
        ?>
</span>: <span class="value" id="rolo_company_phone_Fax"><?php 
        echo $company['rolo_company_phone_Fax'];
        ?>
</span></li><?php 
    }
    if ($company['rolo_company_phone_Other'] != "") {
        ?>
<li class="tel tel-other"><span class="type"><?php 
        _e('Other', 'rolopress');
        ?>
</span>: <span class="value" id="rolo_company_phone_Other"><?php 
        echo $company['rolo_company_phone_Other'];
        ?>
</span></li><?php 
    }
    ?>
                </ul>
            </li>

            <li>
                <ul class="im social group">
				<?php 
    if ($company['rolo_company_im_Yahoo'] != "") {
        ?>
<li class="social social-yahoo url-field"><span class="type"><?php 
        _e('Yahoo', 'rolopress');
        ?>
</span>: <a class="yahoo" href="ymsgr:sendIM?<?php 
        echo $company['rolo_company_im_Yahoo'];
        ?>
"><?php 
        echo $company['rolo_company_im_Yahoo'];
        ?>
</a><span id="rolo_company_im_Yahoo" class="edit-icon" style=""><?php 
        echo $company['rolo_company_im_Yahoo'];
        ?>
</span></li><?php 
    }
    if ($company['rolo_company_im_MSN'] != "") {
        ?>
<li class="social social-msn url-field"><span class="type"><?php 
        _e('MSN', 'rolopress');
        ?>
</span>: <a class="msn" href="msnim:chat?company=<?php 
        echo $company['rolo_company_im_MSN'];
        ?>
"><?php 
        echo $company['rolo_company_im_MSN'];
        ?>
</a><span id="rolo_company_im_MSN" class="edit-icon" style=""><?php 
        echo $company['rolo_company_im_MSN'];
        ?>
</span></li><?php 
    }
    if ($company['rolo_company_im_AIM'] != "") {
        ?>
<li class="social social-aim url-field"><span class="type"><?php 
        _e('AIM', 'rolopress');
        ?>
</span>: <a class="aim" href="aim:goIM?<?php 
        echo $company['rolo_company_im_AOL'];
        ?>
"><?php 
        echo $company['rolo_company_im_AOL'];
        ?>
</a><span id="rolo_company_im_AOL" class="edit-icon" style=""><?php 
        echo $company['rolo_company_im_AOL'];
        ?>
</span></li><?php 
    }
    if ($company['rolo_company_im_GTalk'] != "") {
        ?>
<li class="social social-gtalk url-field"><span class="type"><?php 
        _e('GTalk', 'rolopress');
        ?>
</span>: <a class="gtalk" href="gtalk:chat?jid=<?php 
        echo $company['rolo_company_im_GTalk'];
        ?>
"><?php 
        echo $company['rolo_company_im_GTalk'];
        ?>
</a><span id="rolo_company_im_GTalk" class="edit-icon" style=""><?php 
        echo $company['rolo_company_im_Yahoo'];
        ?>
</span></li><?php 
    }
    if ($company['rolo_company_twitter'] != "") {
        ?>
<li class="social social-twitter url-field"><span class="type"><?php 
        _e('Twitter', 'rolopress');
        ?>
</span> <a class="twitter" href="http://www.twitter.com/<?php 
        echo $company['rolo_company_twitter'];
        ?>
"><?php 
        echo $company['rolo_company_twitter'];
        ?>
</a><span id="rolo_company_twitter" class="edit-icon" style=""><?php 
        echo $company['rolo_company_twitter'];
        ?>
</span></li><?php 
    }
    ?>
                </ul>
            </li>

    		<?php 
    if ($company['rolo_company_website'] != "") {
        ?>
<li class="website url-field group"><span class="type"><?php 
        _e('Website', 'rolopress');
        ?>
</span> <a class="url" href="http://<?php 
        echo $company['rolo_company_website'];
        ?>
"><?php 
        echo $company['rolo_company_website'];
        ?>
</a><span id="rolo_company_website" class="edit-icon" style=""><?php 
        echo $company['rolo_company_website'];
        ?>
</span></li><?php 
    }
    ?>
			
			<?php 
    if ($company['rolo_company_post_tag'] != "") {
        ?>
				<li class="tags url-field"><span class="type"><?php 
        _e('Tags', 'rolopress');
        ?>
</span>
					<?php 
        $post_tags = get_the_tags();
        $tag_list = '';
        $i = 0;
        foreach ($post_tags as $pt) {
            $tag_list .= $pt->name;
            if ($i + 1 < sizeof($post_tags)) {
                $tag_list .= ', ';
            }
            $i++;
        }
        $tag_links = get_the_term_list($cid, 'post_tag', '', ', ');
        $tag_links = explode(', ', $tag_links);
        ?>
					
				<ul class="tags group">
					<?php 
        foreach ($tag_links as $i => $tag) {
            ?>
						<li class="url-field">
							<?php 
            echo $tag;
            ?>
							<?php 
            if ($i + 1 == sizeof($tag_links)) {
                ?>
								<span id="post_tag" class="edit-icon" style=""><?php 
                echo $tag_list;
                ?>
</span>
							<?php 
            }
            ?>
						</li>
			
				<?php 
            if ($i + 1 < sizeof($tag_links)) {
                echo ', ';
            }
            ?>
					<?php 
        }
        ?>
				</ul>
			<?php 
    }
    ?>

</li>
			
			
			
			
			<?php 
    rolopress_after_company_details();
    ?>
    </ul><!-- vcard -->
    </form>
<?php 
}
/**
 * RoloPress master loop
 *
 * Currently handles most situations
 *
 * @since 1.2
 */
function rolo_loop()
{
    if (!is_single()) {
        // This class is not needed on single pages
        ?>
<!-- 	<ul class="item-list">  -->
<?php 
    }
    if (have_posts()) {
        while (have_posts()) {
            the_post();
            ?>

	<?php 
            $current_user_id = get_current_user_id();
            global $post;
            $author_id = $post->post_author;
            $skype_status = '';
            if ($current_user_id != $author_id) {
                continue;
            }
            $thumbid = get_post_thumbnail_id($post->ID);
            $frame_number = get_post_meta($post->ID, 'rolo_contact_framename', true);
            $thumb = wp_get_attachment_image_src($thumbid, $frame_number);
            // <------- NAO ESTA A FUNCIONAR
            $srcimage = $thumb[0];
            if (isset($srcimage)) {
                $style = "background:url(" . $srcimage . ") no-repeat;";
            } else {
                $style = "background: transparent no-repeat";
            }
            $link = get_permalink();
            global $_wp_additional_image_sizes;
            $w = $_wp_additional_image_sizes['frame' . $frame_number]['width'];
            $h = $_wp_additional_image_sizes['frame' . $frame_number]['height'];
            if (rolo_type_is('contact')) {
                $contact = get_post_meta($post->ID, 'rolo_contact', true);
                if (isset($contact['rolo_contact_phone'])) {
                    $skype_name = $contact['rolo_contact_phone'];
                    $link_skype = "skype:" . $skype_name . " ?call";
                }
                $importance = get_post_meta($post->ID, 'rolo_contact_importance', true);
            }
            if (is_archive() || is_home()) {
                ?>
		
		<div id="contact-list">		
			<div class="photo" id="<?php 
                echo $frame_number;
                ?>
">			
				<a href="<?php 
                echo $link;
                ?>
">
				<span class="<?php 
                echo $frame_number;
                ?>
" style="background: url('') no-repeat;">
				
				<?php 
                the_post_thumbnail($frame_number, array('alt' => $skype_name, 'title' => get_the_title()));
                // AQUI É ONDE É POSTA A FOTO
                ?>
				
				</span>
				</a>
				    <img src="<?php 
                echo get_bloginfo('template_url') . '/library/images/frames/' . $frame_number . '.png';
                ?>
" alt="">		
			 	<!-- 	<div id="title"> 	 -->
					<?php 
                //the_title();
                ?>
		  		 <!-- </div>  --> 
		  		
			</div>
		</div>	<!-- close div contact -->
		
			<?php 
                continue;
            }
            if (is_single()) {
                if (rolo_type_is('contact')) {
                    if (!empty($skype_name)) {
                        $skype_status = getSkypeStatus($skype_name);
                    }
                    ?>
 
			<h2 class="page-title"><?php 
                    the_title();
                    ?>
</h2>
				<div id="contact-single" > 
				
				<div class="left">
				
 				<div style="float:right;">
				
				<div class="photo" id="<?php 
                    echo $frame_number;
                    ?>
">
					<a href="<?php 
                    echo $link_skype;
                    ?>
">
					<span class="<?php 
                    echo $frame_number . ' ';
                    ?>
" style="background: url('') no-repeat;">
						<?php 
                    the_post_thumbnail($frame_number, array("alt" => $skype_name, 'title' => get_the_title()));
                    // AQUI É ONDE É POSTA A FOTO
                    ?>
					</span>
					</a>
						<img src="<?php 
                    echo get_bloginfo('template_url') . '/library/images/frames/' . $frame_number . '.png';
                    ?>
" alt="">		
				</div>	
				</div>
				</div>
				
				<?php 
                    if (isset($contact['rolo_contact_phone'])) {
                        ?>
					<div class="right">
					
					
					<div class="address">
						<?php 
                        $contact = get_post_meta($post->ID, 'rolo_contact');
                        $contact = $contact[0];
                        $address = $contact['rolo_contact_address'];
                        $city = rolo_get_term_list($contact_id, 'city');
                        $state = rolo_get_term_list($contact_id, 'state');
                        $zip = rolo_get_term_list($contact_id, 'zip');
                        $country = rolo_get_term_list($contact_id, 'country');
                        $city = $city == '' ? '' : $city;
                        //   $state = ($state == '') ? 'State' : $state;
                        $zip = $zip == '' ? '' : $zip;
                        $country = $country == '' ? '' : $country;
                        if (isset($address) && ($address != '' || $zip . $city . $country != '')) {
                            echo "Morada:<br/>";
                            echo $address . ' ' . $zip . ' ' . $city . ' ' . $country;
                        }
                        ?>
					
					</div>
						
					</div>	
				<?php 
                    }
                    the_content();
                }
                rolo_entry_footer();
            } elseif (is_search()) {
                ?>
			
			<div id="contact-list">		
			<div class="photo" id="<?php 
                echo 'frame' . $frame_number;
                ?>
">
				<a href="<?php 
                echo $link;
                ?>
">
				<span class="<?php 
                echo $frame_number;
                ?>
" style="background: url('') no-repeat;">
				
				<?php 
                the_post_thumbnail($frame_number);
                // AQUI É ONDE É POSTA A FOTO
                ?>
				
				</span>
				</a>
				    <img src="<?php 
                echo get_bloginfo('template_url') . '/library/images/frames/' . $frame_number . '.png';
                ?>
" alt="">		
				
			</div>
		</div>	<!-- close div contact -->
			
			<?php 
            } elseif (is_page()) {
                ?>
<div id="contact"><?php 
                the_content();
                // show the page content
                if (is_page_template('widgets.php') || is_page_template('widgets-no-sidebar.php')) {
                    // is this a widget page
                    if (is_active_sidebar("widget-page")) {
                        // is the widget area active
                        ?>
						<div class="widget-area">
						<ul class="xoxo">
				<?php 
                        dynamic_sidebar("widget-page");
                        ?>
						</ul> 
						</div><!-- #widget-area -->	
						<?php 
                    } else {
                        rolo_add_some_widgets_message();
                        // if not, show a message
                    }
                }
            } else {
                ?>
 
					<div id="contact">
						<li id="entry-<?php 
                echo basename(get_permalink());
                ?>
" class="entry-header">
							<?php 
                echo '<img class="entry-icon" src=' . ROLOPRESS_IMAGES . '/icons/rolo-default.jpg />';
                ?>
							<a class="entry-title" href="<?php 
                the_permalink();
                ?>
" rel="bookmark"><?php 
                the_title();
                ?>
</a>
						</li>				
					<?php 
            }
            ?>
					
			<!-- 	</div> --><!-- .entry-main -->
					
			

				<?php 
            rolopress_after_entry();
            // After entry hook
            ?>
				
		</div><!-- #entry-<?php 
            the_ID();
            ?>
 -->
<?php 
        }
        ?>

<?php 
        if (!is_single()) {
            // not needed on single pages
            ?>
<!-- 	</ul> --><!-- item-list-->
<?php 
        }
        ?>


<?php 
    } else {
        // 404 or no search results
        ?>

		<li id="entry-0" class="<?php 
        rolopress_entry_class();
        ?>
">
			<?php 
        rolopress_before_entry();
        // Before entry hook
        ?>
				<div class="entry-main">
				
					<?php 
        // on inital setup if no contacts or companies are created then
        // the menu items produce a 404
        // This will provide instructions on how to fix
        $referring_page = $_SERVER['REQUEST_URI'];
        if ($referring_page == "/type") {
            rolo_type_tax_message();
        } else {
            //rolo_404_message();
        }
        ?>
					
				</div><!-- .entry-main -->
			<?php 
        rolopress_after_entry();
        // After entry hook
        ?>
		</li><!-- #entry-0 -->

<?php 
    }
}