<?php
/**
 * Social Icon Widget Class
 */
class Theme_Widget_Social extends WP_Widget {

	var $sites = array(
		'AIM','Apple','Bebo','Blogger','Brightkite','Cargo','Delicious','Designfloat',
		'Designmoo','Deviantart','Digg','Dopplr','Dribbble','Email','Ember','Evernote',
		'Facebook','Flickr','Forrst','Friendfeed','Gamespot','Google','GoogleVoice','GooglePlus','GoogleWave',
		'GoogleTalk','Gowalla','Grooveshark','iLike','lastFm','LinkedIn','Mixx','MobileMe',
		'Mynameise','MySpace','Netvibes','Newsvine','Openid','Orkut','Pandora','Pinboard','Paypal','Picasa',
		'Playstation','Plurk','Posterous','qik','Rdio','Readernaut','Reddit','Roboto','RSS','Sharethis',
		'Skype','Slashdot','Steam','StumbleUpon','Technorati','Tumblr','Twitter','Viddler','Vimeo','Virb','Wordpress',
		'Xanga','Xing','Yahoo','YahooBuzz','Yelp','YouTube','Zootool'
	);
	var $packages = array(
		'komodomedia_16' => array(
			'name'=>'Social Network Icon Pack 16px',
			'path'=>'komodomedia_16/{:name}_16.png',
		),
		'komodomedia_32' => array(
			'name'=>'Social Network Icon Pack 32px',
			'path'=>'komodomedia_32/{:name}_32.png',
		),
	);
	
	
    function Theme_Widget_Social() {
        $this->textdomain   = THEME_ADMIN_DOMAIN;
        $widget_ops = array('classname' => 'widget_social', 'description' => __( 'Displays a list of Social Icons', THEME_ADMIN_DOMAIN) );
		$this->WP_Widget('social', THEME_SLUG.' - '.__('Social Icon', THEME_ADMIN_DOMAIN), $widget_ops);
		
		if ('widgets.php' == basename($_SERVER['PHP_SELF'])) {
			add_action( 'admin_print_scripts', array(&$this, 'add_admin_script') );
		}
	}
	
	function add_admin_script(){
		wp_enqueue_script( 'social-icon-widget', THEME_ADMIN_ASSETS_URI . '/js/social-icon-widget.js', array('jquery'));
	}
	

	function widget( $args, $instance ) {
		extract( $args );
		$title = apply_filters('widget_title', empty($instance['title']) ? '' : $instance['title'], $instance, $this->id_base);
		$display_title = !empty($instance['display_title']) && $instance['display_title'] ? true : false;
        $alt = isset($instance['alt'])?$instance['alt']:'';
		$alt = wpml_t(THEME_NAME, 'Social Icon Widget Alt Title', $instance['alt']);
		$animation = isset($instance['animation'])?$instance['animation']:'fade';
		$linkTarget = isset($instance['linkTarget'])?$instance['linkTarget']:'_blank';
        $package = $instance['package'];
		$custom_count = $instance['custom_count'];
        $output = '<ul>';
        $title_class = 'icon32';
        if($package =='komodomedia_16')
            $title_class = 'icon16';
		if( !empty($instance['enable_sites']) ){
			foreach($instance['enable_sites'] as $site){
				$path = str_replace('{:name}',strtolower($site),$this->packages[$package]['path']);
				$link = isset($instance[$site])?$instance[$site]:'#';
                if(file_exists(THEME_DIR . '/images/social/'.$path)){
                    $under_name = '';
                    if($display_title)
                        $under_name ='<br/>'.$site;
					$output .= '<li class="social-item '.$title_class.'"><a href="'.$link.'" rel="nofollow" target="'.$linkTarget.'"><img src="'.THEME_IMAGES.'/social/'.$path.'" alt="'.$alt.' '.$site.'" title="'.$alt.' '.$site.'"/>'.$under_name.'</a></li>';
				}
			}
		}
		if( $custom_count > 0){
			for($i=1; $i<= $custom_count; $i++){
				$name = isset($instance['custom_'.$i.'_name'])?$instance['custom_'.$i.'_name']:'';
				$icon = isset($instance['custom_'.$i.'_icon'])?$instance['custom_'.$i.'_icon']:'';
                $link = isset($instance['custom_'.$i.'_url'])?$instance['custom_'.$i.'_url']:'#';
                if(!empty($icon)){
                    $under_name ='';
                    if($display_title)
                        $under_name='<br/>'.$name;
                    $output .= '<li class="social-item '.$title_class.'"><a href="'.$link.'" rel="nofollow" target="_blank"><img src="'.$icon.'" alt="'.$alt.' '.$name.'" title="'.$alt.' '.$name.'"/>'.$under_name.'</a></li>';
				}
			}
        }
        $output .='</ul><div class="clear"></div>';
		if ( !empty( $output ) ) {
			echo $before_widget;
			if ( $title)
				echo $before_title . $title . $after_title;
		?>
		<div class="social_wrap social_animation_<?php echo $animation;?> <?php echo $package;?>">
			<?php echo $output; ?>
		</div>
		<?php
			echo $after_widget;
		}
	}

	function update( $new_instance, $old_instance ) {

		$instance = $old_instance;
        $instance['title'] = strip_tags($new_instance['title']);
        $instance['display_title'] = !empty($new_instance['display_title']) ? 1 : 0;
		$instance['alt'] = strip_tags($new_instance['alt']);
		$instance['package'] = strip_tags($new_instance['package']);
		$instance['animation'] = strip_tags($new_instance['animation']);
		$instance['linkTarget'] = strip_tags($new_instance['linkTarget']);
		$instance['enable_sites'] = $new_instance['enable_sites'];
		$instance['custom_count'] = (int) $new_instance['custom_count'];

		if(!empty($instance['enable_sites'])){
			foreach($instance['enable_sites'] as $site){
				$instance[$site] = isset($new_instance[$site])?strip_tags($new_instance[$site]):'';
			}
		}
		for($i=1;$i<=$instance['custom_count'];$i++){
			$instance['custom_'.$i.'_name'] = strip_tags($new_instance['custom_'.$i.'_name']);
			$instance['custom_'.$i.'_url'] = strip_tags($new_instance['custom_'.$i.'_url']);
			$instance['custom_'.$i.'_icon'] = strip_tags($new_instance['custom_'.$i.'_icon']);
		}
		return $instance;
	}

	function form( $instance ) {
		//Defaults
        $title = isset($instance['title']) ? esc_attr($instance['title']) : '';
        $display_title = isset($instance['display_title']) ? esc_attr($instance['display_title']) : '';
		$alt = isset($instance['alt']) ? esc_attr($instance['alt']) : 'Follow Us on';
		$animation = isset($instance['animation']) ? $instance['animation'] : 'fade';
		$linkTarget = isset($instance['linkTarget']) ? $instance['linkTarget'] : '_blank';
		$package = isset($instance['package']) ? $instance['package'] : '';
		$enable_sites = isset($instance['enable_sites']) ? $instance['enable_sites'] : array();
		foreach($this->sites as $site){
			$$site = isset($instance[$site]) ? esc_attr($instance[$site]) : '';
		}

		$custom_count = isset($instance['custom_count']) ? absint($instance['custom_count']) : 0;
		for($i=1;$i<=10;$i++){
			$custom_name = 'custom_'.$i.'_name';
			$$custom_name = isset($instance[$custom_name]) ? $instance[$custom_name] : '';
			$custom_url = 'custom_'.$i.'_url';
			$$custom_url = isset($instance[$custom_url]) ? $instance[$custom_url] : '';
			$custom_icon = 'custom_'.$i.'_icon';
			$$custom_icon = isset($instance[$custom_icon]) ? $instance[$custom_icon] : '';
		}
	?>
		<p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:', THEME_ADMIN_DOMAIN); ?></label> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></p>
        <p><label for="<?php echo $this->get_field_id( 'display_title' ); ?>"><input class="checkbox" type="checkbox" <?php checked( $display_title, true ); ?> id="<?php echo $this->get_field_id( 'display_title' ); ?>" name="<?php echo $this->get_field_name( 'display_title' ); ?>" /> <?php _e( 'Display the title under icon image?', $this->textdomain ); ?> </label></p>
        <p><label for="<?php echo $this->get_field_id('alt'); ?>"><?php _e('Icon Alt Title:', THEME_ADMIN_DOMAIN); ?></label> <input class="widefat" id="<?php echo $this->get_field_id('alt'); ?>" name="<?php echo $this->get_field_name('alt'); ?>" type="text" value="<?php echo $alt; ?>" /></p>
		<p>
			<label for="<?php echo $this->get_field_id('package'); ?>"><?php _e( 'Icon Package:' , THEME_ADMIN_DOMAIN); ?></label>
			<select name="<?php echo $this->get_field_name('package'); ?>" id="<?php echo $this->get_field_id('package'); ?>" class="widefat">
				<?php foreach($this->packages as $name => $value):?>
				<option value="<?php echo $name;?>"<?php selected($package,$name);?>><?php echo $value['name'];?></option>
				<?php endforeach;?>
			</select>
		</p>
		<p>
			<label for="<?php echo $this->get_field_id('animation'); ?>"><?php _e( 'Icon Hover Animation:', THEME_ADMIN_DOMAIN ); ?></label>
			<select name="<?php echo $this->get_field_name('animation'); ?>" id="<?php echo $this->get_field_id('animation'); ?>" class="widefat">
				<option value="fade"<?php selected($animation,'fade');?>><?php _e( 'Fade', THEME_ADMIN_DOMAIN ); ?></option>
				<option value="scale"<?php selected($animation,'scale');?>><?php _e( 'Scale', THEME_ADMIN_DOMAIN ); ?></option>
				<option value="bounce"<?php selected($animation,'bounce');?>><?php _e( 'Bounce', THEME_ADMIN_DOMAIN ); ?></option>
				<option value="combo"<?php selected($animation,'combo');?>><?php _e( 'Combo', THEME_ADMIN_DOMAIN ); ?></option>
			</select>
		</p>
		<p>
			<label for="<?php echo $this->get_field_id('linkTarget'); ?>"><?php _e( 'Link Target:', THEME_ADMIN_DOMAIN ); ?></label>
			<select name="<?php echo $this->get_field_name('linkTarget'); ?>" id="<?php echo $this->get_field_id('linkTarget'); ?>" class="widefat">
				<option value="_blank"<?php selected($linkTarget,'_blank');?>><?php _e( 'Load in a new window', THEME_ADMIN_DOMAIN ); ?></option>
				<option value="_self"<?php selected($linkTarget,'_self');?>><?php _e( 'Load in the same frame as it was clicked', THEME_ADMIN_DOMAIN ); ?></option>
				<option value="_parent"<?php selected($linkTarget,'_parent');?>><?php _e( 'Load in the parent frameset', THEME_ADMIN_DOMAIN ); ?></option>
				<option value="_top"<?php selected($linkTarget,'_top');?>><?php _e( 'Load in the full body of the window', THEME_ADMIN_DOMAIN ); ?></option>
			</select>
		</p>
		<p>
			<label for="<?php echo $this->get_field_id('enable_sites'); ?>"><?php _e( 'Enable Social Icon:', THEME_ADMIN_DOMAIN ); ?></label>
			<select name="<?php echo $this->get_field_name('enable_sites'); ?>[]" style="height:10em" id="<?php echo $this->get_field_id('enable_sites'); ?>" class="social_icon_select_sites widefat" multiple="multiple">
				<?php foreach($this->sites as $site):?>
				<option value="<?php echo $site;?>"<?php echo in_array($site, $enable_sites)? 'selected="selected"':'';?>><?php echo $site;?></option>
				<?php endforeach;?>
			</select>
		</p>
		
		<p>
			<em><?php _e("Note: Please input FULL URL <br/>(e.g. <code>http://www.example.com</code>)", THEME_ADMIN_DOMAIN);?></em>
		</p>
		<div class="social_icon_wrap">
		<?php foreach($this->sites as $site):?>
		<p class="social_icon_<?php echo $site;?>" <?php if(!in_array($site, $enable_sites)):?>style="display:none"<?php endif;?>>
			<label for="<?php echo $this->get_field_id( $site ); ?>"><?php echo $site.' '.__('URL:', THEME_ADMIN_DOMAIN)?></label>
			<input class="widefat" id="<?php echo $this->get_field_id( $site ); ?>" name="<?php echo $this->get_field_name( $site ); ?>" type="text" value="<?php echo $$site; ?>" />
		</p>
		<?php endforeach;?>
		</div>

		<p><label for="<?php echo $this->get_field_id('custom_count'); ?>"><?php _e('How many custom icons to add?', THEME_ADMIN_DOMAIN); ?></label>
		<input id="<?php echo $this->get_field_id('custom_count'); ?>" class="social_icon_custom_count" name="<?php echo $this->get_field_name('custom_count'); ?>" type="text" value="<?php echo $custom_count; ?>" size="3" /></p>

		<div class="social_custom_icon_wrap">
		<?php for($i=1;$i<=10;$i++): $custom_name='custom_'.$i.'_name';$custom_url='custom_'.$i.'_url'; $custom_icon='custom_'.$i.'_icon'; ?>
			<div class="social_icon_custom_<?php echo $i;?>" <?php if($i>$custom_count):?>style="display:none"<?php endif;?>>
				<p><label for="<?php echo $this->get_field_id( $custom_name ); ?>"><?php printf(__('Custom %s Name:', THEME_ADMIN_DOMAIN),$i);?></label>
				<input class="widefat" id="<?php echo $this->get_field_id( $custom_name ); ?>" name="<?php echo $this->get_field_name( $custom_name ); ?>" type="text" value="<?php echo $$custom_name; ?>" /></p>
				<p><label for="<?php echo $this->get_field_id( $custom_url ); ?>"><?php printf(__('Custom %s URL:', THEME_ADMIN_DOMAIN),$i);?></label>
				<input class="widefat" id="<?php echo $this->get_field_id( $custom_url ); ?>" name="<?php echo $this->get_field_name( $custom_url ); ?>" type="text" value="<?php echo $$custom_url; ?>" /></p>
				<p><label for="<?php echo $this->get_field_id( $custom_icon ); ?>"><?php printf(__('Custom %s Icon:', THEME_ADMIN_DOMAIN),$i);?></label>
				<input class="widefat" id="<?php echo $this->get_field_id( $custom_icon ); ?>" name="<?php echo $this->get_field_name( $custom_icon ); ?>" type="text" value="<?php echo $$custom_icon; ?>" /></p>
			</div>

		<?php endfor;?>
		</div>


		
<?php
	}
}
